Python Forum
Why is ssh connecting but giving no output?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is ssh connecting but giving no output?
#1
I have the code below that runs without errors, but doesn't seem to give any output after establishing the connection, nor after running exec_command to execute the command. Any thoughts on why that is?

 #####################################################
# IN: hostname to connect to, username to connect as, string of command(s) to run
# OUT: array of information to parse for OS; returns nothing if not able to connect

def SSHConnect(hostname, username, command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    output = []

    try:
        Log("info",  inspect.getframeinfo(inspect.currentframe()).function, "Connecting to " + hostname + " by ssh as user " + username + " t
        result = ssh.connect(hostname, username=username, password=GetCreds(username), auth_timeout=30, look_for_keys=True)
        print("SSH result is " + str(result))
        (sdtin, stdout, stderr) = ssh.exec_command(command)
        Log("info", "stdout is " + str(stdout) + ", stderr is " + str(stderr))
        output = stdout.readlines()
        if (stderr):
            Log("err", inspect.getframeinfo(inspect.currentframe()).function, "Errors generated - " + str(stderr))
        ssh.close()
    except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:
        Log("err",  inspect.getframeinfo(inspect.currentframe()).function, "Error in connecting - " + message)
        pass

    finally:
        Log("debug",  inspect.getframeinfo(inspect.currentframe()).function, "Output - " + str(output))
        return output
It runs without errors, and accepts the password, but the output contains nothing. In contrast, I can ssh to it from the bash prompt and the desired command (hostname), using the same password.

Output:
$ rm -f /tmp/ssh.log ; ./ssh-test.py; cat /tmp/ssh.log Starting, using ssh-test.cfg and writing to logfile /tmp/ssh.log SSHConnect - Connecting to hostname-here by ssh as user username-here to run command(s) hostname GetCreds - Found credentials for user username-here SSH result is None <module> - Finished. Duration is 0.092745 seconds. 2018-10-02 16:20:15,773 - <module> - INFO - Started ./ssh-test.py at 0.15 2018-10-02 16:20:15,773 - <module> - INFO - Using config file ssh-test.cfg 2018-10-02 16:20:15,798 - Log - INFO - SSHConnect - Connecting to ga-devarcmsdc19.ds.dtveng.net by ssh as user d401771 to run command(s) hostname 2018-10-02 16:20:15,798 - Log - DEBUG - GetCreds - Getting creds for user username-here 2018-10-02 16:20:15,798 - Log - INFO - GetCreds - Found credentials for user username-here 2018-10-02 16:20:16,168 - _log - DEBUG - starting thread (client mode): 0xb9e0510L 2018-10-02 16:20:16,168 - _log - DEBUG - Local version/idstring: SSH-2.0-paramiko_2.4.1 2018-10-02 16:20:16,188 - _log - DEBUG - Remote version/idstring: SSH-2.0-OpenSSH_7.4 2018-10-02 16:20:16,188 - _log - INFO - Connected (version 2.0, client OpenSSH_7.4) 2018-10-02 16:20:16,192 - _log - DEBUG - kex algos:[u'gss-gex-sha1-toWM5Slw5Ew8Mqkay+al2g==', u'gss-group1-sha1-toWM5Slw5Ew8Mqkay+al2g==', u'gss-group14-sha1-toWM5Slw5Ew8Mqkay+al2g==', u'curve25519-sha256', u'[email protected]', u'ecdh-sha2-nistp256', u'ecdh-sha2-nistp384', u'ecdh-sha2-nistp521', u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group16-sha512', u'diffie-hellman-group18-sha512', u'diffie-hellman-group14-sha256', u'diffie-hellman-group14-sha1'] server key:[u'ssh-rsa', u'rsa-sha2-512', u'rsa-sha2-256', u'ecdsa-sha2-nistp256', u'ssh-ed25519'] client encrypt:[u'[email protected]', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'[email protected]', u'[email protected]'] server encrypt:[u'[email protected]', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'[email protected]', u'[email protected]'] client mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] server mac:[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] client compress:[u'none', u'[email protected]'] server compress:[u'none', u'[email protected]'] client lang:[u''] server lang:[u''] kex follows?False 2018-10-02 16:20:16,192 - _log - DEBUG - Kex agreed: ecdh-sha2-nistp256 2018-10-02 16:20:16,192 - _log - DEBUG - HostKey agreed: ssh-ed25519 2018-10-02 16:20:16,192 - _log - DEBUG - Cipher agreed: aes128-ctr 2018-10-02 16:20:16,192 - _log - DEBUG - MAC agreed: hmac-sha2-256 2018-10-02 16:20:16,192 - _log - DEBUG - Compression agreed: none 2018-10-02 16:20:16,248 - _log - DEBUG - kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256> 2018-10-02 16:20:16,249 - _log - DEBUG - Switch to new keys ... 2018-10-02 16:20:16,273 - _log - DEBUG - Adding ssh-ed25519 host key for ga-devarcmsdc19.ds.dtveng.net: c9d6111e7dac37c8c1312116d0d0af62 2018-10-02 16:20:16,274 - _log - DEBUG - Trying discovered key f5c702e13542e4ad0dcc614d8f15deeb in /Users/bc143k/.ssh/id_rsa 2018-10-02 16:20:16,304 - _log - DEBUG - userauth is OK 2018-10-02 16:20:16,363 - _log - INFO - Auth banner: S Kernel 3.10.0-862.11.6.el7.x86_64 on an x86_64 2018-10-02 16:20:16,364 - _log - INFO - Authentication (publickey) failed. 2018-10-02 16:20:16,379 - _log - DEBUG - Trying discovered key f7385028aa9a79f265ef275ab994e606 in /Users/bc143k/.ssh/id_dsa 2018-10-02 16:20:16,398 - _log - DEBUG - userauth is OK 2018-10-02 16:20:16,419 - _log - INFO - Authentication (publickey) failed. 2018-10-02 16:20:16,522 - _log - DEBUG - userauth is OK 2018-10-02 16:20:16,603 - _log - INFO - Authentication (password) successful! 2018-10-02 16:20:16,633 - _log - DEBUG - [chan 0] Max packet in: 32768 bytes 2018-10-02 16:20:17,232 - _log - DEBUG - Received global request "[email protected]" 2018-10-02 16:20:17,232 - _log - DEBUG - Rejecting "[email protected]" global request from server. 2018-10-02 16:20:17,232 - _log - DEBUG - Debug msg: Ignored authorized keys: bad ownership or modes for directory /home/bchang 2018-10-02 16:20:17,232 - _log - DEBUG - Debug msg: Ignored authorized keys: bad ownership or modes for directory /home/bchang 2018-10-02 16:20:17,233 - _log - DEBUG - [chan 0] Max packet out: 32768 bytes 2018-10-02 16:20:17,233 - _log - DEBUG - Secsh channel 0 opened. 2018-10-02 16:20:17,277 - _log - DEBUG - [chan 0] Sesch channel 0 request ok 2018-10-02 16:20:17,277 - Log - DEBUG - SSHConnect - Output - [] 2018-10-02 16:20:17,278 - Log - INFO - <module> - Finished. Duration is 0.092745 seconds. 2018-10-02 16:20:17,302 - _log - DEBUG - EOF in transport thread
Again, I'm confused on why it's not throwing any exceptions, but doesn't appear to give any kind of output. Thoughts?
Reply
#2
Updated working code below. Primary reason is stdin is spelled wrong above.

#####################################################
# IN: hostname to connect to, username to connect as, string of command(s) to run
# OUT: array of information to parse for OS; returns nothing if not able to connect

def SSHConnect(hostname, username, command):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    output = ""
    (stdin, stdout, stderr) = "", "", ""

    try:
        Log("info",  inspect.getframeinfo(inspect.currentframe()).function, "Connecting to " + hostname + " by ssh as user " + username + " to run command(s) " + command)
        #result = ssh.connect(hostname, 22, username=username, password=GetCreds(username), auth_timeout=30, look_for_keys=True)
        ssh.connect(hostname, username=username, 
                password=GetCreds(username), key_filename="full-path-of-pemfile-here") 
        stdin, stdout, stderr = ssh.exec_command(command)
        #Log("debug", inspect.getframeinfo(inspect.currentframe()).function, "stdout is " + str(stdout) + ", stderr is " + str(stderr))
        stdout = stdout.readlines()
        for line in stdout:
            output=output+line
        if output!="":
            Log("debug", inspect.getframeinfo(inspect.currentframe()).function, "Output is " + output)
        else:
            Log("err", inspect.getframeinfo(inspect.currentframe()).function, "There was no output for command " + command )
        ssh.close()
    except (socket.error,paramiko.AuthenticationException,paramiko.SSHException) as message:
        Log("err",  inspect.getframeinfo(inspect.currentframe()).function, "Error in connecting - " + message)
        pass

    finally:
        Log("debug",  inspect.getframeinfo(inspect.currentframe()).function, "Output - " + str(output) + ", stdout is " + str(stdout) + ", stderr is " + str(stderr) )
        return output
Reply
#3
Thank you for notifying and posting the solution to your issue.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020