-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh_proxy.py
executable file
·56 lines (47 loc) · 1.76 KB
/
ssh_proxy.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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
#coding=utf-8
'''
Created on Sep 14, 2011
@author: dawn
'''
import pexpect
import time
from subprocess import call
server = 's05.flyssh.net'
bind_ip = 'localhost'
bind_port = 7070
user_name = 'ssh_account'
password = 'ssh_password'
shl = 'ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no\
-p %(port)d -f -N -g -D %(bind_ip)s:%(bind_port)s %(user_name)s@%(server)s'%{'bind_ip':bind_ip,
'bind_port':bind_port,
'server':server,
'user_name':user_name,
'port':port}token = '%(user_name)s@%(server)s'%{'user_name':user_name,'server':server}
def start_tunnel(shl,password):
try:
ssh_tunnel = pexpect.spawn(shl)
print 'shl run ',shl
index = ssh_tunnel.expect(['yes/no','password:'])
if index == 0:
print 'ensure connection'
ssh_tunnel.sendline('yes')
index = ssh_tunnel.expect(['password:'])
print 'password asking...'
time.sleep(0.1)
ssh_tunnel.sendline(password)
else:
print 'password asking...'
time.sleep(0.1)
ssh_tunnel.sendline(password)
print 'password sent waiting for done'
time.sleep(5)
ssh_tunnel.expect(pexpect.EOF)
print 'done'
except Exception,e:
print str(e)
def end_tunnel():
call('pkill -f %s'%token,shell=True)
if __name__ == '__main__':
end_tunnel()
start_tunnel(shl,password)