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

相关文章

pytorch模型预测结果与ndarray互转方式

预测结果转为numpy: logits=model(feature) #如果模型是跑在GPU上 result=logits.data.cpu().numpy() / logi...

python整小时 整天时间戳获取算法示例

根据当前时间戳获得整小时时间戳 unit = 3600 start_time = int(time.time())/3600 * 3600 根据当前时间戳获得整天时间戳 uni...

Python操作列表的常用方法分享

下面列出列表常用的方法操作列表以及小例子:1.  Append     在列表末尾添加元素,需在列表末尾添加元素,需要注意几个点:&nb...

Python3.5常见内置方法参数用法实例详解

本文实例讲述了Python3.5常见内置方法参数用法。分享给大家供大家参考,具体如下: Python的内置方法参数详解网站为:https://docs.python.org/3/libr...

Python模块汇总(常用第三方库)

Python模块汇总(常用第三方库)

模块 定义 计算机在开发过程中,代码越写越多,也就越难以维护,所以为了编写可维护的代码,我们会把函数进行分组,放在不同的文件里。在python里,一个.py文件就是一个模块 优点:...