当前位置:Gxlcms > Python > Python实现根据指定端口探测服务器/模块部署的方法

Python实现根据指定端口探测服务器/模块部署的方法

时间:2021-07-01 10:21:17 帮助过:15人阅读

本文实例讲述了Python实现根据指定端口探测服务器/模块部署的方法,非常具有实用价值。分享给大家供大家参考借鉴。

有些时候,在维护过程中,服务器数量非常多。应用模块部署在不同服务器上。有时维护人员做了模块迁移,而未及时同步至手册中。查找比较困难。于是,产生Python根据应用端口进行探测,获取模块部署。

设想非常简单:通过简单的tcp链接,如果能够成功的建立,立即断开,防止影响业务。表示模块在某服务器上有部署。

具体功能代码如下:

  1. #!/bin/env python
  2. #
  3. import socket
  4. import time
  5. from threading import Thread
  6. 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"]
  7. onLine=[]
  8. offLine=[]
  9. gathered=[]
  10. hostDict={"onLine":[],"offLine":[]}
  11. class detect(Thread):
  12. def __init__(self,ip, port=22):
  13. Thread.__init__(self)
  14. self.ip=ip
  15. self.port=port
  16. def run(self):
  17. address=(self.ip,self.port)
  18. sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. try:
  20. sock.connect(address)
  21. buff=sock.recv(1024)
  22. if(len(buff)):
  23. print("detect Host %s Online" % self.ip)
  24. onLine.append(self.ip)
  25. except:
  26. print("detect Host %s OffLine" % self.ip)
  27. offLine.append(self.ip)
  28. sock.close
  29. def sigle_detect(ip):
  30. p=detect(ip)
  31. p.start()
  32. p.join(60)
  33. def multi_detect(host):
  34. T_thread=[]
  35. for ip in set(host):
  36. t=detect(ip)
  37. t.name=ip
  38. t.start()
  39. T_thread.append(t)
  40. for t in T_thread:
  41. t.join(15)
  42. def filter_gather(hlist):
  43. gather=[]
  44. for t in set(hlist):
  45. gather.append(t)
  46. return gather
  47. def mak_hostList_byip3(iplist):
  48. global hostList
  49. hostList=[]
  50. for ip in set(iplist):
  51. tmp=ip.split('.')
  52. if(len(tmp)==3):
  53. for i in range(2,254):
  54. hostList.append('%s.%d' % (ip, i))
  55. elif(len(tmp)==4):
  56. hostList.append(ip)
  57. else:
  58. continue
  59. return hostList
  60. def update_hostDict(onLine, offLine):
  61. hostDict["onLine"]=onLine
  62. hostDict["offLine"]=offLine
  63. def make_pickle_fileName():
  64. import time
  65. fileName=""
  66. for s in time.localtime()[:5]:
  67. fileName=fileName+str(s)
  68. fileName="Host_%s.pkl" % fileName
  69. return fileName
  70. def save_gathered(fileName, hostDict):
  71. import pickle
  72. F=open(fileName,'wb')
  73. pickle.dump(hostDict,F)
  74. F.close()
  75. def recovery_gathered(fileName, keyList):
  76. import pickle
  77. try:
  78. F=open(fileName,'rb')
  79. E=pickle.load(F)
  80. keyList.append(E)
  81. except:
  82. F.close()
  83. return
  84. while E:
  85. try:
  86. E=pickle.load(F)
  87. keyList.append(E)
  88. except:
  89. F.close()
  90. break
  91. if __name__=='__main__':
  92. sigle_detect(hostList[0])
  93. #---------------
  94. mak_hostList_byip3(hostList)
  95. multi_detect(hostList)
  96. onLine=filter_gather(onLine)
  97. print(onLine)
  98. offLine=filter_gather(offLine)
  99. print(offLine)
  100. gathered=onLine+offLine
  101. print(gathered)
  102. update_hostDict(onLine, offLine)
  103. print(hostDict)
  104. fN=make_pickle_fileName()
  105. save_gathered(fN,hostDict)
  106. keyList=[]
  107. recovery_gathered(fN,keyList)
  108. print(keyList)

希望本文讲述的方法对大家的Python程序设计有所帮助。

人气教程排行