Python implements methods for detecting server/module deployments based on specified ports _python

Source: Internet
Author: User

This article illustrates how Python implementations are very useful for detecting server/module deployments based on specified ports. Share for everyone for reference.

Sometimes, the number of servers in the maintenance process is very large. Application modules are deployed on different servers. Sometimes the maintainer does a module migration, but not synchronized to the manual in time. Finding is more difficult. As a result, Python detects a module deployment based on the application port.

The idea is very simple: through simple TCP links, if you can successfully build, disconnect immediately to prevent the impact of the business. Indicates that the module has a deployment on a server.

The specific function code is as follows:

#!/bin/env Python # import socket import time from threading import Thread hostlist=["10.10.126.170", "10.10.126.173", "10 .10.126.177 "," 10.10.126.170 "," 10.10.126.173 "," 10.10.126.177 "] online=[] offline=[] gathered=[] hostdict={" OnLine ": 
 [], ' offLine ': []} class Detect (Thread): Def __init__ (Self,ip, port=22): thread.__init__ (self) self.ip=ip self.port=port def run (self): address= (Self.ip,self.port) sock=socket.socket (socket.af_inet, socket. SOCK_STREAM) Try:sock.connect (address) BUFF=SOCK.RECV (1024) if (len (Buff)): Print ("Detect Host%s Online"% self. IP) online.append (SELF.IP) except:print ("Detect Host%s OffLine"% Self.ip) offline.append (SELF.IP) Sock.close D EF sigle_detect (IP): P=detect (IP) p.start () P.join () def multi_detect (host): t_thread=[] for IP in Set (host): T=d 
 Etect (IP) t.name=ip t.start () t_thread.append (t) for T in T_thread:t.join (m) def filter_gather (hlist): gather=[] For T in Set (hlist): Gather.append (t) return gather def Mak_HOSTLIST_BYIP3 (iplist): Global hostList hostlist=[] for IP in Set (iplist): Tmp=ip.split ('. ')
 if (LEN (TMP) ==3): For I in Range (2,254): Hostlist.append ('%s.%d '% (IP, i)) elif (LEN (TMP) ==4): Hostlist.append (IP) Else:continue return hostList def update_hostdict (online, offLine): hostdict["Online"]=online hostdict["OffLine"]=of Fline def make_pickle_filename (): Import Time Filename= "" for S in Time.localtime () [: 5]: Filename=filename+str (s) fi Lename= "HOST_%S.PKL"% filename return filename def save_gathered (filename, hostdict): Import pickle f=open (filename, ' WB ') Pickle.dump (hostdict,f) f.close () def recovery_gathered (filename, keylist): Import pickle try:f=open (filename, ' RB ') E=pickle.load (f) keylist.append (E) Except:f.close () return while E:try:e=pickle.load (f) keylist.append ( E) Except:f.close () Break if __name__== ' __main__ ': Sigle_detect (hostlist[0]) #---------------MAK_HOSTLIST_BYIP3 (hostList) Multi_detect (hostList) Online=filteR_gather (online) print (online) offline=filter_gather (offLine) print (offLine) gathered=online+offline print (gathered ) update_hostdict (OnLine, offLine) print (hostdict) fn=make_pickle_filename () save_gathered (fn,hostdict) keylist=[] R

 Ecovery_gathered (fn,keylist) print (keylist)

Hopefully this article will help you with your Python programming.

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.