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使用电子邮件模块smtplib的方法

python使用电子邮件模块smtplib的方法

Smptp类定义:smtplib.SMTP(host[,port[,local_hostname[,,timeout]]]),作为SMTP的构造函数,功能是与smtp服务器建立连接,在连...

python傅里叶变换FFT绘制频谱图

本文实例为大家分享了python傅里叶变换FFT绘制频谱图的具体代码,供大家参考,具体内容如下 频谱图的横轴表示的是 频率, 纵轴表示的是振幅 #coding=gbk...

简述Python中的进程、线程、协程

进程、线程和协程之间的关系和区别也困扰我一阵子了,最近有一些心得,写一下。 进程拥有自己独立的堆和栈,既不共享堆,亦不共享栈,进程由操作系统调度。 线程拥有自己独立的栈和共享的堆,共享堆...

Python使用字典的嵌套功能详解

当需要存储很多同类型的不通过数据时可能需要使用到嵌套,先用一个例子说明嵌套的使用 1、在列表中存储字典 #假设年级里有一群国际化的学生,有黄皮肤的中国人、有白皮肤的美国人也有黑皮肤的...

python re.sub()替换正则的匹配内容方法

如下所示: import re c = re.compile(r'\d') s = 'you1are2welcome' # 用指定的内容,替换正则匹配的内容,也可以指...