Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. from paramiko import SSHClient, AutoAddPolicy
  2. from paramiko.ssh_exception import AuthenticationException
  3. from getpass import getpass
  4.  
  5.  
  6. class SshClient(object):
  7. '''
  8. The Sshclient class wraps some basic
  9. paramiko.SSHClient functionality
  10.  
  11. Attributes:
  12. host(str): remote host name
  13. user(str): login user name
  14. password(str): login user password
  15. connection(object): the wrapped paramiko.SSHClient
  16. object or None if the connection is not successful
  17. '''
  18.  
  19.  
  20. def __init__(self, host, user, pword):
  21. '''
  22. The class constructor.
  23. self.connection is intialized to a paramiko.SSHClient
  24. instance or None if the connection is not successful.
  25.  
  26. Args:
  27. host(str): remote host name
  28. user(str): login user name
  29. pword(str): login user password
  30. '''
  31. self.host = host
  32. self.user = user
  33. self.password = pword
  34. self.connection = None
  35.  
  36. try:
  37. self.connection = SSHClient()
  38. self.connection.set_missing_host_key_policy(AutoAddPolicy())
  39. self.connection.connect(hostname=host, username=user, password=pword)
  40.  
  41. except AuthenticationException as exception:
  42. print(exception.args)
  43. self.connection.close()
  44. self.connection = None
  45.  
  46.  
  47. def execute(self, cmd):
  48. '''
  49. Executes a command on the remote host.
  50.  
  51. Args:
  52. cmd(str): the command to be executed
  53.  
  54. Returns:
  55. a dictionary containing the out,
  56. err and retval keys.
  57. '''
  58.  
  59. stdin, stdout, stderr = self.connection.exec_command(cmd)
  60.  
  61. return {'out': stdout.readlines(),
  62. 'err' : stderr.readlines(),
  63. 'retval': stdout.channel.recv_exit_status()}
  64.  
  65.  
  66. def close(self):
  67. '''
  68. Closes the connection.
  69. '''
  70.  
  71. if not self.connection is None:
  72. self.connection.close()
  73. self.connection = None
  74.  
  75.  
  76.  
  77. if __name__ == '__main__':
  78.  
  79. host = input('Remote host name: ')
  80. user = input('User name: ')
  81. pword = getpass()
  82.  
  83. # connects to the remote client
  84. # using the given credentials
  85.  
  86. try:
  87. client = SshClient(host, user, pword)
  88. except exception:
  89. print(exception.args)
  90.  
  91. # if the connection is successful
  92. # create a directory on the remote host
  93. # if it does not exist
  94.  
  95. if not client.connection is None:
  96.  
  97. dir_name = 'my-files'
  98. list_dirs = 'ls'
  99. mkdir = 'mkdir ' + dir_name
  100. msg_dir_exists = 'directory ' + dir_name + ' exists'
  101.  
  102. ret = client.execute(list_dirs)
  103.  
  104. success = ret['retval'] == 0
  105. dir_exists = any(dir_name in s for s in ret['out'])
  106.  
  107. if success and not dir_exists:
  108. ret = client.execute(mkdir)
  109. print(ret['retval'])
  110. else:
  111. print(msg_dir_exists)
  112.  
  113. client.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement