Guest User

Untitled

a guest
Nov 17th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import paramiko
  2. from functools import partial
  3.  
  4.  
  5. class SuperParamiko(object):
  6. """
  7. usage:
  8. >> ssh = SuperParamiko("hostname", "username")
  9. >> ssh.ls()
  10. >> ssh.git("pull")
  11. """
  12. def __init__(self, host, username, password=None, port=22):
  13. self.session = paramiko.SSHClient()
  14. self.session.set_missing_host_key_policy(
  15. paramiko.AutoAddPolicy())
  16. if password == None:
  17. self.session.connect(host, username=username, port=port)
  18. else:
  19. self.session.connect(
  20. host,
  21. username=username,
  22. password=password,
  23. port=port
  24. )
  25.  
  26. def generate_command_string(self, cmd, *args, **kwargs):
  27. command = [cmd,]
  28. command.extend(list(args))
  29. for key, arg in kwargs.items():
  30. if arg == None or arg == "":
  31. command.append("--{0}".format(key))
  32. else:
  33. command.append("--{0} {1}".format(key, arg))
  34. return ' '.join(command)
  35.  
  36. def ssh_func_wrapper(self, cmd, *args, **kwargs):
  37. command = self.generate_command_string(cmd, *args, **kwargs)
  38. stdin, stdout, stderr = self.session.exec_command(command)
  39. errors = stderr.readlines()
  40. if errors != []:
  41. raise Exception(errors)
  42. else:
  43. return map(lambda s: s.strip(), stdout.readlines())
  44.  
  45. def __getattr__(self, name):
  46. return partial(self.ssh_func_wrapper, name)
Add Comment
Please, Sign In to add comment