Linux下使用python自动修改本机网关代码分享
更新时间:2015年05月21日 10:29:41 投稿:junjie
这篇文章主要介绍了Linux下使用python自动修改本机网关代码分享,本文直接给出实现代码,需要的朋友可以参考下
#!/usr/bin/python
#auto change gateway Created By mickelfeng
import os
import random,re
g='gateway 192.168.1.'
rand=random.randint(1,3)
test='www.baidu.com'
command='/etc/init.d/networking restart'
GW = "%s%d"%(g,rand)
PingTest = 'ping -c 3 ' + test
try:
result=os.system(PingTest)
print result
if result!=0:
data = open('/etc/network/interfaces').read()
data = re.sub('gateway 192.168.1.*',GW, data)
open('/etc/network/interfaces', 'wb').write(data)
os.system(command)
os.system(command)
except:
pass
相关文章
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
下面小编就为大家带来一篇python anaconda 安装 环境变量 升级 以及特殊库安装的方法。2017-06-06


最新评论