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

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

相关文章

scrapy自定义pipeline类实现将采集数据保存到mongodb的方法

本文实例讲述了scrapy自定义pipeline类实现将采集数据保存到mongodb的方法。分享给大家供大家参考。具体如下: # Standard Python library im...

python数据类型判断type与isinstance的区别实例解析

在项目中,我们会在每个接口验证客户端传过来的参数类型,如果验证不通过,返回给客户端“参数错误”错误码。 这样做不但便于调试,而且增加健壮性。因为客户端是可以作弊的,不要轻易相信客户端传过...

Python使用urllib2模块实现断点续传下载的方法

本文实例讲述了Python使用urllib2模块实现断点续传下载的方法。分享给大家供大家参考。具体分析如下: 在使用HTTP协议进行下载的时候只需要在头上设置一下Range的范围就可以进...

python3.6使用tkinter实现弹跳小球游戏

本文实例为大家分享了python3.6实现弹跳小球游戏的具体代码,供大家参考,具体内容如下 import random import time from tkinter import...

Python读写Excel文件的实例

Python读写Excel文件的实例

最近由于经常要用到Excel,需要根据Excel表格中的内容对一些apk进行处理,手动处理很麻烦,于是决定写脚本来处理。首先贴出网上找来的读写Excel的脚本。 1.读取Excel(需要...