Advertisement
Guest User

Untitled

a guest
Sep 18th, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. paramiko.SSHClient().connect(host, timeout=10, username=user,
  2. key_filename=seckey, compress=True)
  3.  
  4. self._pkey = paramiko.RSAKey.from_private_key_file(server.ssh_key_file,
  5. password=ssh_pwd)
  6.  
  7. Authentication is attempted in the following order of priority:
  8. - The C{pkey} or C{key_filename} passed in (if any)
  9. - Any key we can find through an SSH agent
  10. - Any "id_rsa" or "id_dsa" key discoverable in C{~/.ssh/}
  11. - Plain username/password auth, if a password was given
  12.  
  13. def SshCommand(**kwargs):
  14. '''
  15. Run a command on a remote host via SSH.
  16.  
  17. Connect to the given host=<host-or-ip>, as user=<user> (defaulting to
  18. $USER), with optional seckey=<secret-key-file>, timeout=<seconds>
  19. (default 10), and execute a single command=<command> (assumed to be
  20. addressing a unix shell at the far end.
  21.  
  22. Returns the exit status of the remote command (otherwise would be
  23. None save that an exception should be raised instead).
  24.  
  25. Example: SshCommand(host=host, user=user, command=command, timeout=timeout,
  26. seckey=seckey)
  27. '''
  28. remote_exit_status = None
  29.  
  30. if debug:
  31. sys.stderr.write('SshCommand kwargs: %rn' % (kwargs,))
  32.  
  33. paranoid = True
  34.  
  35. host = kwargs['host']
  36. user = kwargs['user'] if kwargs['user'] else os.environ['USER']
  37. seckey = kwargs['seckey']
  38. timeout = kwargs['timeout']
  39. command = kwargs['command']
  40.  
  41. ssh = paramiko.SSHClient()
  42. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  43.  
  44. time_end = time.time() + int(timeout)
  45.  
  46. ssh_is_up = False
  47. while time.time() < time_end:
  48. try:
  49. ssh.connect(host, timeout=10, username=user, key_filename=seckey,
  50. compress=True)
  51. if paranoid:
  52. token_generator = 'echo xyz | tr a-z A-Z'
  53. token_result = 'XYZ' # possibly buried in other text
  54. stdin, stdout, stderr = ssh.exec_command(token_generator)
  55. lines = ''.join(stdout.readlines())
  56. if re.search(token_result, lines):
  57. ssh_is_up = True
  58. if debug:
  59. sys.stderr.write("[%d] command stream is UP!n"
  60. % time.time())
  61. break
  62. else:
  63. ssh_is_up = True
  64. break
  65. except paramiko.PasswordRequiredException as e:
  66. sys.stderr.write("usage idiom clash: %rn" % (e,))
  67. return False
  68. except Exception as e:
  69. sys.stderr.write("[%d] command stream not yet availablen"
  70. % time.time())
  71. if debug:
  72. sys.stderr.write("exception is %rn" % (e,))
  73. time.sleep(1)
  74.  
  75. if ssh_is_up:
  76. # ideally this is where Bcfg2 or Chef or such ilk get called.
  77. # stdin, stdout, stderr = ssh.exec_command(command)
  78. chan = ssh._transport.open_session()
  79. chan.exec_command(command)
  80. # note that out/err doesn't have inter-stream ordering locked down.
  81. stdout = chan.makefile('rb', -1)
  82. stderr = chan.makefile_stderr('rb', -1)
  83. sys.stdout.write(''.join(stdout.readlines()))
  84. sys.stderr.write(''.join(stderr.readlines()))
  85. remote_exit_status = chan.recv_exit_status()
  86. if debug:
  87. sys.stderr.write('exit status was: %dn' % remote_exit_status)
  88.  
  89. ssh.close()
  90. if None == remote_exit_status:
  91. raise SSHException('remote command result undefined')
  92. return remote_exit_status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement