[Python] python自动修改本机网关的代码 →→→→→进入此内容的聊天室

来自 , 2021-03-15, 写在 Python, 查看 130 次.
URL http://www.code666.cn/view/662a2e96
  1. #!/usr/bin/python
  2. #auto change gateway Created By mickelfeng
  3. import os
  4. import random,re
  5. g='gateway 192.168.1.'
  6. rand=random.randint(1,3)
  7. test='www.baidu.com'
  8. command='/etc/init.d/networking restart'
  9. GW = "%s%d"%(g,rand)
  10. PingTest = 'ping -c 3 ' + test
  11. try:
  12.     result=os.system(PingTest)
  13.     print result
  14.     if result!=0:
  15.         data = open('/etc/network/interfaces').read()
  16.         data = re.sub('gateway 192.168.1.*',GW, data)
  17.         open('/etc/network/interfaces', 'wb').write(data)
  18.         os.system(command)
  19.         os.system(command)
  20. except:
  21.     pass
  22.  
  23. #//python/6074

回复 "python自动修改本机网关的代码"

这儿你可以回复上面这条便签

captcha