Python implements 2 ways to detect if a server can ping _python

Source: Internet
Author: User

I want to throw out another 10 posts before the end of 2014, ~ (>_<) do not write a blog is not a good omen, at least to explain the desire for learning and knowledge of the study are not so positive, if this 1 days I can drive out a few exquisite Bowen, you believe it, haha, anyway, I am a letter ...

2 ways Python detects whether a server is ping

1, the first comparison is to call the shell with Ping,python, this applies to a small number of servers, hundreds of has been very slow (of course, the Python synchronization method, if Nodejs asynchronous way is still very fast, but Nodejs CPU calculation, So try the next only about 200 servers can ping at the same time, more words will also collapse the program

The shell script is simple enough, ping.sh as follows:

Copy Code code as follows:

#!/bin/bash
Ping= ' Ping-c 3 $ | grep ' 0 received ' | Wc-l '
Echo $PING

Actually very simple, ping 3 packages, as long as ping, the result of the return is not 0. Is the first parameter passed in, that is, IP

The idea is simply to read the IP list from the database and then invoke the above script:

Copy Code code as follows:

#检查ip能否ping通
#0: Normal, 1:ping not pass
Def check_ip_ping ():
Record = Get_ip () #从数据库中读取的IP列表
For I in range (0,len (record)):
p = subprocess. Popen ([R './ping.sh ', record[i]],stdout=subprocess. PIPE)
result = P.stdout.read ()
Status = 0
if result = = ' 1\n ':
Status = 1
#print I,record[i], '----ping failed----'
Else
Ping_ok.append (Record[i])
#print I,record[i], '----ping success----'
MySQL (' Update ip_connect set status=%d where ip= '%s '% (status,record[i))

2, much faster than this, suitable for the large number of servers to use, fping command, it is a file of the bulk ping, instantaneous completion, if the ping impassability, it is slower, the daily ping impassability after all is a minority, so this very suitable. To feel, it ping the result, create a new file IPList, which is the IP list, fping results are as follows:

In fact, the results of the two is Alive/is unrreachable, the other intermediate detection of its own output ignore.

Fping.sh:

Copy Code code as follows:

#!/bin/bash
Rm-f Result.txt
Cat Ipmi_ping.txt | fping > Result.txt

The idea is also simple, read the IP list into a iplist file, and then write the result file for the fping (call fping.sh) batch execution:
Copy Code code as follows:

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 file, extract the row of the IP is unreachable update MySQL state is 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! '

It's a good idea to make a plan task and run it several times a day. Oh..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.