Guest User

Untitled

a guest
Jul 28th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. from paramiko import SSHClient, AutoAddPolicy
  2. from sys import exit
  3.  
  4. def connect(host, port, username, password):
  5. ssh_client = SSHClient()
  6. ssh_client.set_missing_host_key_policy(AutoAddPolicy())
  7. ssh_client.connect(host, port=port, username=username, password=password)
  8. return ssh_client.invoke_shell('raw')
  9.  
  10. def write_line(channel, line):
  11. channel.send(line + '\n')
  12.  
  13. def read_line(channel):
  14. if not hasattr(channel, 'line_buffer'):
  15. channel.line_buffer = ""
  16. while True:
  17. end_of_line = channel.line_buffer.find('\n\r')
  18. if not end_of_line == -1:
  19. line = channel.line_buffer[0:end_of_line]
  20. channel.line_buffer = channel.line_buffer[end_of_line+2:]
  21. return line
  22. channel.line_buffer += channel.recv(4096)
  23.  
  24. def execute(channel, command):
  25. result = []
  26. write_line(channel, command)
  27. while True:
  28. line = read_line(channel)
  29. result.append(line)
  30. if line.startswith('error'):
  31. return result
  32.  
  33. if __name__ == '__main__':
  34. channel = connect('localhost', 10022, 'serveradmin', 'secret')
  35. header = read_line(channel)
  36. if not header == 'TS3':
  37. exit('Not a TS3-Server...')
  38. motd = read_line(channel)
  39.  
  40. result = execute(channel, 'whoami')
  41. print(result)
  42.  
  43. execute(channel, 'quit')
Add Comment
Please, Sign In to add comment