Skip to content

Instantly share code, notes, and snippets.

@davyzhang
Created June 12, 2014 07:02
Show Gist options
  • Save davyzhang/a1ca409bc075783eb159 to your computer and use it in GitHub Desktop.
Save davyzhang/a1ca409bc075783eb159 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
#coding=utf-8
'''
Created on Sep 14, 2011
@author: dawn
'''
import pexpect
import time
from subprocess import call
server = 's02.flyssh.net'
bind_ip = 'localhost'
bind_port = 7070
user_name = ''
password = ''
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 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment