Use python to automatically modify local Gateway code in Linux

Source: Internet
Author: User
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
#! /Usr/bin/python # auto change gateway Created By mickelfengimport osimport random, reg = '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' + testtry: 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) failed t: pass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.