Guest User

Untitled

a guest
Dec 8th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< #
  3. # ssh - simple ssh2 wrapper
  4. # Author: Aaron Ghent
  5. # >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< #
  6. #
  7. # import ssh
  8. # client = ssh.Connection(host='example.com', username='root', password='default')
  9. # client.execute('ls')
  10. # client.close()
  11. #
  12. # >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< #
  13.  
  14. import os
  15. import tempfile
  16. import paramiko
  17.  
  18. class Connection(object):
  19. def __init__(self,
  20. host,
  21. username=None,
  22. private_key=None,
  23. password=None,
  24. port=22,
  25. logfile=None,
  26. ):
  27.  
  28. if not username:
  29. username = os.environ['LOGNAME']
  30.  
  31. # Log to a temporary file.
  32. if logfile:
  33. paramiko.util.log_to_file(logfile)
  34.  
  35. self._ssh = paramiko.SSHClient()
  36. self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  37. self._ssh.load_system_host_keys()
  38. self._ssh.connect(hostname=host, username=username, password=password)
  39.  
  40. # Authenticate the transport.
  41. if password:
  42. # Using Password.
  43. self._ssh.connect(hostname=host, username=username, password=password)
  44. else:
  45. # Use Private Key.
  46. if not private_key:
  47. # Try to use default key.
  48. if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')):
  49. private_key = '~/.ssh/id_rsa'
  50. elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')):
  51. private_key = '~/.ssh/id_dsa'
  52. else:
  53. raise TypeError, "You have not specified a password or key."
  54.  
  55. private_key_file = os.path.expanduser(private_key)
  56. rsa_key = paramiko.RSAKey.from_private_key_file(private_key_file)
  57. self._ssh.connect(hostname=host, username=username, pkey=rsa_key)
  58.  
  59. def get(self, remotepath, localpath = None):
  60. """
  61. Copies a file between the remote host and the local host.
  62. """
  63.  
  64. if not localpath:
  65. localpath = os.path.split(remotepath)[1]
  66.  
  67. sftp = self._ssh.open_sftp()
  68. sftp.get(remotepath, localpath)
  69.  
  70. def put(self, localpath, remotepath = None):
  71. """
  72. Copies a file between the local host and the remote host.
  73. """
  74. if not remotepath:
  75. remotepath = os.path.split(localpath)[1]
  76.  
  77. sftp = self._ssh.open_sftp()
  78. sftp.put(localpath, remotepath)
  79.  
  80. def execute(self, command):
  81. """
  82. Execute the given commands on a remote machine.
  83. """
  84. try:
  85. std_in, std_out, std_err = self._ssh.exec_command(command)
  86. except:
  87. return '[Error] execute: command failed'
  88.  
  89. if std_out.readlines():
  90. return std_out.readlines()
  91. elif std_err.readlines():
  92. return std_err.readlines()
  93. else:
  94. return
  95.  
  96. def close(self):
  97. """
  98. Closes the connection and cleans up.
  99. """
  100. self._ssh.close()
  101.  
  102. def __del__(self):
  103. """
  104. Attempt to clean up if not explicitly closed.
  105. """
  106. self.close()
  107.  
  108. # >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< #
Add Comment
Please, Sign In to add comment