Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. import os
  2. import paramiko
  3.  
  4. class ManageSSH:
  5. """Manages ssh connections."""
  6. def __init__(self):
  7. self.hosts = {"testbox": ['testbox', 'test', 'test']}
  8. self.sshConnections = {}
  9. self.sftpConnections = {}
  10. self.localfile = "C:\testfile"
  11. self.remotefile = "/tmp/tempfile"
  12. self.fetchedfile = "C:\tempdl"
  13.  
  14. def ssh(self):
  15. """Manages ssh connections."""
  16. for host in self.hosts.keys():
  17. try:
  18. self.sshConnections[host]
  19. print "ssh connection is already open for %s" % host
  20. except KeyError, e: # if no ssh connection for the host exists then open one
  21. # open ssh connection
  22. ssh = paramiko.SSHClient()
  23. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  24. ssh.connect(self.hosts[host][0], 22, self.hosts[host][1], self.hosts[host][2])
  25. self.sshConnections[host] = ssh
  26. print "ssh connection to %s opened" % host
  27. try:
  28. self.sftpConnections[host]
  29. print "sftp connection is already open for %s" % host
  30. except KeyError, e:
  31. # open sftp connection
  32. ssh = paramiko.SSHClient()
  33. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  34. ssh.connect(self.hosts[host][0], 22, self.hosts[host][1], self.hosts[host][2])
  35. self.sftpConnections[host] = ssh.open_sftp()
  36. print "sftp connection to %s opened" % host
  37.  
  38. def runCommand(self):
  39. """run commands and return output"""
  40. for host in self.hosts:
  41. command = "if [ -d /tmp ]; then echo -n 1; else echo -n 0; fi"
  42. stdin, stdout, stderr = self.sshConnections[host].exec_command(command)
  43. print "%s executed on %s" % (command, host)
  44. print "returned %s" % stdout.read()
  45. self.sftpConnections.get(self.remotefile, self.fetchedfile)
  46. print "downloaded %s from %s" % (self.remotefile, host)
  47. self.sftpConnections[host].put(self.localfile, self.remotefile)
  48. print "uploaded %s to %s" % (self.localfile, host)
  49. self.sftpConnections[host].close()
  50. self.sshConnections[host].close()
  51.  
  52. def simpleTest(self):
  53. host = "testbox"
  54. ssh = paramiko.SSHClient()
  55. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  56. ssh.connect(host, 22, 'test', 'test')
  57. sftp = ssh.open_sftp()
  58. print "sftp connection to %s opened" % host
  59. sftp.get(self.remotefile, self.fetchedfile)
  60. print "downloaded %s from %s" % (self.localfile, host)
  61. sftp.put(self.localfile, self.remotefile)
  62. print "uploaded %s to %s" % (self.localfile, host)
  63. sftp.close()
  64.  
  65. if __name__ == "__main__":
  66. test = ManageSSH()
  67. print "running test that works"
  68. test.simpleTest()
  69. print "running test that fails"
  70. test.ssh()
  71. test.runCommand()
  72.  
  73. running test that works
  74. sftp connection to testbox opened
  75. downloaded C:testfile from testbox
  76. uploaded C:testfile to testbox
  77. running test that fails
  78. ssh connection to testbox opened
  79. sftp connection to testbox opened
  80. if [ -d /tmp ]; then echo -n 1; else echo -n 0; fi executed on testbox
  81. returned 1
  82. downloaded /tmp/tempfile from testbox
  83. Traceback (most recent call last):
  84. File "paramikotest.py", line 71, in <module>
  85. test.runCommand()
  86. File "paramikotest.py", line 47, in runCommand
  87. self.sftpConnections[host].put(self.localfile, self.remotefile)
  88. File "C:Python27libsite-packagesparamikosftp_client.py", line 561, in put
  89.  
  90. fr = self.file(remotepath, 'wb')
  91. File "C:Python27libsite-packagesparamikosftp_client.py", line 245, in open
  92. t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
  93. File "C:Python27libsite-packagesparamikosftp_client.py", line 627, in _request
  94. num = self._async_request(type(None), t, *arg)
  95. File "C:Python27libsite-packagesparamikosftp_client.py", line 649, in _async_request
  96. self._send_packet(t, str(msg))
  97. File "C:Python27libsite-packagesparamikosftp.py", line 172, in _send_packet
  98. self._write_all(out)
  99. File "C:Python27libsite-packagesparamikosftp.py", line 138, in _write_all
  100.  
  101. raise EOFError()
  102. EOFError
  103.  
  104. t = paramiko.Transport((host, 22))
  105. t.connect(username=username, password=password)
  106. sftp = paramiko.SFTPClient.from_transport(t)
  107.  
  108. stdin, stdout, stderr = self.sshConnections[host].exec_command(command)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement