Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. def sendCommandViaSSH(usr,psswrd,command):
  2. line = 'ssh '+ usr +'@'+ RIp + ' ' +"""+ command+"""
  3. print_message(line)
  4. child = pexpect.spawn (line)
  5. #i = child.expect('Are you sure you want to continue connecting (yes/no)?')
  6. i = child.expect (['Are you sure you want to continue connecting (yes/no)?', pexpect.EOF, pexpect.TIMEOUT],2)
  7. print_message(i)
  8. if i == 0:
  9. child.sendline ('yes')
  10. i = child.expect(['s password:', pexpect.EOF, pexpect.TIMEOUT])
  11. print_message(i)
  12. if i == 0:
  13. print_message(psswrd)
  14. child.sendline (psswrd)
  15.  
  16. ssh MyUsr@XXX.XXX.XXX.XXX "shutdown -k now"
  17. 2
  18. 0
  19. MyPsswrd
  20.  
  21. 1.code that i wrote so far
  22. 2.output that I get using it (while encountering the same problem of not working commands )
  23.  
  24. import paramiko
  25. import logging
  26. logging.basicConfig(level=logging.DEBUG)
  27.  
  28. def exec_paramiko(ssh, command):
  29. print_message("executing '{0}' on remote machine".format(command))
  30. chan = ssh.get_transport().open_session()
  31. chan.get_pty()
  32. f = chan.makefile()
  33. chan.exec_command(command)
  34. flag=True
  35. exit_code=None
  36. output=""
  37. while True:
  38. if chan.recv_ready():
  39. output+=(chan.recv(4096).decode('ascii') + "n")
  40. if chan.recv_stderr_ready():
  41. output+=("error: {0}".format(chan.recv_stderr(4096).decode('ascii')))
  42. if chan.exit_status_ready():
  43. output+=(chan.recv(4096).decode('ascii') + "n")
  44. error_string=0
  45. if chan.recv_stderr_ready():
  46. error_string=chan.recv_stderr(4096).decode('ascii')
  47. exit_code = chan.recv_exit_status()
  48. if exit_code != 0:
  49. output+="nerror: {0}".format(error_string)
  50. break
  51. return exit_code, output
  52.  
  53. ssh = paramiko.SSHClient()
  54. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  55. ssh.connect(hostname="10.xx.xx.xx",username="usr",password="psswrd",allow_agent=False,look_for_keys=False, timeout=20)
  56. exit_code, output = exec_paramiko(ssh,'shutdown -k now' )
  57. print "error code: {0} message: {1}".format(exit_code, output)
  58.  
  59. DEBUG:paramiko.transport:starting thread (client mode): 0xdb17c310L
  60. DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_1.17.1
  61. DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_5.3
  62. INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)
  63. DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] 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
  64. DEBUG:paramiko.transport:Kex agreed: diffie-hellman-group1-sha1
  65. DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
  66. DEBUG:paramiko.transport:MAC agreed: hmac-md5
  67. DEBUG:paramiko.transport:Compression agreed: none
  68. DEBUG:paramiko.transport:kex engine KexGroup1 specified hash_algo <built-in function openssl_sha1>
  69. DEBUG:paramiko.transport:Switch to new keys ...
  70. DEBUG:paramiko.transport:Adding ssh-rsa host key for xx.xx.xx.xx: 0c13035c7e4c0e3999d1c85215e97394
  71. DEBUG:paramiko.transport:userauth is OK
  72. INFO:paramiko.transport:Authentication (password) successful!
  73. DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
  74. DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
  75. DEBUG:paramiko.transport:Secsh channel 0 opened.
  76. DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
  77. INFO:paramiko.transport.sftp:[chan 0] Opened sftp connection (server version 3)
  78. shutting down
  79. executing 'shutdown -k now' on remote machine
  80. DEBUG:paramiko.transport:[chan 1] Max packet in: 32768 bytes
  81. DEBUG:paramiko.transport:[chan 1] Max packet out: 32768 bytes
  82. DEBUG:paramiko.transport:Secsh channel 1 opened.
  83. DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok
  84. DEBUG:paramiko.transport:[chan 1] Sesch channel 1 request ok
  85. DEBUG:paramiko.transport:[chan 1] EOF received (1)
  86. DEBUG:paramiko.transport:[chan 1] EOF sent (1)
  87. error code: 0 message:
  88.  
  89. DEBUG:paramiko.transport:EOF in transport thread
  90.  
  91. sshpass -pfoobar ssh -o StrictHostKeyChecking=no user@host command_to_run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement