1. Requirements Description
working environment, often use to ping network on the host to do network testing, such as the machine shelves, offline, testing and other needs, for a large number of machines, one by one host ping test, obviously difficult to meet the requirements, for the machine more than the scene, The IP address that needs to perform the ping test can be stored in a file, and it is convenient and convenient to invoke the script to perform the network test.
2. Program Contents
vim ping.py #!/usr/bin/env python#_*_ coding:utf8 _*_#author: happy# from Happy test test The http://happylab.blog.51cto.com# script is primarily used to ping the test import reimport subprocessdef check_alive (IP, count=1,timeout=1): " ping network test, by calling the ping command, send an ICMP packet, from the results through the regular match whether there are 100% keywords, there is the loss of packets, none of the normal ' cmd = ' ping -c %d -w %d %s ' % (COUNT,TIMEOUT,IP) p = subprocess. Popen (cmd, Stdin=subprocess. pipe, stdout= Subprocess. Pipe, &nbSp; stderr=subprocess. Pipe, shell= true ) result = p.stdout.read () regex = re.findall (' 100% packet loss ', result) if len (regex) == 0: print "\033[31m%s up\033[0m" % (IP) else: print "\033[32m %s down\033[0m " % (IP) if __name__ == " __main__ ": with file ('/root/ip.txt ', ' R ') as f: &nbsP; for line in f.readlines (): ip = line.strip () check_alive (IP) #执行函数功能调用
3. Test results
Python ping.py 10.16.2.4 DOWN10.16.2.5 DOWN10.16.4.16 UP10.16.4.30 UP10.16.4.61 UP10.16.4.65 UP10.16.4.66 UP10.16.4.68 UP10.16.4.74 UP10.16.4.76 UP10.16.4.77 up
4. About subprocess Module Learning
In Python, there are three main ways to invoke commands in the shell: 1. Os.system (), 2. commands modules, 3.subprocess modules, methods are different.
1.os.system (), call system command, get command output and accept return code
>>> import os>>> os.system (' df -h ') filesystem size used avail use% mounted on /dev/sda2 9.9g 7.3g 2.2G 78% /tmpfs 3.9g 4.0k 3.9g 1% /dev/shm/dev /sda1 1008m 82m 876M 9% /boot/dev/sda4 899g 36g 818g 5% /data110.16.2.8:openstack _glances 899G 37G 816G 5% /var/lib/glance/images0>> > result = os.system (' df -h ') #保存返回值Filesystem size used avail use% mounted on /dev/sda2 9.9g 7.3g 2.2G 78% /tmpfs 3.9g 4.0k 3.9g 1% /dev/shm/dev /sda1 1008m 82m 876M 9% /boot/dev/sda4 899g 36g 818g 5% /data110.16.2.8:openstack _glances 899g 37g 816g 5% /var/lib/glance/images >>> print result0
2.cmmands module
>>> import commands# get command output result >>> commands.getoutput (' df -h ') ' filesystem size used avail use% mounted on\n/dev/sda2 9.9G 7.3G 2.2G 78% /\ntmpfs 3.9G 4.0K 3.9g 1% /dev/shm\n/dev/sda1 1008M 82M 876M 9% /boot\n/dev/sda4 899g 36g 818g 5% /data1\n10.16.2.8:openstack_glances\n 899g 37g 816g 5% /var/lib/glance/images ' #得到命令输出结果和返回码, showing >>> commands.getstatusoutput (' DF ' in the form of a tuple -h ') (0, ' filesystem size Used Avail Use% Mounted on\n/dev/sda2 9.9g 7.3g 2.2g 78% /\ntmpfs 3.9g 4.0K 3.9G 1% /dev/shm\n/dev/sda1 1008M 82M 876M 9% /boot\n/dev/sda4 899g 36g 818g 5% /data1\n10.16.2.8:openstack_glances\n 899G 37G 816g 5% /var/lib/glance/images ') >>> >>> Commands.getstatusoutput (' df -h ') [0]0
3.os.popen () module
>>> import os>>> p = os.popen (' df -h ') >>> p.read () ' filesystem size used Avail Use% Mounted on\n/dev/sda2 9.9G 7.3G 2.2G 78% /\ntmpfs 3.9G 4.0K 3.9G 1% /dev/shm\n/dev/sda1 1008M 82M 876M 9% /boot\n/dev/sda4 899G 36G 818g 5% /data1\n10.16.2.8:openstack_glances\n 899g 37g 816g 5% /var/lib/glance/images\n ' Other methods, as well as p.readline () read a line, p.readlines () reads all the rows, and the file operation is similar to the method! It is not recommended to use this method, subprocess is recommended because Subprocess is a more advanced package of os.popen () and more powerful
4.subprocess Module
This article from "Happy Laboratory" blog, declined reprint!
Python real-Combat series of batch host ping Network test (07)