Linux下使用python自动修改本机网关代码分享

这篇文章主要介绍了Linux下使用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

以上就是Linux下使用python自动修改本机网关代码分享的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python