1. 程式人生 > >Python網路連通性檢測-樣例

Python網路連通性檢測-樣例

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

author:Skate
time:2014/10/13

 

Python網路連通性檢測:

[[email protected] ~]# 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')
   

執行結果:

[[email protected] ~]# 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

這裡寫圖片描述