Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import threading, paramiko
  2. import time
  3. import sys
  4. #==========================
  5. command = sys.argv[1]
  6. command2 = sys.argv[2]
  7. command3 = sys.argv[3]
  8. command4 = sys.argv[4]
  9. command5 = sys.argv[5]
  10. command6 = sys.argv[6]
  11. command7 = sys.argv[7]
  12. command8 = sys.argv[8]
  13. command9 = sys.argv[9]
  14. command10 = sys.argv[10]
  15. #==========================
  16. print("<html><head>")
  17. print("</head><body>")
  18.  
  19. class ssh:
  20. shell = None
  21. client = None
  22. transport = None
  23.  
  24. def __init__(self, address, username, password):
  25. # print(("Connecting to server on ip", str(address) + "."))
  26. self.client = paramiko.client.SSHClient()
  27. self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
  28. self.client.connect(address, username=username, password=password, look_for_keys=False)
  29. self.transport = paramiko.Transport((address, 22))
  30. self.transport.connect(username=username, password=password)
  31.  
  32. thread = threading.Thread(target=self.process)
  33. thread.daemon = True
  34. thread.start()
  35.  
  36. def close_connection(self):
  37. if(self.client != None):
  38. self.client.close()
  39. self.transport.close()
  40.  
  41. def open_shell(self):
  42. self.shell = self.client.invoke_shell()
  43.  
  44. def send_shell(self, command):
  45. if(self.shell):
  46. self.shell.send(command + "n")
  47. else:
  48. print("<h1>Shell não aberta.</h1>")
  49.  
  50. def process(self):
  51. global strdata, fulldata
  52. while True:
  53. # Print data when available
  54. if self.shell is not None and self.shell.recv_ready():
  55. alldata = self.shell.recv(1024)
  56. while self.shell.recv_ready():
  57. alldata += self.shell.recv(1024)
  58. strdata = strdata + str(alldata)
  59. fulldata = fulldata + alldata.decode("UTF-8")
  60. strdata = self.print_lines(strdata) # print all received data except last line
  61.  
  62. def print_lines(self, data):
  63. last_line = data
  64. if 'n' in data:
  65. lines = data.splitlines()
  66. for i in range(0, len(lines)-1):
  67. print((lines[i]))
  68. last_line = lines[len(lines) - 1]
  69. if data.endswith('n'):
  70. print(lines.split(last_line))
  71. last_line = ''
  72. return last_line
  73.  
  74. sshUsername = "admin"
  75. sshPassword = "password"
  76. sshServer = "192.168.40.169"
  77.  
  78. connection = ssh(sshServer, sshUsername, sshPassword)
  79. connection.open_shell()
  80. connection.send_shell(command)
  81. connection.send_shell(command2)
  82. connection.send_shell(command3)
  83. connection.send_shell(command4)
  84. connection.send_shell(command5)
  85. connection.send_shell(command6)
  86. connection.send_shell(command7)
  87. connection.send_shell(command8)
  88. connection.send_shell(command9)
  89. connection.send_shell(command10)
  90. time.sleep(1)
  91. print(fulldata) # This contains the complete data received.
  92. connection.close_connection()
  93.  
  94. print("</body></html>")
  95.  
  96. R5#en
  97. R%#conf t
  98. Enter configuration commands, one per line. End with CNTL/Z.
  99. R5(config)#hostname R5
  100. R5(config)#
  101.  
  102. R5#en R5#conf t Enter configuration commands, one per line. End with CNTL/Z. R5(config)#hostname R5 R5(config)#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement