此脚本支持显示同一个服务占用多个端口的情况
1.脚本内容
[root@ysw ~]# cat auto_discovery_server_port.sh
#!/bin/bash
#zabbix LLD 自动发现脚本
#作者:醉眼看人间,个个都温柔
#功能:自动发现服务器端口和端口服务名称
portarray=(`/usr/bin/netstat -tnpl|grep -v '@pts\|cpus\|master'|awk '/^tcp/{sub(/.*:/,"",$4);sub(/[0-9]*\//,"",$7);if(NF==7)print $4,$7;else print $4,$7,$NF}'|sed 's/[ ][ ]*/:/g'|sort -n |uniq`)
length=${#portarray[@]}
echo "{"
echo " \"data\":["
for ((i=0;i<$length;i++))
do
PORT_NUM=`echo ${portarray[$i]} | awk -F : '{print $1}'`
PORT_NAME=`echo ${portarray[$i]} | awk -F : '{print $2$3}'`
echo ' {'
echo " \"{#PORT_NUM}\":\"${PORT_NUM}\","
echo " \"{#PORT_NAME}\":\"${PORT_NAME}\""
if [ $i -lt $[$length-1] ];then
echo ' },'
fi
done
echo ' }'
echo " ]"
echo "}"
[root@ysw ~]#
2.脚本执行结果
[root@ysw ~]# sh auto_discovery_server_port.sh
{
"data":[
{
"{#PORT_NUM}":"21",
"{#PORT_NAME}":"vsftpd"
},
{
"{#PORT_NUM}":"22",
"{#PORT_NAME}":"sshd"
},
{
"{#PORT_NUM}":"2222",
"{#PORT_NAME}":"sshd"
},
{
"{#PORT_NUM}":"6379",
"{#PORT_NAME}":"redis-server1"
},
{
"{#PORT_NUM}":"10050",
"{#PORT_NAME}":"zabbix_agentd"
},
{
"{#PORT_NUM}":"41000",
"{#PORT_NAME}":"mongod"
}
]
}
[root@ysw ~]#
网友评论