Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python
- import sys
- import time
- import socket
- import logging
- import paramiko
- import threading
- timeformat = '%y/%m/%d %H:%M:%S'
- logformat = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
- log_handler = logging.StreamHandler(sys.stdout)
- log_handler.setFormatter(logging.Formatter(logformat, timeformat))
- log = logging.getLogger('paramiko')
- log.addHandler(log_handler)
- log.setLevel(logging.DEBUG)
- socket_handle = socket.socket()
- socket_handle.bind(('', 1830))
- socket_handle.listen(10)
- log.info("Waiting for New connection")
- connection, address = socket_handle.accept()
- log.info("New connection from " + str(address))
- class netconf_t(paramiko.server.SubsystemHandler) :
- def start_subsystem(self, name, transport, channel):
- log.info("netconf_t.start_subsystem")
- while transport.is_active() :
- if channel.recv_ready() :
- channel.recv(32*1024)
- def finish_subsystem(self):
- log.info("netconf_t.finish_subsystem")
- def __del__(self) :
- log.info("The object is being destructed")
- class ssh_server(paramiko.ServerInterface):
- def __init__(self):
- log.info("ssh_server.__init__()")
- def check_channel_request(self, kind, chanid):
- log.info("ssh_server.check_channel_request(kind=%s, chanid=%s)", kind, chanid)
- if kind == 'session':
- return paramiko.OPEN_SUCCEEDED
- return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
- def check_auth_password(self, username, password):
- log.info("ssh_server.check_auth_password(username=%s, password=%s)", username, password)
- return paramiko.AUTH_SUCCESSFUL
- def check_auth_publickey(self, username, key):
- log.info("ssh_server.check_auth_publickey()")
- log.critical('Public key authentication is NOT supported')
- return paramiko.AUTH_FAILED
- def get_allowed_auths(self, username):
- log.info("ssh_server.get_allowed_auths(username=%s)", username)
- return 'password'
- def check_channel_shell_request(self, channel):
- log.info("ssh_server.check_channel_shell_request()")
- log.critical('SHELL request is NOT supported')
- return False
- def check_channel_exec_request(self, channel, command):
- log.info("ssh_server.check_channel_exec_request()")
- log.critical('EXEC request is NOT supported')
- return False
- def check_channel_pty_request(self, channel, term, width, height, pixelwidth, pixelheight, modes):
- log.info("ssh_server.check_channel_pty_request(term=%s, width=%d, height=%d)", term, width, height)
- log.critical('PTY request is NOT supported')
- return False
- def check_channel_subsystem_request(self, channel, name):
- log.info("ssh_server.check_channel_subsystem_request(name=%s)", name)
- netconf = netconf_t(channel, name, self)
- netconf.start()
- # netconf.join()
- return True
- def __del__(self):
- log.info("ssh_server is destructed")
- host_key = paramiko.RSAKey.generate(2048)
- transport = paramiko.Transport(connection)
- # transport.set_subsystem_handler('netconf', netconf_t)
- transport.add_server_key(host_key)
- transport.start_server(server=ssh_server())
- transport.join()
- log.info('server execution is done')
- 18/09/01 00:48:16 INFO ssh_server.py:79 ssh_server.check_channel_subsystem_request(name=netconf)
- 18/09/01 00:48:16 DEBUG transport.py:1687 Starting handler for subsystem netconf
- 18/09/01 00:48:16 INFO ssh_server.py:28 netconf_t.start_subsystem
- 18/09/01 00:48:36 DEBUG channel.py:1184 [chan 0] EOF sent (0)
- 18/09/01 00:48:37 DEBUG transport.py:1687 EOF in transport thread
- 18/09/01 00:48:37 INFO ssh_server.py:34 netconf_t.finish_subsystem
- 18/09/01 00:48:37 INFO ssh_server.py:94 server execution is done
- 18/09/01 00:48:37 INFO ssh_server.py:37 The object is being destructed
Add Comment
Please, Sign In to add comment