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

相关文章

python实现kmp算法的实例代码

python实现kmp算法的实例代码

kmp算法 kmp算法用于字符串的模式匹配,也就是找到模式字符串在目标字符串的第一次出现的位置 比如 abababc 那么bab在其位置1处,bc在其位置5处 我们首先想到的最简...

基于python中__add__函数的用法

运算符重载 _add ##定义:让自定义的类生成的对象(实例)能够使用运算符进行操作 class Vector01: ‘'‘定义一个一维向量''' def init(self,x...

Python单例模式的两种实现方法

Python单例模式的两种实现方法 方法一  import threading class Singleton(object): __instance = N...

详解Python 序列化Serialize 和 反序列化Deserialize

详解Python 序列化Serialize 和 反序列化Deserialize 序列化 (serialization) 序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是...

python 抓包保存为pcap文件并解析的实例

首先是抓包,使用scapy模块, sniff()函数 在其中参数为本地文件路径时,操作为打开本地文件 若参数为BPF过滤规则和回调函数,则进行Sniff,回调函数用于对Sniff到的数据...