ar08 commited on
Commit
debd19e
·
verified ·
1 Parent(s): cc81665

Create ssh.py

Browse files
Files changed (1) hide show
  1. ssh.py +68 -0
ssh.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import socket
2
+ import paramiko
3
+
4
+ # Define host and port
5
+ host_key = paramiko.RSAKey(filename='test_rsa.key')
6
+ host, port = 'localhost', 2222
7
+
8
+ # Create SSH server class
9
+ class SSHServer(paramiko.ServerInterface):
10
+ def __init__(self):
11
+ self.event = threading.Event()
12
+
13
+ def check_channel_request(self, kind, chanid):
14
+ if kind == 'session':
15
+ return paramiko.OPEN_SUCCEEDED
16
+ return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
17
+
18
+ def check_auth_password(self, username, password):
19
+ if username == 'test' and password == 'password':
20
+ return paramiko.AUTH_SUCCESSFUL
21
+ return paramiko.AUTH_FAILED
22
+
23
+ # Main function to start the server
24
+ def start_server():
25
+ server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
26
+ server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
27
+ server.bind((host, port))
28
+ server.listen(100)
29
+
30
+ print(f'[*] Listening for connection on {host}:{port}...')
31
+
32
+ while True:
33
+ client, addr = server.accept()
34
+ print(f'[*] Accepted connection from {addr[0]}:{addr[1]}')
35
+
36
+ try:
37
+ transport = paramiko.Transport(client)
38
+ transport.add_server_key(host_key)
39
+ server = SSHServer()
40
+
41
+ transport.start_server(server=server)
42
+
43
+ chan = transport.accept(20)
44
+ if chan is None:
45
+ print('*** No channel.')
46
+ continue
47
+
48
+ print(f'[*] Authenticated!')
49
+ chan.send('Welcome to my SSH server!')
50
+
51
+ while True:
52
+ command = chan.recv(1024)
53
+ if not command:
54
+ break
55
+ print(f'[*] Received command: {command.decode()}')
56
+ chan.send(command)
57
+
58
+ transport.close()
59
+
60
+ except Exception as e:
61
+ print(f'[-] Caught exception: {e.__class__.__name__}: {e}')
62
+ try:
63
+ transport.close()
64
+ except:
65
+ pass
66
+
67
+ if __name__ == '__main__':
68
+ start_server()