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

Source: Internet
Author: User
The examples in this paper describe how Python implements the method of probing server/module deployment based on the specified port, which is of great practical value. Share to everyone for reference.

There are times when there are a lot of servers in the maintenance process. The application modules are deployed on different servers. Sometimes maintenance personnel do the module migration, but not in time to synchronize to the manual. Finding is more difficult. As a result, Python detects the application port and gets the module deployment.

The idea is very simple: through simple TCP links, if you can successfully build, immediately disconnect, 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 socketimport timefrom Threading Import threadhostlist=["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 (1024x768) if (len): Print ("Detect Host%s Online"% Self.ip) Online.append (SELF.IP) except:print ("Detect Host%s OffLine"% Self.ip) offline.append (SELF.IP) sock.closedef Sigle_det  ECT (IP): P=detect (IP) p.start () P.join (+) def multi_detect (host): t_thread=[] for IP in Set (host): T=detect (IP) t.name=ip T.start () t_thread.append (t) for T in T_thread:t.join (+) def filter_gather (hlist): gather=[] for T in Set (hlist): gathe R.append (t) return gatherdef 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 hostlistdef update_hostdict (OnLine, offLine): hostdict[" OnLine "]=online hostdict[" OffLine "]=offlinedef make_pickle_filename (): Import Time Filename=" "for S in Time.localtime ( ) [: 5]: Filename=filename+str (s) filename= "host_%s.pkl"% fileName return filenamedef 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=pick Le.load (F) keylist.append (E) except:f.close () breakif __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=[] recovery_gathered (fn,keylist) print (keylist)

It is hoped that the method described in this article will help you with 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.