-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_proxy.py
executable file
·68 lines (60 loc) · 2.01 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
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python2.7
#coding=utf-8
'''
Created on Sep 14, 2011
@author: dawn
'''
import pexpect
import time
from subprocess import call
from sys import argv
if len(argv) < 2:
print "usage: ssh_proxy.py server username password bind_ip"
else:
server = argv[1]
user_name = argv[2]
password = argv[3]
if len(argv) > 4:
bind_ip = argv[4]
else:
bind_ip = ''
bind_port = 7070
port = 22
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 clear_host():
call("ssh-keygen -R "+server,shell=True)
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)
print 'password sent waiting for done'
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__':
clear_host()
end_tunnel()
start_tunnel(shl,password)