Python detects if the server is pinging

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:

# !/bin/bash  $ | grep ' 0 received ' | WC-$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:

#Check that the IP can ping through#0: Normal, 1:ping not passdefcheck_ip_ping (): Record= Get_ip ()#List of IP read from database     forIinchRange (0,len (record)): P = subprocess. Popen ([R'./ping.sh', record[i]],stdout=subprocess. PIPE) Result = p.stdout.read () Status=0ifresult = ='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:

# !/bin/bash - F | 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:

defcheck_online_ip (): IP= MySQL ('SELECT * from Ip_check')    #write IP into a file    ifOs.path.exists ('Iplist.txt'): Os.remove ('Iplist.txt') IPList='Iplist.txt'     forIinchRange (0,len (IP)): With open (IPList,'a') as F:f.write (Ip[i][0]+'\ n')    #fping The IP in the file   p = subprocess. Popen (R'./fping.sh', stdout=subprocess. PIPE) P.stdout.read () #read the Result.txt file, update the line extraction for IP is unreachable mysql status to 1result = Open ('Result.txt','R') Content= Result.read (). Split ('\ n')     forIinchRange (0,len (content)-1): TMP=Content[i] IP= Tmp[:tmp.index (' is')-1] Status=0if 'Unreachable' inchTmp:status= 1#Print I,ipMySQL'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..

Python detects if the server is pinging

Related Article

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.