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.