Pingcheck – 用Ping检查网络通断并即时通知

使用方法:

python pingcheck.py <目标>

比如

python pingcheck.py 8.8.8.8

简单的小脚本…跨平台…分别用了pynotify和pywin32库来执行通知, 因此需要安装相应的库才可以使用.
用ping检查网络通断,并且在通断情况改变时提示用户…

截图:

下载地址:
脚本(任意平台): http://movieinfofetch.googlecode.com/files/pingcheck.py
Windows下的已编译版本(需要VC2005运行库): http://movieinfofetch.googlecode.com/files/pingcheck.exe

#!/usr/bin/python
import subprocess,platform,sys
if len(sys.argv)<2:
    print "Usage: pingcheck "
    sys.exit(1)
target=sys.argv[1]
if platform.system()[0]!="W":
    arg=['ping','-n',target]
    import pynotify
    notify=lambda a,b:pynotify.Notification(a,b).show()
else:
    arg=['ping','-t',target]
    import win32com.client
    axWshShell=win32com.client.Dispatch("WScript.Shell")
    notify=lambda a,b:axWshShell.Popup(b,5,a,0)
title="Ping "+target
p=subprocess.Popen(args=arg,bufsize=0,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
s=''
status=True
while p.poll()==None:
    a=p.stdout.read(1)
    if a!='\n':
        s+=a
    else:
        print s
        s=s.strip()
        if target not in s and s!='':
            if 'time=' in s and not status:
                notify(title,"Server is up!")
                status=True
            elif 'time=' not in s and status:
                notify(title,"Server is down!")
                status=False
        s=''

14 thoughts on “Pingcheck – 用Ping检查网络通断并即时通知”

Leave a Reply

Your email address will not be published. Required fields are marked *

QR Code Business Card