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


Posted in Python onAugust 25, 2014

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

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

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

具体功能代码如下:

#!/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

def sigle_detect(ip):
 p=detect(ip)
 p.start()
 p.join(60)

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(15)
 
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"]=offLine

def make_pickle_fileName():
 import time
 fileName=""
 for s in time.localtime()[:5]:
 fileName=fileName+str(s)
 fileName="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=[]
 recovery_gathered(fN,keyList)
 print(keyList)

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

Python 相关文章推荐
举例讲解Python程序与系统shell交互的方式
Apr 09 Python
Python用 KNN 进行验证码识别的实现方法
Feb 06 Python
Python实现获取本地及远程图片大小的方法示例
Jul 21 Python
Python3 SSH远程连接服务器的方法示例
Dec 29 Python
python基础梳理(一)(推荐)
Apr 06 Python
python实现键盘输入的实操方法
Jul 16 Python
python 实现矩阵按对角线打印
Nov 29 Python
Python实现图像去噪方式(中值去噪和均值去噪)
Dec 18 Python
使用python的turtle函数绘制一个滑稽表情
Feb 28 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
python 爬虫如何实现百度翻译
Nov 16 Python
Python合并多张图片成PDF
Jun 09 Python
python的类变量和成员变量用法实例教程
Aug 25 #Python
Python写的创建文件夹自定义函数mkdir()
Aug 25 #Python
Python中的startswith和endswith函数使用实例
Aug 25 #Python
Python socket.error: [Errno 98] Address already in use的原因和解决方法
Aug 25 #Python
Python对小数进行除法运算的正确方法示例
Aug 25 #Python
Python实现的一个自动售饮料程序代码分享
Aug 25 #Python
Python中请使用isinstance()判断变量类型
Aug 25 #Python
You might like
微信公众平台开发之配置与请求
2015/08/26 PHP
非常重要的php正则表达式详解
2016/01/04 PHP
CI框架整合smarty步骤详解
2016/05/19 PHP
php 防止表单重复提交两种实现方法
2016/11/03 PHP
thinkPHP5实现数据库添加内容的方法
2017/10/25 PHP
CutePsWheel javascript libary 控制输入文本框为可使用滚轮控制的js库
2010/02/07 Javascript
z-blog SyntaxHighlighter 长代码无法换行解决办法(jquery)
2014/11/16 Javascript
jQuery中的编程范式详解
2014/12/15 Javascript
js实现a标签超链接提交form表单的方法
2015/06/24 Javascript
jquery.fastLiveFilter.js实现输入自动过滤的方法
2015/08/11 Javascript
移动端H5开发 Turn.js实现很棒的翻书效果
2016/06/20 Javascript
通过扫描二维码打开app的实现代码
2016/11/10 Javascript
BootStrap实现鼠标悬停下拉列表功能
2017/02/17 Javascript
Vue实现virtual-dom的原理简析
2017/07/10 Javascript
js实现简易聊天对话框
2017/08/17 Javascript
highcharts 在angular中的使用示例代码
2017/09/20 Javascript
js canvas实现红包照片效果
2018/08/21 Javascript
Vue 数据绑定的原理分析
2020/11/16 Javascript
使用Python编写Linux系统守护进程实例
2015/02/03 Python
python数组过滤实现方法
2015/07/27 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
2018/01/08 Python
Python 3.8中实现functools.cached_property功能
2019/05/29 Python
python数据预处理方式 :数据降维
2020/02/24 Python
什么是Python包的循环导入
2020/09/08 Python
Python 生成短8位唯一id实战教程
2021/01/13 Python
CSS3 transform的skew属性值图文详解
2014/07/21 HTML / CSS
Html5移动端获奖无缝滚动动画实现示例
2018/06/25 HTML / CSS
美国在线纱线商店:Darn Good Yarn
2019/03/20 全球购物
教师一岗双责责任书
2014/04/16 职场文书
合作协议书怎么写
2014/04/18 职场文书
小学生期末评语
2014/04/21 职场文书
社区两委对照检查材料
2014/08/23 职场文书
销售顾问工作计划书
2014/09/15 职场文书
幼儿园中班教师个人工作总结
2015/02/06 职场文书
学习与创新自我评价
2015/03/09 职场文书
2015年行政人事工作总结
2015/05/21 职场文书