Guest User

Untitled

a guest
Jan 16th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. except (IOError, SystemExit):
  2. raise
  3. except KeyboardInterrupt:
  4. print ("Crtl+C Pressed. Shutting down.")
  5.  
  6. import paramiko
  7. import time
  8. import re
  9. import socket
  10. import os
  11.  
  12. ssh = paramiko.SSHClient()
  13. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  14.  
  15. address = '192.168.1.1'
  16. username = 'admin'
  17. password = ''
  18.  
  19. ssh.connect(address, port=22, username = username, password = password)
  20.  
  21. shell = ssh.invoke_shell(height=400)
  22. shell.settimeout(2)
  23.  
  24. def prompt_console(command = '', channel = shell, timeout = False):
  25. print(channel.recv(9999).decode('utf-8')) #<-- this is for removing the initial prompt from buffer
  26. if timeout == False:
  27. shell.settimeout(3600) #<--increasing the timeout
  28. channel.send(command+'n')
  29. log = ''
  30. while True:
  31. try:
  32. log += channel.recv(1).decode('utf-8') #<--every loop it loads a char into the "log" variable
  33. if log[-9:] == '--More-- ':
  34. shell.send(" ")
  35. log.replace('--More-- r rtt','rtt')
  36. if len(re.findall('.*n',log))>0: #<--it yields every line to python console
  37. yield log
  38. log = ''
  39. except (IOError, SystemExit):
  40. raise
  41. except KeyboardInterrupt:
  42. print ("Crtl+C Pressed. Shutting down.")
Add Comment
Please, Sign In to add comment