Using NC commands under Linux to monitor port usage of the detection server (reproduced)

Source: Internet
Author: User
Tags how to send mail net send

Recently encountered a project, front end with Apache HTTTPD for publishing (80 port), through the dual-machine load balancer forwarding to the back end of two tomcat processing (8081 and 8082 ports), now need to monitor the situation of these three ports, once down the need to be able to immediately alarm processing. Batch system monitoring is a good use of nagios software to achieve, such a small project to install a Nagios software, a bit cumbersome. On the Internet to check some information, summed up the experiment, you can use a simple NC command to achieve. One, the use of the NC Command detection Port # NC-V-W%ip%-Z%port%-v shows the instruction execution process. -W < timeout seconds > set the time to wait for the connection. -U means using the UDP protocol-z using 0 input/output mode, only when scanning the communication port. Example 1: Scan a specified 8080 port # nc-v W 10-z 192.168.0.100 8080Connection to 192.168.0.100 8080 port [tcp/http] succeeded! Example 2: Scan 20 to 25 port fan and detailed output. # nc-v-W 2-z 192.168.0.100 20-25nc:connect to 192.168.0.100 port (TCP) Failed:connection Refusednc:connect to 192 .168.0.100 port (TCP) Failed:connection refusedconnection to 192.168.0.100 ports [TCP/SSH] Succeeded!nc:connect to 192.168.0.100 port (TCP) Failed:connection refusednc:connect to 192.168.0.100 port (TCP) Failed:connection refuse Dnc:connect to 192.168.0.100 port (TCP) Failed:connection refused example 3: Scan 1 to 65535 port range, output only open ports (minus-v parameter) # Nc-w 1-z 192 .168.0.100 1-65535connection 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, batch detection server specified port open: 1, if we want to monitor a bunch of specified IP and port, you can create a new file (1th server IP, The 2nd 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 212, We can write such a script to detect whether the port is open in bulk: # 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 the read line does nc-w 10-z $line >/dev/null 2>&1 if [$?-eq 0] then echo $line: Ok else echo $line: Fail fi Done3, execute the script to see the results of the operation as follows: # chmod a+x/scripts/ncports.sh#/scripts/ncports.sh192.168.0.100 80:ok192.168.0.100 8081:ok192.168.0.101 8082:ok192.168.1.100 21:fail Three, the port is not through the set alarm: 1, Mail alarm: (1) first install Linux under the Mail Sender program Mutt (see my other article, " Linux below how to send mail with the mutt command (2) modify the above ncports.sh detection script, in the display failed fail when the time to add a line: ....... echo $line: Failecho "server $line port is not working, please deal with it ASAP! " | Mutt-s "" "Machine Room Monitoring" server $line port "[email protected] ....... ......... (3) If the above receiving mailbox is set to move 139 mailbox, and turn on receiving email message notification, you can realize the function of "SMS Alarm". 2. Windows message pop-up alarm: (1) first open the "Messenger" Service of Windows client receiving message pop-up, set to "Start" (2) use smbclient command to send message, net script file is 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

Using NC commands under Linux to monitor the port usage of the detection server (reprint)

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.