#!/bin/env python#author:karl#date:2018-1-3#version:v1.0##################### #import timeimport osimport paramikoimport datetimeimport sysimport MySQLdbprivate_key = Paramiko. Rsakey.from_private_key_file ('/home/appdeploy/.ssh/id_rsa ') def timestamptotime (timestamp): timestruct = time.localtime (timestamp) return time.strftime ( '%y-%m-%d %h:%m:%s ', timestruct) def data_mysql (info): try: string=info sip=string.split (" ") [0] dip=string.split (" ") [1] port=string.split (" ") [2] Result=string.split (" ") [3] con = MySQLdb.connect (host= ' localhost ', user="Root", passwd= "********", db= "Zabbix", port=3306,charset= "UTF8") Dt = datetime.datetime.now (). Strftime ("%y-%m-%d %h:%m:%s") cursor=con.cursor () sql= "" "INSERT INTO balant_telnet (Telnet_sourceip,telnet_desip,telnet_port,create_time,telnet_result) VALUES (%s,% s,%s,%s,%s) "" valuse= (Sip,dip,port,dt,result) result=cursor.execute (Sql,valuse) cursor.close () con.commit () con.close () except MySQLdb.Error, e: print "error %d: %s" % (e.args[0], E.ARGS[1])    &NBSp; sys.exit (1) def check_port (ager): for index in range (Len (Ager.keys ())): hostname=ager.keys () [index] ssh=paramiko. Sshclient () try: ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) ssh.connect (hostname, Port=22,username= ' Appdeploy ', pkey=private_key,timeout=5) cmd= "Sh /home/appdeploy/monitor.sh {value}". Format (Value=ager[ager.keys () [index]] stdin,stdout,stderr = Ssh.exec_command (CMD) for file_d&Nbsp;in stdout.readlines (): print file_d data_mysql (file_d) except: result=cmd+ ', ' + ' failed ' + ' \ n ' ssh.close () Def get_parameter (): ager={} value=[] with open ("Monitor_config") as context: for line in context: while not "," in list (line): host=line.strip () [1:-1] value=[] break line=line.strip (). Split (",") value=value+line   AGER[STR (host)]=value return agerparm=get_parameter () Check_port (parm)
Configuration file:
[10.117.194.23]
10.117.194.77,10.116.41.82,9920
10.117.194.77,10.116.47.12,1080
10.117.194.77,10.116.45.56,1081
[10.117.194.24]
10.117.194.78,10.116.41.82,9920
10.117.194.78,10.116.47.12,1080
10.117.194.78,10.116.45.56,1081
10.117.194.78,10.116.145.33,8001
The IP inside is the physical IP, which corresponds to the source IP of the application, the destination IP, and the test port number, respectively.
The agent is implemented directly with the shell
Effects in the database
Finally, through the Zabbix API parsing can be displayed.
Python implementation of Port connectivity detection