分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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------
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/2e19ffb3303a490d20ea5d00dd397156.gif)