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之wxPython菜单使用详解
Sep 28 Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 Python
Python脚本处理空格的方法
Aug 08 Python
Django中间件实现拦截器的方法
Jun 01 Python
django2.0扩展用户字段示例
Feb 13 Python
在PyTorch中Tensor的查找和筛选例子
Aug 18 Python
Python从入门到精通之环境搭建教程图解
Sep 26 Python
Python倒排索引之查找包含某主题或单词的文件
Nov 13 Python
Pandas-Cookbook 时间戳处理方式
Dec 07 Python
python列表生成器迭代器实例解析
Dec 19 Python
Python类的绑定方法和非绑定方法实例解析
Mar 04 Python
Python3实现飞机大战游戏
Apr 24 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
如何分别全角和半角以避免乱码
2006/10/09 PHP
PHP实时显示输出
2008/10/02 PHP
PHP spl_autoload_register实现自动加载研究
2011/12/06 PHP
php中3种方法统计字符串中每种字符的个数并排序
2012/08/27 PHP
PHP读取配置文件类实例(可读取ini,yaml,xml等)
2015/07/28 PHP
PHP实现防盗链的方法分析
2017/07/25 PHP
浅析PHP数据导出知识点
2018/02/17 PHP
dojo 之基础篇(二)之从服务器读取数据
2007/03/24 Javascript
js计算页面刷新的次数
2009/07/20 Javascript
jQuery+css实现的蓝色水平二级导航菜单效果代码
2015/09/11 Javascript
写给小白的JavaScript引擎指南
2015/12/04 Javascript
JS加载器如何动态加载外部js文件
2016/05/26 Javascript
js实现适配不同的屏幕大小
2017/04/10 Javascript
基于js中this和event 的区别(详解)
2017/10/24 Javascript
iview tabs 顶部导航栏和模块切换栏的示例代码
2019/03/04 Javascript
浅谈js闭包理解
2019/04/01 Javascript
JS自定义滚动条效果
2020/03/13 Javascript
Vue使用预渲染代替SSR的方法
2020/07/02 Javascript
Python 正则表达式操作指南
2009/05/04 Python
Python使用gensim计算文档相似性
2016/04/10 Python
python中print的不换行即时输出的快速解决方法
2016/07/20 Python
python2.7 mayavi 安装图文教程(推荐)
2017/06/22 Python
Python序列化基础知识(json/pickle)
2017/10/19 Python
numpy.ndarray 交换多维数组(矩阵)的行/列方法
2018/08/02 Python
Python求解正态分布置信区间教程
2019/11/20 Python
selenium中get_cookies()和add_cookie()的用法详解
2020/01/06 Python
python中图像通道分离与合并实例
2020/01/17 Python
利用python绘制数据曲线图的实现
2020/04/09 Python
竞选演讲稿范文
2013/12/28 职场文书
竞选卫生委员演讲稿
2014/04/28 职场文书
搞笑爱情保证书
2014/04/29 职场文书
社会工作专业自荐信
2014/09/26 职场文书
工地材料员岗位职责
2015/04/11 职场文书
2015年煤矿工作总结
2015/04/28 职场文书
写给老师的保证书
2015/05/09 职场文书
SQL Server代理:理解SQL代理错误日志处理方法
2021/06/30 SQL Server