Python网络连通性检测-样例
author:Skate
time:2014/10/13
python网络连通性检测:
[root@skatedb55 ~]# vi checkping.py
#!/usr/bin/env Python
#-*- coding: utf-8 -*-
#Author:Skate
import os,sys,re
import subprocess
def NetCheck(ip):
try:
p = subprocess.Popen(["ping -c 1 -w 1 "+ ip],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
out=p.stdout.read()
#err=p.stderr.read()
regex=re.compile('100% packet loss')
#print out
#print regex
#print err
if len(regex.findall(out)) == 0:
print ip + ': host up'
return 'UP'
else:
print ip + ': host down'
return 'DOWN'
except:
print 'NetCheck work error!'
return 'ERR'
if __name__ == '__main__':
NetCheck('10.20.0.56')
NetCheck('10.10.0.56')
NetCheck('10.10.0')
运行结果:
[root@skatedb55 ~]# python checkping.py
10.20.0.56: host up
UP
10.10.0.56: host down
10.10.0: host down
-----end------
相关文章