Use python to automatically modify local gateway code in Linux
This article mainly introduces how to use python to automatically modify the local gateway code in Linux. This article provides the implementation code directly. For more information, see
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
#! /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 t:
Pass
|