-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_forwarding.py
executable file
·232 lines (184 loc) · 5.74 KB
/
ssh_forwarding.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python
# encoding: utf-8
'''
__version__ = "0.01"
__author__ = "Jinzheng Zhang"
__email__ = "[email protected]"
'''
from __future__ import print_function
import os
import time
import json
import socket
from threading import Thread, Lock
from subprocess import PIPE, Popen
from optparse import OptionParser
INTERVAL = 5
LOCAL_ADDR = '127.0.0.1'
def shell(command, stdin=None):
process = Popen(
args=command,
stdout=PIPE,
stderr=PIPE,
stdin=stdin,
shell=True,
close_fds=True
)
return process
def os_kill(pid):
if pid is not None:
shell("kill -9 %s" % pid)
def close_noexcept(fd):
try:
fd.close()
except:
pass
def terminate_subprocess(proc, msg):
print("terminate subprocess: %s" % msg)
close_noexcept(proc.stdin)
close_noexcept(proc.stdout)
close_noexcept(proc.stderr)
try:
proc.kill()
except:
pass
proc.wait()
def communicate(proc):
while True:
try:
proc.stderr.read()
proc.stdout.read()
except Exception as e:
print("communicate:", e)
break
time.sleep(INTERVAL)
class Forwarding(Thread):
def lock(func):
def wrapper(*args, **kwargs):
with args[0].lock:
return func(*args, **kwargs)
return wrapper
def get_tasks(self):
for l in self.forwarding_list:
addrs = l.split(':')
if len(addrs) != 3 and len(addrs) != 4:
raise(Exception('invalid forwarding list'))
task = {'forwarding': l, 'down': True}
if len(addrs) == 3:
if self.flag == 'L':
task['check_addr'], task['check_port'] = \
LOCAL_ADDR, addrs[0]
if len(addrs) == 4:
task['check_port'] = addrs[1]
if self.flag == 'L':
if addrs[0] == '0.0.0.0':
task['check_addr'] = LOCAL_ADDR
else:
task['check_addr'] = addrs[0]
if self.flag == 'R':
if addrs[0] == '0.0.0.0':
task['check_addr'] = self.host
self.tasks.append(task)
def __init__(self, cfg):
self.lock = Lock()
if cfg['mode'] == 'local' or cfg['mode'] == 'L':
self.flag = 'L'
elif cfg['mode'] == 'remote' or cfg['mode'] == 'R':
self.flag = 'R'
else:
raise(Exception('unknown mode'))
self.timeout = cfg.get('timeout', 2)
self.close = False
remote = cfg['remote']
self.host = remote['host']
self.user = remote['user']
self.port = remote.get('port', 22)
self.tasks, self.forwarding_list = [], cfg['forwarding_list']
self.get_tasks()
def __str__(self):
return json.dumps(self.tasks)
def run(self):
print(self)
try:
while True and not self.close:
self.check_status(False)
time.sleep(INTERVAL)
except (EOFError):
return
def forwarding(self, task):
ssh_cmd = 'ssh -o ServerAliveInterval=60 -o StrictHostKeyChecking=no' \
' -{flag}' \
' {forwarding}' \
' {user}@{host} -p {port}'
attr = {
'flag': self.flag,
'forwarding': task['forwarding'],
'user': self.user,
'host': self.host,
'port': self.port,
}
cmd = ssh_cmd.format(**attr)
task['proc'] = shell(cmd, PIPE)
Thread(target=communicate, args=(task['proc'],)).start()
def check_alive(self, task, verbose):
if not task.get('check_addr'):
if task.get('proc'):
try:
task['proc'].stdin.write('pwd\n')
print("interact ok")
except IOError:
task['down'] = True
else:
task['down'] = False
return
addr, port = task['check_addr'], task['check_port']
msg = None
try:
sock = None
sock = socket.create_connection((addr, int(port)),
self.timeout)
except socket.error as e:
msg = e
task['down'] = True
else:
task['down'] = False
msg = 'success'
finally:
if sock:
sock.close()
if verbose:
print("connect %s:%s (%s)" % (addr, port, msg))
@lock
def check_status(self, verbose):
for task in self.tasks:
self.check_alive(task, verbose)
if task['down']:
if task.get('proc'):
terminate_subprocess(task['proc'], task['forwarding'])
self.forwarding(task)
@lock
def exit(self):
self.close = True
for task in self.tasks:
if task.get('proc'):
terminate_subprocess(task['proc'], task['forwarding'])
def check_input(ssh_forwarding):
while True:
try:
raw_input("type ^D to exit\n")
except (EOFError):
ssh_forwarding.exit()
os_kill(os.getpid())
else:
ssh_forwarding.check_status(True)
def main():
parser = OptionParser()
parser.add_option('-c', dest='config', action="store", type="string",
help='Specify the config json file')
(options, args) = parser.parse_args()
cfg = json.load(open(options.config))
ssh_forwarding = Forwarding(cfg)
Thread(target=check_input, args=(ssh_forwarding,)).start()
Thread(target=ssh_forwarding.run).start()
if __name__ == '__main__':
main()