发布时间:2019-09-06 08:53:45编辑:auto阅读(2576)
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------
上一篇: Python中的操作符--转载
下一篇: python 打印矩阵
47863
46424
37314
34760
29332
25990
24945
19967
19564
18050
5806°
6433°
5947°
5975°
7080°
5926°
5963°
6456°
6418°
7798°