How Linux uses NC commands to monitor server ports _linux

Source: Internet
Author: User
Tags chmod net send

Recently encountered a project, the front end with the Apache HTTTPD (80 port), through the two-machine load balance forward to the back end of the two Tomcat processing (8081 and 8082 ports), now need to monitor the situation of these three ports, once down need to be able to immediately alarm processing. Batch system monitoring is better to use Nagios software to achieve, such a small project to install a Nagios software, a little cumbersome. On the Internet to check some information, summed up the experiment, you can use a simple NC command to achieve.

The use of the NC command detection port
# nc-v-W%ip%-Z%PORT%
-V Displays the instruction execution process.
-W < timeout seconds > set the time to wait for the connection.
-U indicates use of UDP protocol
-Z uses 0 input/output mode, only when scanning the communication port.

Example 1: Scan the specified 8080 port

Copy Code code as follows:

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

Example 2: Scan the 20 to 25 port range and output in detail.

Copy Code code as follows:

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

Nc:connect to 192.168.0.100 port (TCP) Failed:connection refused
Nc:connect to 192.168.0.100 port (TCP) Failed:connection refused
Connection to 192.168.0.100 Port [Tcp/ssh] succeeded!
Nc:connect to 192.168.0.100 port (TCP) Failed:connection refused
Nc:connect to 192.168.0.100 port (TCP) Failed:connection refused
Nc:connect to 192.168.0.100 port (TCP) Failed:connection refused

Example 3: Scan port ranges from 1 to 65535, output only open ports (remove the-v argument)

Copy Code code as follows:

# nc-w 1-z 192.168.0.100 1-65535

Connection to 192.168.0.100 Port [Tcp/ssh] succeeded!
Connection to 192.168.0.100 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!

Second, the batch testing server specified port opening conditions:

1, if we want to monitor a bunch of specified IP and port, you can create a new file (1th server IP, column 2nd to monitor the port).

Copy Code code as follows:

# 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 such a script to bulk detection port is open:

Copy Code code as follows:

# vim/scripts/ncports.sh
#!/bin/bash
#检测服务器端口是否开放, Success will return 0 value display OK, failure will return 1 value display fail

Cat/scripts/ip-ports.txt | While Read line
Todo
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 to see the results of the operation as follows:

Copy Code code as follows:

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

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

Third, the port does not pass when set alarm:

1, Mail alarm:
1 Install the following Linux mail Send program mutt (see my another article, "Linux How to use the Mutt command to send mail"

2 Modify the above ncports.sh detection script, add a line when the failure fail is displayed:
........................
echo $line: Fail
echo "Server $line port is not working, please handle it ASAP! " | Mutt-s "" "" "" Engine Room Monitor "server $line port does not pass" test@139.com
........................
3 If the above receiving mailbox is set to move 139 mailboxes, and open to receive mail message, you can realize the "SMS Alert" function.

2, Windows message Bomb window alarm:
(1) First open the Receive Message window Windows Client "Messenger" service, set to "Start"
(2) using the smbclient command to send messages, net script files are as follows:

Copy Code code as follows:

# vim/scripts/net.sh
#!/bin/bash
#/scripts/net.sh
Case "$" in
Send
echo "$" |smbclient-i "$"-M ' Nmblookup-a "$" |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 the Message Bomb Window command test: (Send to 192.168.1.83 this win XP machine, send content does not support Chinese)

Copy Code code as follows:

#/scripts/net.sh Send 192.168.1.83 "Hello,nihao"

3, the port does not pass the message and the message window alarm script is as follows:

Copy Code code as follows:

# vim/scripts/ncports.sh
#!/bin/bash
#检测服务器端口是否开放, Success returns a value of 0, no meeting returns 1 value
Cat/scripts/ip-ports.txt | While Read line
Todo
Nc-w 10-z $line >/dev/null 2>&1
If [$?-eq 0]
Then
echo $line: OK
Else
echo $line: Fail
echo "Server $line port is not working, please handle it ASAP! " | Mutt-s "" "" "" Engine Room Monitor "server $line port does not pass" test@18.com
/scripts/net.sh send 192.168.1.83 "the $line fail"
Fi
Done

4, join the task plan every 2 minutes to execute once

Copy Code code as follows:

# CRONTAB-E
*/2 * * * */scripts/ncports.sh >/dev/null 2>&1
# Service Crond Restart

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.