中国婚恋网站排名,鑫瀚通网站建设,销售平台网站建设方案,网站建设需要几个人本文主要向大家介绍了Python语言 实现端口连通性检测#xff0c;通过具体的内容向大家展示#xff0c;希望对大家学习Python语言有所帮助。# -*- coding: utf-8 -*-#!/bin/env python#AUTHOR:karl#DATE:2018-1-19#VERSION:V1.0######################import timeimport osimp…本文主要向大家介绍了Python语言 实现端口连通性检测通过具体的内容向大家展示希望对大家学习Python语言有所帮助。# -*- coding: utf-8 -*-#!/bin/env python#AUTHOR:karl#DATE:2018-1-19#VERSION:V1.0######################import timeimport osimport paramikoimport datetimeimport sysimport MySQLdbimport threading########################################首次插入数据是REG0更新数据REG为其他#######################################REG1private_key paramiko.RSAKey.from_private_key_file(/home/appdeploy/.ssh/id_rsa)def Data_mysql(info):try:stringinfoPipstring.split( )[0]Sipstring.split( )[1]Dipstring.split( )[2]Portstring.split( )[3]Resultstring.split( )[4].replace(\n,)con MySQLdb.connect(hostlocalhost,userroot,passwd******,dbzabbix,port3306,charsetutf8)dt datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S)cursorcon.cursor()valuse(dt,Result,Pip,Sip,Dip,Port)if REG 0:sqlINSERT INTO Balant_telnet(create_time,telnet_result,telnet_physicalip,telnet_sourceip,telnet_desip,telnet_port)VALUES (%s,%s,%s,%s,%s,%s) resultcursor.execute(sql,valuse)else:sqlUPDATE Balant_telnet setcreate_time%s,telnet_result%s where telnet_physicalip%s andtelnet_sourceip%s and telnet_desip%s and telnet_port%s;%(valuse)resultcursor.execute(sql)cursor.close()con.commit()con.close()except MySQLdb.Error, e:print Error %d: %s % (e.args[0], e.args[1])sys.exit(1)def check_port(value,content):ager{}ager[value]contentsshparamiko.SSHClient()try:ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(value,port22,usernameappdeploy,pkeyprivate_key,timeout5)cmdsh /home/appdeploy/monitor.sh {value}.format(valueager[ager.keys()[index]])stdin,stdout,stderr ssh.exec_command(cmd)for file_d in stdout.readlines():Data_mysql(file_d)except:resultcmd,failed\nssh.close()#################################### 对配置文件进行参数解析###################################def get_parameter():ager{}value[]with open(monitor_config) as context:for line in context:while not , in list(line):hostline.strip()[1:-1]value[]breaklineline.strip().split(,)valuevaluelineager[str(host)]valuereturn agerif __name__ __main__:threads[]parmget_parameter()filesrange(len(parm.keys()))for index,content in parm.items():t threading.Thread(targetcheck_port,args(index,content))threads.append(t)for index in files:threads[index].start()for index in files:threads[index].join()配置文件[10.117.194.23]10.117.194.77,10.116.41.82,992010.117.194.77,10.116.47.12,108010.117.194.77,10.116.45.56,1081[10.117.194.24]10.117.194.78,10.116.41.82,992010.117.194.78,10.116.47.12,108010.117.194.78,10.116.45.56,108110.117.194.78,10.116.145.33,8001【】里面的IP 是物理IP下面分别对应着应用的源IP目标IP和测试端口号agent 直接用shell 实现.数据库中效果在zabbix 中 加入对表中数据的监控。在zabbix 的mysql 模板中加入Balant 的监控项也可以加入图像我们最后的结果是要展示在grafana 上进行展示。为1就说明应用端口是正常运行。本文由职坐标整理并发布希望对同学们学习Python有所帮助更多内容请关注职坐标编程语言Python频道