Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import time
  2.  
  3. import paramiko
  4.  
  5.  
  6. class SomeSSHThing(object):
  7. def __init__(self, ip, username, password):
  8. self._ip = ip
  9. self._username = username
  10. self._password = password
  11.  
  12. self._connection = paramiko.SSHClient()
  13. self._connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  14.  
  15. self._channel = None
  16.  
  17. def connect(self):
  18. self._connection.connect(
  19. hostname=self._ip,
  20. username=self._username,
  21. password=self._password,
  22. )
  23.  
  24. self._channel = self._connection.invoke_shell()
  25.  
  26. def send(self, data):
  27. self._channel.send(data)
  28.  
  29. def recv(self, length=16348):
  30. return self._channel.recv(length)
  31.  
  32. def read_until(self, delimiter):
  33. data = ''
  34. while delimiter not in data:
  35. data += self.recv(1)
  36.  
  37. return data
  38.  
  39. def disconnect(self):
  40. self._channel.close()
  41. self._connection.close()
  42.  
  43.  
  44. if __name__ == '__main__':
  45. s = SomeSSHThing(
  46. ip='hostname.domain',
  47. username='user',
  48. password='password',
  49. )
  50.  
  51. s.connect()
  52.  
  53. s.read_until('$')
  54.  
  55. s.send('ifconfig\r\n')
  56.  
  57. print s.read_until('$')
  58.  
  59. s.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement