-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_tunnel.py
More file actions
90 lines (64 loc) · 2.46 KB
/
ssh_tunnel.py
File metadata and controls
90 lines (64 loc) · 2.46 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
"""This starts an SSH tunnel to a given host. If the SSH process ever dies then
this script will detect that and restart it."""
import pexpect
import time
import os
import threading
from subprocess import Popen, PIPE, STDOUT
class TunnelThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = True
def stop_tunnel(self):
self.running = False
def start_tunnel(self):
try:
ssh_tunnel = pexpect.spawn(tunnel_command, timeout=4800)
ssh_tunnel.expect ('password:')
ssh_tunnel.sendline (X)
time.sleep (2) # Cygwin is slow to update process status.
p = Popen('ps -eo pid,lstart,cmd | grep "' + tunnel_command + '"', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
output = p.stdout.read().split("\n")[0]
command = "echo $'\nHTTP tunnel restarted, new processs info:\n%s' >> /path_to_log_file/ssh_tunnel_log" % output
os.system(command)
ssh_tunnel.expect(pexpect.EOF)
except Exception, e:
command = "echo $'\nHTTP TIMEOUT reached at : ' %s >> /path_to_log_file/ssh_tunnel_log" % time.asctime()
os.system(command)
def run(self):
self.start_tunnel()
threading.Thread.__init__(self)
tunnel_command = '/usr/bin/ssh -NnTxi /root/cie.if -R your_port:localhost:22 -l your_user your_domain.com'
host = 'your_domain.com'
#Credentials
user = 'your_user'
X = 'your_password'
def get_process_info ():
# This seems to work on both Linux and BSD, but should otherwise be considered highly UNportable.
ps = pexpect.run ('ps ax -O ppid')
pass
def main ():
t = TunnelThread()
while True:
try:
ps = pexpect.spawn ('nmap -p your_port your_domain.com')
time.sleep(1)
net_status = ps.expect (['closed','open'])
ps = pexpect.spawn ('pgrep -f "%s"' % tunnel_command)
time.sleep (0.1)
ppid = ps.read()
ps.close()
if not ppid:
time.sleep(3)
t.start()
elif net_status == 0 and ppid:
ps = pexpect.spawn ('pkill -9 -f "%s"' % tunnel_command)
time.sleep(3)
t = TunnelThread()
t.start()
time.sleep(2)
except pexpect.EOF:
pass
if __name__ == '__main__':
main ()