-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_gateway.py
More file actions
71 lines (61 loc) · 1.89 KB
/
ssh_gateway.py
File metadata and controls
71 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import socket
import paramiko
import threading
import sys
host_key = paramiko.RSAKey(filename='test_rsa.key')
class Server(paramiko.ServerInterface):
def __init__(self):
self.event = threading.Event()
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):
print('check pw')
if username == 'laurijssen' and password == 'test':
return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED
server = sys.argv[1]
ssh_port = int(sys.argv[2])
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((server, ssh_port))
server_socket.listen(20)
client, addr = server_socket.accept()
except Exception as e:
print('listen failed: ' + str(e))
sys.exit(1)
print('connection success')
try:
session = paramiko.Transport(client)
session.add_server_key(host_key)
server = Server()
try:
session.start_server(server=server)
except paramiko.SSHException as x:
print('ssh nego failed')
chan = session.accept(20)
print('authenticated')
print(chan.recv(1024))
chan.send('connected ssh')
while True:
try:
cmd = input('enter: ').strip('\n')
if cmd != 'exit':
chan.send(cmd)
print(chan.recv(1024).decode('ascii') + '\n')
else:
chan.send('exit')
print('exit')
session.close()
raise Exception('exit')
except KeyboardInterrupt:
session.close()
except Exception as e:
print('caught ' + str(e))
try:
session.close()
except:
pass
sys.exit(1)