很简单的一个脚本,就是开机时判断是不是在家庭网络中,如果是就自动拨号到公司
练习下python代码而已
代码中关于怎么获取本机IP的方法可以参考:
http://blog.chinaunix.net/uid-8874157-id-3890771.html
-
#!/usr/bin/env python
-
#coding:utf-8
-
# Author: Edward --<edgeman_03@163.com>
-
# Purpose: 判断是否为家庭网络环境,如果是,则自动启动OPENVPN服务连接至公司
-
# Created: 2013年09月08日
-
-
import os
-
import sys
-
import socket
-
-
def get_my_ip():
-
"""
-
Returns the actual ip of the local machine.
-
This code figures out what source address would be used if some traffic
-
were to be sent out to some well known address on the Internet. In this
-
case, a Google DNS server is used, but the specific address does not
-
matter much. No traffic is actually sent.
-
"""
-
try:
-
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
csock.connect(('8.8.8.8', 80))
-
(addr, port) = csock.getsockname()
-
csock.close()
-
return addr
-
except socket.error:
-
return "127.0.0.1"
-
-
def main():
-
if "192.168.1" in get_my_ip():
-
os.system('sudo service openvpn restart')
-
#print "starting openvpn services ok!"
-
else:
-
sys.exit()
-
-
if __name__=='__main__':
-
main()
再来一个windows下面的版本,方式差不多,只是程序用的是openvpn-gui的客户端
另外为使客户端连接时不提示对话框,静默连接,需修改注册表
修改位置: HKEY_LOCAL_MACHINE\SOFTWARE\OpenVPN-GUI ,修改 silent_connection 值为 1
代码如下:
-
#!/usr/bin/env python
-
#coding:utf-8
-
# Author: Edward -- <edgeman_03@163.com>
-
# Purpose: 判断是否为家庭网络环境,如果是,则自动启动OPENVPN服务连接至公司
-
# Created: 2013/9/9
-
-
import sys
-
import os
-
import socket
-
import time
-
-
def get_my_ip():
-
"""
-
Returns the actual ip of the local machine.
-
This code figures out what source address would be used if some traffic
-
were to be sent out to some well known address on the Internet. In this
-
case, a Google DNS server is used, but the specific address does not
-
matter much. No traffic is actually sent.
-
"""
-
try:
-
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-
csock.connect(('8.8.8.8', 80))
-
(addr, port) = csock.getsockname()
-
csock.close()
-
return addr
-
except socket.error:
-
return "127.0.0.1"
-
-
def main():
-
time.sleep(10)
-
if "192.168.1" in get_my_ip():
-
os.chdir(r"C:\Program Files\OpenVPN\bin")
-
os.popen('openvpn-gui.exe --connect hq-udp-1196.ovpn')
-
else:
-
sys.exit()
-
-
if __name__=='__main__':
-
main()
阅读(3001) | 评论(0) | 转发(0) |