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中利用h5py模块读取h5文件中的主键方法

如下所示: import h5py import numpy as np #HDF5的写入: imgData = np.zeros((2,4)) f = h5py.File('HDF...

详解python:time模块用法

详解python:time模块用法

time模块下有两种时间表示方法: 第1种是:时间戳的方式。是基于1970年1月1日0时0分0秒的偏移。浮点数。 第2种是:struct_time()类型的表示方法。gmtime()和l...

对Python w和w+权限的区别详解

今日上课,有位同学问到:w和w+有何区别呢。 说实话,我们经常只是用一种权限,没用在意之间的区别,实际上,w+具有可读可写权限,而w只有可写权限。 下面上代码: fd=open(...

python装饰器-限制函数调用次数的方法(10s调用一次)

这是博主最近一家大公司的面试题,写一个装饰器,限制函数每10s调用一次。当时是笔试的,只写了大概的代码,回来后温习了python装饰器的基础知识,把代码写完了。决定写篇博客记录下。 装饰...

python 消费 kafka 数据教程

1.安装python模块 pip install --user kafka-python==1.4.3 如果报错压缩相关的错尝试安装下面的依赖 yum install sna...