python实战系列之批量主机ping网络测试(七)

1.需求说明

  工作环境中,经常会有使用到ping对网络上的主机做网络测试,如机器上架,下线,测试等一些需要,对于大批量的机器来说,逐个主机ping测试,显然难以满足要求,对于机器比较多的场景,可以将需要执行ping测试的IP地址存放至一个文件内,调用脚本执行网络测试,方便,便捷。

2.程序内容

vim ping.py 
#!/usr/bin/env python
#_*_ coding:utf8 _*_
#author: Happy
#来自Happy试验试验 http://happylab.blog.51cto.com
#脚本主要用于做ping测试

import re
import subprocess

def check_alive(ip,count=1,timeout=1):
        '''
        ping网络测试,通过调用ping命令,发送一个icmp包,从结果中通过正则匹配是否有100%关键字,有则表示丢包,无则表示正常
        '''
        cmd = 'ping -c %d -w %d %s' % (count,timeout,ip)

        p = subprocess.Popen(cmd,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                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:
                for line in f.readlines():
                        ip = line.strip()
                        check_alive(ip)         #执行函数功能调用

3. 测试结果

python ping.py 
10.16.2.4 DOWN
10.16.2.5 DOWN
10.16.4.16 UP
10.16.4.30 UP
10.16.4.61 UP
10.16.4.65 UP
10.16.4.66 UP
10.16.4.68 UP
10.16.4.74 UP
10.16.4.76 UP
10.16.4.77 UP

4.关于subprocess模块学习

   在python中,调用shell中的命令主要有三种方法:1. os.system(), 2.commands模块,3.subprocess模块,方法都有所不同。

1.os.system(),调用系统命令,获取命令输出结果并接受返回码

>>> 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% /data1
10.16.2.8:openstack_glances
                      899G   37G  816G   5% /var/lib/glance/images
0

>>> 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% /data1
10.16.2.8:openstack_glances
                      899G   37G  816G   5% /var/lib/glance/images
>>> print result
0

2.cmmands模块

>>> import commands

#得到命令输出结果
>>> 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'

#得到命令输出结果和返回码,以元组的形式展示
>>> commands.getstatusoutput('df -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()模块

>>> 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'

其他方法,还有p.readline()读一行,p.readlines()读取所有的行,和文件操作的方法相类似!不建议使用这种方法,建议使用subprocess,因为subprocess是os.popen()的更高级封装,功能更强

4.subprocess模块

>>> import subprocess            
>>> p = subprocess.Popen('df -h',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

#获取命令执行结果的返回码,通过wait()函数
>>> p.wait()
0

#获取命令输出结果(标准输出),通过read()方法
>>> p.stdout.read()
'Filesystem            Size  Used Avail Use% Mounted on\n/dev/sda2             9.9G  7.3G  2.1G  79% /\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'

#获取命令错误输出结果,通过read()方法
>>> p.stderr.read()            #为空,说明没有错误输出,看如下例子
''

#获取错误输出
>>> subprocess.Popen('ls /etc/password',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,close_fds=True)
<subprocess.Popen object at 0x7f267528dbd0>
>>> p = subprocess.Popen('ls /etc/password',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,close_fds=True)
>>> p.stderr.read()
'ls: cannot access /etc/password: No such file or directory\n'

@获取错误输出的其他方法还有:read(),readline(),readlines(),close(),write()和writelines()等.

关于subprocess的模块的用法,可以参考http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html



本文出自 “Happy实验室” 博客,谢绝转载!

你可能感兴趣的:(python,ping,subprocess)