python3导入ping模块_Python ping 模块

本文介绍了如何使用Python结合fping模块批量检测IP在线状态,适用于服务器数量较多的情况。通过创建iplist文件存储IP,然后调用fping.sh脚本批量执行并读取结果,更新数据库中的IP状态。此外,还提供了Python实现的ping功能代码,展示了如何利用原始套接字发送ICMP请求并接收响应。
摘要由CSDN通过智能技术生成

print socket.gethostbyname('www.baidu.com')

fping功能

https://www.cnblogs.com/zhoujie/p/python17.html

适合服务器数量较大时使用,fping命令,它是对一个文件的批量ping,瞬间完成的,如果ping不通,那就较慢,日常ping不通的毕竟是少数,所以这个非常适用。来感受一下,它ping的结果,新建一个文件iplist,里面是IP列表,fping结果如下:

其实结果就两个 is alive / is unrreachable ,其它的中间检测时它自己输出的不用理会。

fping.sh :

#!/bin/bash

rm -f result.txt

cat ipmi_ping.txt | fping > result.txt

思路也很简单,将IP列表读取来写进一个iplist文件,然后再对这个文件fping(调用fping.sh)批量执行的结果写进result文件:

def check_online_ip():

ip = mysql('select * from ip_check')

#将IP写进一个文件

if os.path.exists('iplist.txt'):

os.remove('iplist.txt')

iplist= 'iplist.txt'

for i in range(0,len(ip)):

with open(iplist, 'a') as f:

f.write(ip[i][0]+'\n')

#对文件中的IP进行fping

p = subprocess.Popen(r'./fping.sh',stdout=subprocess.PIPE)

p.stdout.read()#读result.txt文件,将IP is unreachable的行提取更新mysql状态为1

result = open('result.txt','r')

content = result.read().split('\n')

for i in range(0,len(content)-1):

tmp = content[i]

ip = tmp[:tmp.index('is')-1]

Status = 0

if 'unreachable' in tmp:

Status = 1

#print i,ip

mysql('update ip_check set Status=%d where IP="%s"'%(Status,ip))

print 'check all ipconnectness over!'

将这个搞成计划任务,每天跑几遍,还是挺赞的。 呵呵。。

代码

#!/usr/bin/env python

"""

A pure python ping implementation using raw socket.

Note that ICMP messages can only be sent from processes running as root.

Derived from ping.c distributed in Linux's netkit. That code is

copyright (c) by The Regents of the University of California.

That code is in turn derived from code written by Mike Muuss of the

US Army Ballistic Research Laboratory in December, and

placed in the public domain. They have my thanks.

Bugs are naturally mine. I'd be glad to hear about them. There are

certainly word - size dependenceies here.

Copyright (c) Matthew Dixon Cowles, .

Distributable under the terms of the GNU General Public License

version . Provided with no warranties of any sort.

Original Version from Matthew Dixon Cowles:

-> ftp://ftp.visi.com/users/mdc/ping.py

Rewrite by Jens Diemer:

-> http://www.python-forum.de/post-69122.html#69122

Rewrite by George Notaras:

-> http://www.g-loaded.eu/2009/10/30/python-ping/

Fork by Pierre Bourdon:

-> http://bitbucket.org/delroth/python-ping/

Revision history

~~~~~~~~~~~~~~~~

November ,

-----------------

Initial hack. Doesn't do much, but rather than try to guess

what features I (or others) will want in the future, I've only

put in what I need now.

December ,

-----------------

For some reason, the checksum bytes are in the wrong order when

this is run under Solaris .X for SPARC but it works right under

Linux x86. Since I don't know just what's wrong, I'll swap the

bytes always and then do an htons().

December ,

----------------

Changed the struct.pack() calls to pack the checksum and ID as

unsigned. My thanks to Jerome Poincheval for the fix.

May ,

------------

little rewrite by Jens Diemer:

- change socket asterisk import to a normal import

- replace time.time() with time.clock()

- delete "return None" (or change to "return" only)

- in checksum() rename "str" to "source_string"

November ,

----------------

Improved compatibility with GNU/Linux systems.

Fixes by:

* George Notaras -- http://www.g-loaded.eu

Reported by:

* Chris Hallman -- http://cdhallman.blogspot.com

Changes in this release:

- Re-use time.time() instead of time.clock(). The implementation

worked only under Microsoft Windows. Failed on GNU/Linux.

time.clock() behaves differently under the two OSes[].

[] http://docs.python.org/library/time.html#time.clock

September ,

------------------

Little modifications by Georgi Kolev:

- Added quiet_ping function.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值