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 相关文章推荐
pygame学习笔记(5):游戏精灵
Apr 15 Python
Python中set与frozenset方法和区别详解
May 23 Python
python模块之re正则表达式详解
Feb 03 Python
Python使用try except处理程序异常的三种常用方法分析
Sep 05 Python
python实现播放音频和录音功能示例代码
Dec 30 Python
Python修改文件往指定行插入内容的实例
Jan 30 Python
python机器人运动范围问题的解答
Apr 29 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
Jul 22 Python
Python响应对象text属性乱码解决方案
Mar 31 Python
Python 如何批量更新已安装的库
May 26 Python
python爬虫构建代理ip池抓取数据库的示例代码
Sep 22 Python
Python3.7安装PyQt5 运行配置Pycharm的详细教程
Oct 15 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
Discuz! Passport 通行证整合
2008/03/27 PHP
有关DOM元素与事件的3个谜题
2010/11/11 Javascript
玩转方法:call和apply
2014/05/08 Javascript
jQuery固定元素插件scrolltofixed使用指南
2015/04/21 Javascript
JS+CSS实现带有碰撞缓冲效果的竖向导航条代码
2015/09/15 Javascript
jQuery深拷贝Json对象简单示例
2016/07/06 Javascript
微信小程序 LOL 英雄介绍开发实例
2016/09/30 Javascript
JS前向后瞻正则表达式定义与用法示例
2016/12/27 Javascript
薪资那么高的Web前端必看书单
2017/10/13 Javascript
layui实现动态和静态分页
2018/04/28 Javascript
深入Node TCP模块的理解
2019/03/13 Javascript
如何基于JS截获动态代码
2019/12/25 Javascript
小程序接入腾讯位置服务的详细流程
2020/03/03 Javascript
Vue中父子组件的值传递与方法传递
2020/09/28 Javascript
简单谈谈offsetleft、offsetTop和offsetParent
2020/12/04 Javascript
[59:26]DOTA2上海特级锦标赛D组资格赛#1 EG VS VP第二局
2016/02/28 DOTA
详解Python中的条件判断语句
2015/05/14 Python
Python文件右键找不到IDLE打开项解决办法
2015/06/08 Python
python实现在字符串中查找子字符串的方法
2015/07/11 Python
python安装与使用redis的方法
2016/04/19 Python
Python实现求解括号匹配问题的方法
2018/04/17 Python
Django项目实战之用户头像上传与访问的示例
2018/04/21 Python
Python中免验证跳转到内容页的实例代码
2020/10/23 Python
Julep官网:美容产品和指甲油
2017/02/25 全球购物
New Era英国官网:美国棒球帽品牌
2018/03/21 全球购物
配置管理计划的主要内容有哪些
2014/06/20 面试题
公司人力资源的自我评价
2014/01/02 职场文书
应届中专生自荐书范文
2014/02/13 职场文书
酒店开业策划方案
2014/06/02 职场文书
学校爱国卫生月活动总结
2014/06/25 职场文书
小学生光盘行动倡议书
2015/04/28 职场文书
教师文明餐桌光盘行动倡议书
2015/04/28 职场文书
2015年社区消防安全工作总结
2015/10/14 职场文书
小学生班干部竞选稿
2015/11/20 职场文书
2016消防宣传标语口号
2015/12/26 职场文书
mysql数据插入覆盖和时间戳的问题及解决
2022/03/25 MySQL