Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. class myssh(paramiko.SSHClient):
  2.  
  3. @classmethod
  4. @contextmanager
  5. def connect(cls, credentials, password=None):
  6. username, host = credentials.split('@')
  7. ssh = cls()
  8. try:
  9. super(SSH, ssh).connect(host, username=username, password=password)
  10. yield ssh
  11. finally:
  12. ssh.close()
  13.  
  14. with myssh.connect(user_machine, password=password) as ssh:
  15. stdin, stdout, stderr = ssh.exec_command(my_command)
  16.  
  17. return_code = stdout.channel.recv_exit_status()
  18.  
  19. out = stdout.read().decode('utf-8')
  20. err = stderr.read().decode('utf-8')
  21.  
  22. if out.strip():
  23. print(out.strip())
  24. if err.strip():
  25. print(err.strip())
  26.  
  27. Exception ignored in: <object repr() failed>
  28. Traceback (most recent call last):
  29. File "/home/baralv/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 61, in __del__
  30. File "/home/baralv/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 79, in close
  31. File "/home/baralv/.miniconda2/envs/py3/lib/python3.5/site-packages/paramiko/file.py", line 88, in flush
  32. TypeError: 'NoneType' object is not callable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement