Guest User

Untitled

a guest
Sep 1st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #! python
  2.  
  3. import sys
  4. import time
  5. import socket
  6. import logging
  7. import paramiko
  8. import threading
  9.  
  10. timeformat = '%y/%m/%d %H:%M:%S'
  11. logformat = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
  12. log_handler = logging.StreamHandler(sys.stdout)
  13. log_handler.setFormatter(logging.Formatter(logformat, timeformat))
  14. log = logging.getLogger('paramiko')
  15. log.addHandler(log_handler)
  16. log.setLevel(logging.DEBUG)
  17.  
  18. socket_handle = socket.socket()
  19. socket_handle.bind(('', 1830))
  20. socket_handle.listen(10)
  21. log.info("Waiting for New connection")
  22. connection, address = socket_handle.accept()
  23. log.info("New connection from " + str(address))
  24.  
  25. class netconf_t(paramiko.server.SubsystemHandler) :
  26.  
  27. def start_subsystem(self, name, transport, channel):
  28. log.info("netconf_t.start_subsystem")
  29. while transport.is_active() :
  30. if channel.recv_ready() :
  31. channel.recv(32*1024)
  32.  
  33. def finish_subsystem(self):
  34. log.info("netconf_t.finish_subsystem")
  35.  
  36. def __del__(self) :
  37. log.info("The object is being destructed")
  38.  
  39. class ssh_server(paramiko.ServerInterface):
  40.  
  41. def __init__(self):
  42. log.info("ssh_server.__init__()")
  43.  
  44. def check_channel_request(self, kind, chanid):
  45. log.info("ssh_server.check_channel_request(kind=%s, chanid=%s)", kind, chanid)
  46. if kind == 'session':
  47. return paramiko.OPEN_SUCCEEDED
  48. return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  49.  
  50. def check_auth_password(self, username, password):
  51. log.info("ssh_server.check_auth_password(username=%s, password=%s)", username, password)
  52. return paramiko.AUTH_SUCCESSFUL
  53.  
  54. def check_auth_publickey(self, username, key):
  55. log.info("ssh_server.check_auth_publickey()")
  56. log.critical('Public key authentication is NOT supported')
  57. return paramiko.AUTH_FAILED
  58.  
  59. def get_allowed_auths(self, username):
  60. log.info("ssh_server.get_allowed_auths(username=%s)", username)
  61. return 'password'
  62.  
  63. def check_channel_shell_request(self, channel):
  64. log.info("ssh_server.check_channel_shell_request()")
  65. log.critical('SHELL request is NOT supported')
  66. return False
  67.  
  68. def check_channel_exec_request(self, channel, command):
  69. log.info("ssh_server.check_channel_exec_request()")
  70. log.critical('EXEC request is NOT supported')
  71. return False
  72.  
  73. def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, modes):
  74. log.info("ssh_server.check_channel_pty_request(term=%s, width=%d, height=%d)", term, width, height)
  75. log.critical('PTY request is NOT supported')
  76. return False
  77.  
  78. def check_channel_subsystem_request(self, channel, name):
  79. log.info("ssh_server.check_channel_subsystem_request(name=%s)", name)
  80. netconf = netconf_t(channel, name, self)
  81. netconf.start()
  82. # netconf.join()
  83. return True
  84.  
  85. def __del__(self):
  86. log.info("ssh_server is destructed")
  87.  
  88. host_key = paramiko.RSAKey.generate(2048)
  89. transport = paramiko.Transport(connection)
  90. # transport.set_subsystem_handler('netconf', netconf_t)
  91. transport.add_server_key(host_key)
  92. transport.start_server(server=ssh_server())
  93. transport.join()
  94. log.info('server execution is done')
  95.  
  96. 18/09/01 00:48:16 INFO ssh_server.py:79 ssh_server.check_channel_subsystem_request(name=netconf)
  97. 18/09/01 00:48:16 DEBUG transport.py:1687 Starting handler for subsystem netconf
  98. 18/09/01 00:48:16 INFO ssh_server.py:28 netconf_t.start_subsystem
  99. 18/09/01 00:48:36 DEBUG channel.py:1184 [chan 0] EOF sent (0)
  100. 18/09/01 00:48:37 DEBUG transport.py:1687 EOF in transport thread
  101. 18/09/01 00:48:37 INFO ssh_server.py:34 netconf_t.finish_subsystem
  102. 18/09/01 00:48:37 INFO ssh_server.py:94 server execution is done
  103. 18/09/01 00:48:37 INFO ssh_server.py:37 The object is being destructed
Add Comment
Please, Sign In to add comment