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

相关文章

Django的分页器实例(paginator)

先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginat...

Pycharm代码无法复制,无法选中删除,无法编辑的解决方法

Pycharm代码无法复制,无法选中删除,无法编辑的解决方法

菜单栏,tools--去掉勾选的Vim Emulator这个仿真插件就好了。 以上这篇Pycharm代码无法复制,无法选中删除,无法编辑的解决方法就是小编分享给大家的全部内容了,希望能...

Python实现获取磁盘剩余空间的2种方法

Python实现获取磁盘剩余空间的2种方法

本文实例讲述了Python实现获取磁盘剩余空间的2种方法。分享给大家供大家参考,具体如下: 方法1: import ctypes import os import platform...

用Python实现换行符转换的脚本的教程

很简单的一个东西,在'\n'、'\r\n'、'\r'3中换行符之间进行转换。 用法 复制代码 代码如下:usage: eol_convert.py [-h] [-r] [-m {u,p,...

Python关于excel和shp的使用在matplotlib

Python关于excel和shp的使用在matplotlib

关于excel和shp的使用在matplotlib 使用pandas 对excel进行简单操作 使用cartopy 读取shpfile 展示到matplotlib中 利用s...