Guest User

Untitled

a guest
Nov 21st, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import SSH
  2.  
  3. connection = ssh("10.10.65.100", "gerrit2", "gerrit@123")
  4. print("Calling OpenShell")
  5. connection.openShell()
  6. print("Calling sendShell")
  7. connection.sendShell("ls -l")
  8. print("Calling process")
  9. connection.process()
  10. print("Calling closeConnection")
  11. connection.closeConnection()
  12.  
  13. import threading, paramiko
  14.  
  15. class ssh:
  16. shell = None
  17. client = None
  18. transport = None
  19.  
  20. def __init__(self, address, username, password):
  21. print("Connecting to server on ip", str(address) + ".")
  22. self.client = paramiko.client.SSHClient()
  23. self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
  24. self.client.connect(address, username=username, password=password, look_for_keys=False)
  25. self.transport = paramiko.Transport((address, 22))
  26. self.transport.connect(username=username, password=password)
  27.  
  28. thread = threading.Thread(target=self.process)
  29. thread.daemon = True
  30. thread.start()
  31.  
  32. def closeConnection(self):
  33. if(self.client != None):
  34. self.client.close()
  35. self.transport.close()
  36.  
  37. def openShell(self):
  38. self.shell = self.client.invoke_shell()
  39.  
  40. def sendShell(self, command):
  41. if(self.shell):
  42. self.shell.send(command + "n")
  43. else:
  44. print("Shell not opened.")
  45.  
  46. def process(self):
  47. global connection
  48. while True:
  49. # Print data when available
  50. if self.shell != None and self.shell.recv_ready():
  51. alldata = self.shell.recv(1024)
  52. while self.shell.recv_ready():
  53. alldata += self.shell.recv(1024)
  54. strdata = str(alldata, "utf8")
  55. strdata.replace('r', '')
  56. print(strdata, end = "")
  57. if(strdata.endswith("$ ")):
  58. print("n$ ", end = "")
  59.  
  60. > python automate.py
  61.  
  62. Traceback (most recent call last):
  63. File "automate.py", line 1, in <module>
  64. import SSH
  65. File "D:AutomateSSH_ParamikoSSH.py", line 1, in <module>
  66. import threading, paramiko
  67. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesparamiko-2.4.0-py3.7.eggparamiko__init__.py", line 22, in <module>
  68. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesparamiko-2.4.0-py3.7.eggparamikotransport.py", line 57, in <module>
  69. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesparamiko-2.4.0-py3.7.eggparamikoed25519key.py", line 22, in <module>
  70. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesnaclsigning.py", line 19, in <module>
  71. import nacl.bindings
  72. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesnaclbindings__init__.py", line 17, in <module>
  73. from nacl.bindings.crypto_box import (
  74. File "D:Usersprashant-guAppDataLocalProgramsPythonPython37libsite-packagesnaclbindingscrypto_box.py", line 18, in <module>
  75. from nacl._sodium import ffi, lib
  76. ImportError: DLL load failed: The specified module could not be found.
Add Comment
Please, Sign In to add comment