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中getpass模块无回显输入源码解析

本文主要讨论了python中getpass模块的相关内容,具体如下。 getpass模块 昨天跟学弟吹牛b安利Python标准库官方文档的时候偶然发现了这个模块。仔细一看内容挺少的,只有...

最基础的Python的socket编程入门教程

本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下。 Pyth...

Python遍历pandas数据方法总结

Python遍历pandas数据方法总结

前言 Pandas是python的一个数据分析包,提供了大量的快速便捷处理数据的函数和方法。其中Pandas定义了Series 和 DataFrame两种数据类型,这使数据操作变得更简...

Python中转换角度为弧度的radians()方法

 radians()方法把角度转化为弧度角x。 语法 以下是radians()方法的语法: radians(x) 注意:此函数是无法直接访问的,所以我们需要导入mat...

Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法

Python 出现错误TypeError: ‘NoneType' object is not iterable解决办法 TypeError: 'NoneType' object is n...