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中类的继承代码实例
Oct 28 Python
Python使用MONGODB入门实例
May 11 Python
Python实现对一个函数应用多个装饰器的方法示例
Feb 09 Python
python实现淘宝秒杀脚本
Jun 23 Python
Python使用字典的嵌套功能详解
Feb 27 Python
Python对列表的操作知识点详解
Aug 20 Python
Python 转换RGB颜色值的示例代码
Oct 13 Python
使用Python进行中文繁简转换的实现代码
Oct 18 Python
Python FtpLib模块应用操作详解
Dec 12 Python
python能在浏览器能运行吗
Jun 17 Python
为了顺利买到演唱会的票用Python制作了自动抢票的脚本
Oct 16 Python
彻底弄懂Python中的回调函数(callback)
Jun 25 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
新的一年,新的期待:DC在2020年的四部动画电影
2020/01/01 欧美动漫
PHP 通过Socket收发十六进制数据的实现代码
2013/08/16 PHP
php生成缩略图示例代码分享(使用gd库实现)
2014/01/20 PHP
php实现的递归提成方案实例
2015/11/14 PHP
PHP MYSQL简易交互式站点开发
2016/12/27 PHP
laravel 解决groupBy时出现的错误 isn't in Group By问题
2019/10/17 PHP
Firefox中beforeunload事件的实现缺陷浅析
2012/05/03 Javascript
用jQuery获取IE9下拉框默认值问题探讨
2013/07/22 Javascript
js转义字符介绍
2013/11/05 Javascript
javascript去掉前后空格的实例
2013/11/07 Javascript
Node.js中HTTP模块与事件模块详解
2014/11/14 Javascript
解决angular的post请求后SpringMVC后台接收不到参数值问题的方法
2015/12/10 Javascript
js转换对象为xml
2017/02/17 Javascript
jquery仿京东侧边栏导航效果
2017/03/02 Javascript
基于vue的fullpage.js单页滚动插件
2017/03/20 Javascript
Angular.js中上传指令ng-upload的基本使用教程
2017/07/30 Javascript
react实现菜单权限控制的方法
2017/12/11 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
vue:el-input输入时限制输入的类型操作
2020/08/05 Javascript
vue脚手架项目创建步骤详解
2021/03/02 Vue.js
深入分析在Python模块顶层运行的代码引起的一个Bug
2014/07/04 Python
python字符串连接方式汇总
2014/08/21 Python
使用Python编写简单网络爬虫抓取视频下载资源
2014/11/04 Python
Python map和reduce函数用法示例
2015/02/26 Python
Django的信号机制详解
2017/05/05 Python
Python解析json时提示“string indices must be integers”问题解决方法
2019/07/31 Python
Python Process多进程实现过程
2019/10/22 Python
OpenCV中VideoCapture类的使用详解
2020/02/14 Python
next在python中返回迭代器的实例方法
2020/12/15 Python
selenium学习教程之定位以及切换frame(iframe)
2021/01/04 Python
CSS3打造磨砂玻璃背景效果
2016/09/28 HTML / CSS
环境科学专业研究生求职信
2013/10/02 职场文书
干部培训自我鉴定
2014/01/22 职场文书
责任胜于能力演讲稿
2014/05/20 职场文书
如何写求职信
2014/05/24 职场文书
小学一年级语文教学反思
2016/03/03 职场文书