Oct-02-2018, 11:44 PM
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?
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
##################################################### # 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 |
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'curve25519-sha256@libssh.org', 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'chacha20-poly1305@openssh.com', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-gcm@openssh.com', u'aes256-gcm@openssh.com'] server encrypt:[u'chacha20-poly1305@openssh.com', u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'aes128-gcm@openssh.com', u'aes256-gcm@openssh.com'] client mac:[u'umac-64-etm@openssh.com', u'umac-128-etm@openssh.com', u'hmac-sha2-256-etm@openssh.com', u'hmac-sha2-512-etm@openssh.com', u'hmac-sha1-etm@openssh.com', u'umac-64@openssh.com', u'umac-128@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] server mac:[u'umac-64-etm@openssh.com', u'umac-128-etm@openssh.com', u'hmac-sha2-256-etm@openssh.com', u'hmac-sha2-512-etm@openssh.com', u'hmac-sha1-etm@openssh.com', u'umac-64@openssh.com', u'umac-128@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-sha1'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] 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 "hostkeys-00@openssh.com"
2018-10-02 16:20:17,232 - _log - DEBUG - Rejecting "hostkeys-00@openssh.com" 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?