Skip to content

Instantly share code, notes, and snippets.

@etng
Last active July 26, 2021 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etng/4473069 to your computer and use it in GitHub Desktop.
Save etng/4473069 to your computer and use it in GitHub Desktop.
用于使用地下铁路ssh代理的python脚本
#!/usr/bin/python
#coding=utf-8
'''
用于使用地下铁路ssh代理的python脚本
1. 把你的邮箱地址发给我(etng2004#gmail.com),标题为:dixiatielu,内容是你的邮箱地址, 我会把邀请码和使用方法回给你
2. 个人目录下创建 .gfw.ini,如运行(vim ~/.gfw.ini)写入以下内容:
[account]
host=地下铁路主机地址
port=地下铁路端口
user=地下铁路用户名
password=地下铁路密码
[proxy]
port=你作为代理的端口
3. 运行本脚本 `python gfw.py`
4. 在浏览器中使用 socket5 代理上网地址就是你运行这个脚本的服务器IP端口就是上面的"代理端口"处所填的值
5. 访问下 twitter.com, 若能访问,便已成功
this is just a python script to use dixiatielu ssh proxy
1. send me your email address (mail to: etng2004#gmail.com) with subject: dixiatielu, and I will send you an invite code
2. create file gfw.ini with the following content
[account]
host=dixiatielu host
port=dixiatielu port
user=dixiatielu user
password=dixiatielu password
[proxy]
port=your proxy port
3. run `python gfw.py` in a *nix box,
4. use socket5 proxy in your browser with the *nix box's IP and port "your proxy port"
5. check twitter.com, it works
'''
import pexpect
import re
import os
from ConfigParser import ConfigParser
if __name__ == '__main__':
cfg = ConfigParser()
cfg.read(os.path.expanduser('~/.gfw.ini'))
ssh_config_file = os.path.expanduser('~/.ssh/config')
# ssh_config_file = os.path.expanduser('~/ssh_config.txt')
flag=False
if os.path.isfile(ssh_config_file):
host_re = re.compile(r"^\s*Host\s+gfw\s*$")
with open(ssh_config_file, 'r') as f:
for line in f:
if(host_re.match(line)):
flag = True
break
if flag==False:
with open(ssh_config_file, 'a+') as f:
f.seek(0, 2)
f.write("""
Host gfw
HostName {host}
User {user}
Port {port}
DynamicForward {proxy_port}
Compression yes
GatewayPorts yes
""".format(
host = cfg.get('account', 'host'),
user = cfg.get('account', 'user'),
port = cfg.get('account', 'port'),
proxy_port = cfg.get('proxy', 'port')
))
os.chmod(ssh_config_file, 0600);
child = pexpect.spawn('ssh gfw')
child.expect('password:')
child.sendline(cfg.get('account', 'password'))
# @todo do not interact
child.interact()
@etng
Copy link
Author

etng commented Feb 27, 2013

由于ssh翻墙很容易被GFW侦破,地下铁路已经停用了ssh方式的翻墙,其他方式基本涉及到安装证书或者客户端软件,在非管理员权限的机器上是不可能做到的。用此脚本的朋友就自己找ssh,不用发邮件来要地下铁路的翻墙方式了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment