-
Notifications
You must be signed in to change notification settings - Fork 0
/
pysshmux.py
executable file
·181 lines (154 loc) · 5.09 KB
/
pysshmux.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python2
import os
import sys
import socket
import struct
import SshMuxClient
if __name__ == '__main__':
try:
muxclient = SshMuxClient.SshMuxClient(sys.argv[1])
except:
print >>sys.stderr, 'Can\'t create mux client.'
sys.exit(1)
res, val = muxclient.connect()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'Successfully connected to mux master: %s' % (sys.argv[1],)
for name in val:
print 'Unrecognised master extension "%s"' % (name)
try:
cmd = sys.argv[2]
except:
cmd = 'check'
if cmd == 'check':
res, val = muxclient.check()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'MUX master alive with pid %u' % (val,)
elif cmd == 'exit':
res, val = muxclient.exit()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'MUX master temrinated'
elif cmd == 'stop':
res, val = muxclient.stop()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'MUX master stopped listing for mux clients'
elif cmd == 'forward' or cmd == 'cancel':
mode = True
if cmd == 'cancel':
mode = False
fwd_types = {
'local': SshMuxClient.MUX_FWD_LOCAL,
'remote': SshMuxClient.MUX_FWD_REMOTE,
'dynamic': SshMuxClient.MUX_FWD_DYNAMIC
}
type = sys.argv[3]
if type not in fwd_types.keys():
print >>sys.stderr, 'Invalid forwarding type %s' % (type, )
sys.exit(1)
if len(sys.argv) != 5:
print >>sys.stderr, 'Invalid number of arguments'
sys.exit(1)
fwdarg = sys.argv[4].split(':')
listen_host = ''
listen_port = 0
connect_host = ''
connect_port = 0
if len(fwdarg) == 1:
if type != 'dynamic':
print >>sys.stderr, 'Invalid number of arguments for non dynamic forwarding'
sys.exit(1)
listen_port = fwdarg[0]
connect_host = 'socks'
if len(fwdarg) == 2:
if type != 'dynamic':
print >>sys.stderr, 'Invalid number of arguments for non dynamic forwarding'
sys.exit(1)
listen_host = fwdarg[0]
listen_port = int(fwdarg[1])
connect_host = 'socks'
if len(fwdarg) == 3:
if type == 'dynamic':
print >>sys.stderr, 'Invalid number of arguments for dynamic forwarding'
sys.exit(1)
listen_port = int(fwdarg[0])
connect_host = fwdarg[1]
connect_port = int(fwdarg[2])
if len(fwdarg) == 4:
if type != 'dynamic':
print >>sys.stderr, 'Invalid number of arguments for dynamic forwarding'
sys.exit(1)
listen_host = fwdarg[0]
listen_port = int(fwdarg[1])
connect_host = fwdarg[2]
connect_port = int(fwdarg[3])
if type != 'dynamic' and connect_port < 0:
print >>sys.stderr, 'Invalid connecting port for forwarding: %u' % (connect_port,)
sys.exit(1)
res, val = muxclient.forward(
mode,
fwd_types[type],
listen_host,
listen_port,
connect_host,
connect_port)
if not res:
print >>sys.stderr, val
sys.exit(1)
if type == 'remote' and listen_port == 0:
print 'Allocated port for dynamic forwarding to %s:%u: %u' % (connect_host, connect_port, val)
print 'Request succeeded.'
elif cmd == 'forwards':
res, val = muxclient.forwards()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'List of forwardings:'
for fwd in val:
fid, \
ftype, \
listen_host, \
listen_port, \
connect_host, \
connect_port = fwd
print ' %u: %s forwarding %s:%u -> %s:%u' % (fid, ftype, listen_host, listen_port, connect_host, connect_port)
elif cmd == 'sessions':
res, val = muxclient.sessions()
if not res:
print >>sys.stderr, val
sys.exit(1)
print 'List of sessions:'
for session in val:
sid, \
stype, \
rid, \
cid, \
name, \
rname = session
print ' #%u %u %u %u %s: %s' % (sid,
stype,
rid,
cid,
name,
rname)
elif cmd == 'info':
if len(sys.argv) != 4:
print >>sys.stderr, 'Invalid number of arguments'
sys.exit(1)
res, val = muxclient.info(sys.argv[3])
if not res:
print >>sys.stderr, 'Invalid format: %s' % val
sys.exit(1)
print 'MUX info replay:'
print val
else:
print >>sys.stderr, 'Invalid mux command: %s' % (cmd,)
muxclient.close()
sys.exit(1)
muxclient.close()