2025年3月28日 星期五 甲辰(龙)年 月廿七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > Python

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

时间:11-14来源:作者:点击数:44

使用方法:

  • 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=''
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门