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设计模式之访问者模式

轻松掌握python设计模式之访问者模式

本文实例为大家分享了python访问者模式代码,供大家参考,具体内容如下 """访问者模式""" class Node(object): pass class A(Node):...

python实时获取外部程序输出结果的方法

如下所示: s=subprocess.Popen("ping baidu.com -t",bufsize=0,stdout=subprocess.PIPE,universal_new...

Python进程间通信用法实例

本文实例讲述了Python进程间通信用法。分享给大家供大家参考。具体如下: #!/usr/bin/env python # -*- coding=utf-8 -*- import m...

Python 利用内置set函数对字符串和列表进行去重的方法

如下所示: # coding:utf8 __author__ = 'libingxian' __date = "20170415" # 由于数据类型set本身具有无序,唯一值的特性...

浅谈Python中(&,|)和(and,or)之间的区别

浅谈Python中(&,|)和(and,or)之间的区别

(&,|)和(and,or)是两组比较相似的运算符,用在“与”/ “或”上,在用法上有些许区别。 (&,|)和(and,or)是用来比较两组变量的,格式基本上是: a & b a...