Python implements 2 ways to detect whether a server can ping through

Source: Internet
Author: User
Good want to 2014 before the end of a 10 blog, ~ (>_<) ~, not to write a blog is not a good omen, at least that the desire to learn and knowledge of the study is not so positive, if you say this 1 days I can drive out a few exquisite blog, you believe it, haha, anyway, I am a letter ...

2 ways Python detects if a server is ping

1, the first kind of comparison, is to call the shell with Ping,python, this applies to fewer servers, hundreds of is already very slow (of course, the Python synchronization method, if the Nodejs async mode is very fast, but Nodejs CPU calculation does not work, So try to have only 200 or so of the server can ping at the same time, more then the program will also be collapsed)

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

The code is as follows:


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


In fact, it is very simple, ping 3 packets, as long as the ping pass, the result of the above return is not 0. $ $ is the first parameter passed in, which is the IP

The idea is simple, read the IP list from the database, and then call the above script:

The code is as follows:


#检查ip能否ping通
#0: Normal, 1:ping
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 a large number of servers to use, fping command, it is a file of the batch ping, instantaneous completion, if the ping does not pass, it is slower, the daily Ping is a few, after all, so this is very applicable. To feel the result of it ping, create a new file IPList, which is the IP list, fping The result is as follows:

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

Fping.sh:

The code is as follows:


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


The idea is also simple, read the IP list to write into a iplist file, and then write the result of this file fping (call fping.sh) batch execution into the results file:

The code is 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 to update the row fetch for IP is unreachable mysql status to 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 great way to make a plan and run it a few 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.