Linux USES the nc command to monitor the server port

  • 2020-05-07 20:46:05
  • OfStack

Recently, I encountered a project. The front end was released with apache htttpd (port 80), which was forwarded to two tomcat (ports 8081 and 8082) for processing through dual-machine load balancing. Now I need to monitor the situation of these three ports at any time. Batch system monitoring is better to use nagios software to achieve, so a small project dedicated to install one nagios software, a bit cumbersome. Looked up 1 some data on the net, summarized the experiment 1, can use the simple nc command to achieve.

The 1.nc command detects port usage
# nc   -v   -w 10 %IP%     -z   %PORT%
-v   displays instruction execution.
-w   < Timeout seconds >     sets the time to wait for a connection.
-u   means using the UDP protocol
-z   USES 0 I/o mode and is only used when scanning communication ports.

example 1: scan the specified port 8080


# nc -v -w 10 -z 192.168.0.100 8080  
Connection to 192.168.0.100 8080 port [tcp/http] succeeded!

example 2: scan a port range of 20 to 25 and print in detail.


# nc -v -w 2 -z 192.168.0.100 20-25   

nc: connect to 192.168.0.100 port 20 (tcp) failed: Connection refused
nc: connect to 192.168.0.100 port 21 (tcp) failed: Connection refused
Connection to 192.168.0.100 22 port [tcp/ssh] succeeded!
nc: connect to 192.168.0.100 port 23 (tcp) failed: Connection refused
nc: connect to 192.168.0.100   port 24 (tcp) failed: Connection refused
nc: connect to 192.168.0.100 port 25 (tcp) failed: Connection refused

example 3: scan the port range from 1 to 65535 and output only the open ports (remove the -v parameter)


# nc -w 1 -z 192.168.0.100 1-65535 

Connection to 192.168.0.100 22 port [tcp/ssh] succeeded!
Connection to 192.168.0.100 80 port [tcp/http] succeeded!
Connection to 192.168.0.100 2121 port [tcp/scientia-ssdb] succeeded!
Connection to 192.168.0.100 4004 port [tcp/pxc-roid] succeeded!
Connection to 192.168.0.100 8081 port [tcp/tproxy] succeeded!
Connection to 192.168.0.100 11211 port [tcp/*] succeeded!

2. Batch detection of server port opening:

1. If we want to monitor 1 heap for IP and port specified, we can create a new file (column 1 server IP, column 2 port to monitor).


# vim /scripts/ip-ports.txt
192.168.0.100 80  
192.168.0.100 8081  
192.168.0.101 8082  
192.168.1.100 21 

2. We can write a script to check whether the port is open in batch:


# vim /scripts/ncports.sh
#!/bin/bash  
# Checks if the server port is open, and success returns 0 Value displayed ok Failure will return 1 Value displayed fail  

cat /scripts/ip-ports.txt | while read line  
do  
  nc -w 10 -z $line > /dev/null 2>&1  
  if [ $? -eq 0 ]  
  then  
    echo $line:ok  
  else  
    echo $line:fail  
  fi   
done 

3. Execute the script and check the running results as follows:


# chmod a+x  /scripts/ncports.sh
# /scripts/ncports.sh

80:192.168.0.100 ok
8081:192.168.0.100 ok
8082:192.168.0.101 ok
21:192.168.1.100 fail

3. Set alarm when port is blocked:

1. Email alarm:
1) first install mutt (see my other article "Linux: how to send mail with mutt command")

2) modify the above ncports.sh detection script to add 1 line when fail fails:
           ......
          echo $line :fail
          echo "server $line port blocked, please handle as soon as possible!" "[machine room monitoring] server $line port blocked" test@139.com
         ......
3) if the above receiving mailbox is set to move mailbox 139 and the receiving message notification is enabled, the function of "message alarm" can be realized.

2, windows message pop-up warning:
(1) first open the "Messenger" service of the windows client in the receive message popover and set it to "start"
(2) the smbclient command is used to send the message. The net script file is as follows:


# vim /scripts/net.sh
#!/bin/bash  
#/scripts/net.sh  
case "$1" in  
send)  
echo "$3"|smbclient -I "$2" -M `nmblookup -A "$2"|sed -e '1d' -e '3,/*/d'|cut -f2|cut -d' ' -f1`  

*)  
echo "Usage:net send <IPaddr.> <message>"  
exit 1  
esac 
# chmod a+x /scripts/net.sh

(3) send message popover command test :(send to the win xp machine 192.168.1.83, the sent content does not support Chinese)


# /scripts/net.sh  send  192.168.1.83     "hello,nihao"
 

3. The script for sending mail and warning popover when the port is blocked is as follows:


# vim /scripts/ncports.sh
#!/bin/bash  
# Checks if the server port is open, and success returns 0 Value, can not call the meeting back 1 value   
cat /scripts/ip-ports.txt | while read line  
do  
  nc -w 10 -z $line > /dev/null 2>&1  
  if [ $? -eq 0 ]  
  then  
    echo $line:ok   
  else  
    echo $line:fail  
    echo " The server  $line  Port is blocked, please deal with it as soon as possible! " | mutt -s " [computer room monitoring] server $line Port impassability "  test@18.com  
    /scripts/net.sh send 192.168.1.83 "The $line fail"    
  fi   
done 

4. Join the task plan and execute it every 2 minutes


# crontab -e
*/2 * * * *  /scripts/ncports.sh  > /dev/null 2>&1 
# service crond restart


Related articles: