Python multi-thread detection of server online conditions and python online Conditions

Source: Internet
Author: User

Python multi-thread detection of server online conditions and python online Conditions

You need to ping the online status of all machines in a CIDR block. The shell takes too long to run. Use python to write a multi-thread ping command. The Code is as follows:

#! /Usr/bin/python # coding = UTF-8 ''' Created on 2015-8-4 @ author: Administrator ''' import threading, subprocessfrom time import ctime, sleep, timeimport Queuequeue = Queue. queue () class ThreadUrl (threading. thread): def _ init _ (self, queue): threading. thread. _ init _ (self) self. queue = queue def run (self): while True: host = self. queue. get () ret = subprocess. call ('Ping-c 1-w 1' + host, shell = True, stdout = open ('/dev/null', 'w') if ret: print "% s is down" % host else: print "% s is up" % host self. queue. task_done () def main (): for I in range (100): t = ThreadUrl (queue) t. setDaemon (True) t. start () for host in B: queue. put (host) queue. join () a = [] with open('ip.txt ') as f: for line in f. readlines ():. append (line. split () [0]) # print AB = ['2017. 168. 3. '+ str (x) for x in range (1,254)] # ping 192.168.3 network segment start = time () main () print "Elasped Time: % s" % (time () -start) # t2 = threading. thread (target = move, args = ('fff',) # threads. append (t2) ''' for I in a: print ctime () ping (I) sleep (1) if _ name _ = '_ main __': for t in range (len (a): # t. setDaemon (True) threads [t]. start () # t. join () print "All over % s" % ctime ()'''

The effect is as follows:

It only takes about seconds for a single CIDR block to be pinged. It's fast enough !!!

I will share with you a method and code for detecting Internet servers.

Python is often used to check whether the server can be pinged and whether the program runs properly (check whether the corresponding port is normal)

Previously, shell scripts were written as follows:

PINGRET = $ (ping www.baidu.com-c 2 | grep "icmp _"); if [-z $ PINGRET]; then echo "ping fail"; else echo "ping OK"; fi

Or

Ping-c 2 www.baidu.com | grep "icmp _" & echo 'Ping OK '| echo 'Ping fail'

Sample Code:

#! /Usr/bin/python # encoding = UTF-8 # Filename: net_is_normal.pyimport osimport socketimport subprocess # determine whether the network is normal server = 'www .baidu.com '# Check whether the server can be pinged. When the program is running, def pingServer (server): result = OS. system ('ping' + server + '-c 2') if result: print' server % s ping fail '% server else: print 'server % s ping OK '% server print result # locate the program output to/dev/null, otherwise, the command running information def pingServerCall (server): f will be displayed in the standard output when the program is running. Null = open (OS. devnull, 'w') result = subprocess. call ('ping' + server + '-c 2', shell = True, stdout = fnull, stderr = fnull) if result: print 'server % s ping fail' % server else: print 'server % s ping OK' % server fnull. close () # It can be used to check whether the program is normal. For example, to check whether redis is normal, that is, to check whether port 6379 of redis is normal # to check whether ssh is normal, check whether port 22 of ssh is normal def check_aliveness (ip, port): sk = socket. socket (socket. AF_INET, socket. SOCK_STREAM) sk. settimeout (1) try: sk. Connect (ip, port) print 'server % s % d service is OK! '% (Ip, port) return True failed t Exception: print' server % s % d service is not OK! '% (Ip, port) return False finally: sk. close () return False if _ name __= = '_ main _': pingServerCall (server) pingServer (server) check_aliveness ('2017. 168.230.128 ', 6379)

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.