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中while true的使用
Oct 13 Python
python 性能提升的几种方法
Jul 15 Python
Python连接DB2数据库
Aug 27 Python
利用python解决mysql视图导入导出依赖的问题
Dec 17 Python
详解pandas安装若干异常及解决方案总结
Jan 10 Python
详解Python学习之安装pandas
Apr 16 Python
Pytorch之卷积层的使用详解
Dec 31 Python
PyQt5+python3+pycharm开发环境配置教程
Mar 24 Python
Python unittest discover批量执行代码实例
Sep 08 Python
python 如何利用argparse解析命令行参数
Sep 11 Python
Python实现列表索引批量删除的5种方法
Nov 16 Python
Python 实现键盘鼠标按键模拟
Nov 18 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
关于Appserv无法打开localhost问题的解决方法
2009/10/16 PHP
php生成略缩图代码
2012/07/16 PHP
PHP的魔术常量__METHOD__简介
2014/07/08 PHP
phpnow php探针环境检测代码
2014/11/04 PHP
php实现用于计算执行时间的类实例
2015/04/18 PHP
node.js中的fs.lchmod方法使用说明
2014/12/16 Javascript
jQuery多级弹出菜单插件ZoneMenu
2014/12/18 Javascript
javascript 动态创建表格
2015/01/08 Javascript
调试JavaScript中正则表达式中遇到的问题
2015/01/27 Javascript
nw.js实现类似微信的聊天软件
2015/03/16 Javascript
在JavaScript中使用JSON数据
2016/02/15 Javascript
JavaScript函数柯里化详解
2016/04/29 Javascript
JavaScript省市区三级联动菜单效果
2016/09/21 Javascript
纯JavaScript手写图片轮播代码
2016/10/20 Javascript
JS中作用域和变量提升(hoisting)的深入理解
2016/10/31 Javascript
浅析JavaScript的几种Math函数,random(),ceil(),round(),floor()
2016/12/22 Javascript
jQuery中map函数的两种方式
2017/04/07 jQuery
ES6新特性五:Set与Map的数据结构实例分析
2017/04/21 Javascript
详解bootstrap用dropdown-menu实现上下文菜单
2017/09/22 Javascript
vue实现短信验证码登录功能(流程详解)
2019/12/10 Javascript
详解Vue中的MVVM原理和实现方法
2020/07/15 Javascript
python中sleep函数用法实例分析
2015/04/29 Python
利用PyInstaller将python程序.py转为.exe的方法详解
2017/05/03 Python
python导出hive数据表的schema实例代码
2018/01/22 Python
PyQt5响应回车事件的方法
2019/06/25 Python
Django中自定义模型管理器(Manager)及方法
2019/09/23 Python
对python中 math模块下 atan 和 atan2的区别详解
2020/01/17 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
Django Admin 上传文件到七牛云的示例代码
2020/06/20 Python
canvas简单连线动画的实现代码
2020/02/04 HTML / CSS
社区文化建设方案
2014/05/02 职场文书
圣诞节活动策划方案
2014/06/09 职场文书
五五普法心得体会
2014/09/04 职场文书
高中生第一学年自我鉴定2015
2014/09/28 职场文书
2019年中,最受大众欢迎的6本新书
2019/08/07 职场文书
vue项目支付功能代码详解
2022/02/18 Vue.js