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来编写HTTP服务器的超级指南
Feb 18 Python
Python之os操作方法(详解)
Jun 15 Python
python 自动去除空行的实例
Jul 24 Python
Django中在xadmin中集成DjangoUeditor过程详解
Jul 24 Python
Python中调用其他程序的方式详解
Aug 06 Python
Python使用百度api做人脸对比的方法
Aug 28 Python
pygame实现俄罗斯方块游戏(基础篇1)
Oct 29 Python
keras 获取某层的输入/输出 tensor 尺寸操作
Jun 10 Python
基于python实现判断字符串是否数字算法
Jul 10 Python
Python sqlalchemy时间戳及密码管理实现代码详解
Aug 01 Python
Python3爬虫ChromeDriver的安装实例
Feb 06 Python
Python实现批量将文件复制到新的目录中再修改名称
Apr 12 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
php 无限级缓存的类的扩展
2009/03/16 PHP
php使用curl检测网页是否被百度收录的示例分享
2014/01/31 PHP
PHP获取浏览器信息类和客户端地理位置的2个方法
2014/04/24 PHP
php中实现精确设置session过期时间的方法
2014/07/17 PHP
php简单压缩css样式示例
2016/09/22 PHP
php mysql实现mysql_select_db选择数据库
2016/12/30 PHP
用Jquery实现滚动新闻
2014/02/12 Javascript
JavaScript中的this,call,apply使用及区别详解
2016/01/29 Javascript
值得分享和收藏的Bootstrap学习教程
2016/05/12 Javascript
JS 滚动事件window.onscroll与position:fixed写兼容IE6的回到顶部组件
2016/10/10 Javascript
js控制一个按钮是否可点击(可使用)disabled的实例
2017/02/14 Javascript
jQuery实现简单的抽奖游戏
2017/05/05 jQuery
jquery实现提示语淡入效果
2017/05/05 jQuery
利用js实现前后台传送Json的示例代码
2018/03/29 Javascript
使用p5.js实现动态GIF图片临摹重现
2019/10/23 Javascript
javascript自定义右键菜单插件
2019/12/16 Javascript
JS co 函数库的含义和用法实例总结
2020/04/08 Javascript
Python使用dis模块把Python反编译为字节码的用法详解
2016/06/14 Python
用Python实现随机森林算法的示例
2017/08/24 Python
django框架模板中定义变量(set variable in django template)的方法分析
2019/06/24 Python
python实现动态创建类的方法分析
2019/06/25 Python
详解Django 时间与时区设置问题
2019/07/23 Python
导入tensorflow:ImportError: libcublas.so.9.0 报错
2020/01/06 Python
PyTorch 解决Dataset和Dataloader遇到的问题
2020/01/08 Python
python nohup 实现远程运行不宕机操作
2020/04/16 Python
使用python matplotlib 画图导入到word中如何保证分辨率
2020/04/16 Python
Python基于数列实现购物车程序过程详解
2020/06/09 Python
matplotlib制作雷达图报错ValueError的实现
2021/01/05 Python
荷兰演唱会和体育比赛订票网站:viagogo荷兰
2018/04/08 全球购物
社区国庆节活动方案
2014/02/05 职场文书
中文教师求职信
2014/02/22 职场文书
教师学习中国梦心得体会
2016/01/05 职场文书
python使用PySimpleGUI设置进度条及控件使用
2021/06/10 Python
Java面试题冲刺第十五天--设计模式
2021/08/07 面试题
Mysql存储过程、触发器、事件调度器使用入门指南
2022/01/22 MySQL
通过Python把学姐照片做成拼图游戏
2022/02/15 Python