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

相关文章

python命令行参数解析OptionParser类用法实例

python命令行参数解析OptionParser类用法实例

本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考。 具体代码如下: from optparse import OptionParser...

详解Python_shutil模块

import shutil 高级的文件,文件夹,压缩包的处理模块,也主要用于文件的拷贝 shutil.copyfileobj(fsrc,fdst[,length]):  将文件的内容拷...

python制作抖音代码舞

python制作抖音代码舞

本文实例为大家分享了抖音代码舞python制作代码,供大家参考,具体内容如下 一、效果图 二、转换代码 from img import Image ascil_char = l...

简单了解python单例模式的几种写法

方法一:使用装饰器 装饰器维护一个字典对象instances,缓存了所有单例类,只要单例不存在则创建,已经存在直接返回该实例对象。 def singleton(cls): inst...

python实现从pdf文件中提取文本,并自动翻译的方法

python实现从pdf文件中提取文本,并自动翻译的方法

针对Python 3.5.2 测试 首先安装两个包: $ pip install googletrans $ pip install pdfminer3k googletrans会提供一...