Two scripts to check port conditions under BASH shell

Source: Internet
Author: User

These days need to do Linux TCP port check script, found that netstat, NC command, although good, but not intuitive, and the port is a bit more trouble to check, hands-on learning to do two scripts. One is to check the port condition in the form of a parameter and another to scan the port using a configuration file. Recorded for easy use later.


First, nc_check.sh

#!/bin/bashcat./check_port|while read Linedo nc-z ' uname-n ' $line >/dev/null 2>&1 if [$?-eq 0];then Echo "$line OK." Else echo "$line Failed." Fidone


Create a Check_port file in the current directory before use, as follows:

Cat check_port123345456


All TCP detection ports follow one record per line into a file. Execution effect:

[Email protected] ~]#. nc_check123 OK345 OK456 Failed

The program checks all the ports in the list again, successful OK, failed failed


Second, netstat_check.sh

#!/bin/bashdeclare -a chk_tcp_cmd= ' netstat -tln|egrep 0.0.0.0|awk  ' {print $4} ' | awk -f:  ' {print $2} ' |sort -n ' declare -a port_no_existsdeclare -i  current_port=0for tcp_port in [email protected]do  #IS  NUMBER expr  "$ Tcp_port " + 0 >/dev/null 2>&1 if [ " $? "  !=  "0"  ];then  echo  "$TCP _port not a number."   exit 1 fi port_no_exists[$CURRENT _port]= $TCP _port for lsn_port in   $CHK _tcp_cmd  do  if [  $TCP _port ==  $LSN _port ];then    unset port_no_exists[$CURRENT _port]   break  fi  done   ((current_port++)) doneif [ ${#PORT_NO_EXISTS [@]} -gt 0 ];then echo  " Tcp ports ${port_no_exists[@]} failed. "  exit 1fiExit 0 


When used, the port number is added directly after the script as a parameter, and if multiple port numbers are scanned, each port number is separated by a space. If one of the parameters is not a number, the program terminates. Execution effect:

[[Email protected] ~]#./netstat_check.sh 789

The scanned port has no output.

[email protected] ~]#./netstat_check.sh 123 456 789TCP Port 123 456 failed.

The program lists all the failed ports.

[[email protected] ~]#./netstat_check.sh 123 Hello 456hello not a number.

The program output error is not a number.

This article is from "Balsam Pear" blog, please make sure to keep this source http://golehuang.blog.51cto.com/7499/1768264

Two scripts to check port conditions under BASH shell

Related Article

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.