监控一个服务端口

监控telnet服务的端口

#!/bin/bash

# monitor methods
#1) systemctl service 
#2) lsof -i :portno
#3) ps aux | grep PROCESS_NAME
#### press large 
#4) Inspect port response  >>recommand

#telnet 

#main
port_status () {
temp_file=`mktemp port_status.XXX`
#1
[ ! -x /usr/bin/telnet ]&&echo "telnet: not found command"&& exit 1
#2
(telnet $1 $2 <<EOF
quit
EOF
) &>$temp_file

if egrep "^]" $temp_file &>/dev/null;then
  echo "$1 $2 is open"
else
  echo "$1 $2 is close"
fi
rm -rf $temp_file
}


port_status $1 $2

原文地址:https://www.cnblogs.com/persisit/p/13690601.html