Guest User

Untitled

a guest
Apr 16th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import paramiko
  2. import time
  3. import re
  4.  
  5. bastion_ip='ip'
  6. bastion_pass='pass'
  7.  
  8. ssh = paramiko.SSHClient()
  9. ssh.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
  10. ssh.connect(bastion_ip, username='root', password=bastion_pass)
  11.  
  12. chan = ssh.invoke_shell()
  13.  
  14. # other cloud server
  15. priv_ip='ip'
  16. passw='pass'
  17.  
  18. test_script='/root/check_rackconnect.sh'
  19.  
  20. def run_cmd(cmd):
  21. buff = ''
  22. while not buff.endswith(':~# '):
  23. resp = chan.recv(9999)
  24. buff += resp
  25. print(resp)
  26.  
  27. # Ssh and wait for the password prompt.
  28. chan.send(cmd + '\n')
  29.  
  30. buff = ''
  31. while not buff.endswith('\'s password: '):
  32. resp = chan.recv(9999)
  33. buff += resp
  34. print(resp)
  35.  
  36. # Send the password and wait for a prompt.
  37. time.sleep(3)
  38. chan.send(passw + '\n')
  39.  
  40. buff = ''
  41. while buff.find(' done.') < 0 :
  42. resp = chan.recv(9999)
  43. buff += resp
  44. print(resp)
  45.  
  46. ret=re.search( '(\d) done.', buff).group(1)
  47. ssh.close()
  48.  
  49. print('command was successful:' + str(ret=='0'))
  50.  
  51. scp_opt=""
  52. cmd='scp -q ' + scp_opt + ' -o NumberOfPasswordPrompts=1 -o StrictHostKeyChecking=no %s root@%s:~/; echo $? done.' % ( test_script, priv_ip )
  53. print('\n test 2\n cmd %s\n' % cmd)
  54. run_cmd(cmd)
Add Comment
Please, Sign In to add comment