Linux下使用python自动修改本机网关代码分享

yipeiwu_com5年前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

相关文章

详解appium+python 启动一个app步骤

详解appium+python 启动一个app步骤

询问度娘搭好appium和python环境,开启移动app自动化的探索(基于Android),首先来记录下如何启动待测的app吧! 如何启动APP?1.获取包名;2.获取launcher...

Python3访问并下载网页内容的方法

本文实例讲述了Python3访问并下载网页内容的方法。分享给大家供大家参考。具体如下: #!/usr/local/bin/python3.2 import urllib.reques...

python实现微信远程控制电脑

python实现微信远程控制电脑

首先,我们要先看看微信远程控制电脑的原理是什么呢? 我们可以利用Python的标准库控制本机电脑,然后要实现远程的话,我们可以把电子邮件作为远程控制的渠道,我们用Python自动登录...

django 按时间范围查询数据库实例代码

从前台中获得时间范围,在django后台处理request中数据,完成format,按照范围调用函数查询数据库。 介绍一个简单的功能,就是从web表单里获取用户指定的时间范围,然后在数据...

Python使用百度API上传文件到百度网盘代码分享

关于如何获取 access_token 这个可以自己查百度开放的OAuth 2.0 的 API。这里不做介绍。 第三方 Python 库 poster 复制代码 代码如下: # cod...