Daily Shell Exercises (06)--Detection port service

Source: Internet
Author: User

1. Topics

Write a script to determine whether the 80 port of the machine (assuming the service is httpd) is open, if it is open to do nothing, if the discovery port does not exist, then restart the httpd service, the concurrent mail notify yourself. Once the script is written, it is executed every minute, and a dead loop script can be written, 30S checked once.

2. Exercise Analysis
    • First of all, we need to distinguish between requirements, the requirement here is to detect whether the 80 port is listening, not to detect whether the HTTPD service is running, although there is a certain connection, but not a reciprocal relationship. Detects port status with the NETSTAT-LNTP command. If you are asked to detect the port status of a remote host, use the following command:

      nmap  -p 80  host_remote_ip  ## 替换成远程主机的ip就可以了例如:[[email protected] ~]# nmap -p 80 192.168.188.107Starting Nmap 6.40 ( http://nmap.org ) at 2018-05-03 11:48 CSTNmap scan report for cenvm71 (192.168.188.107)Host is up (0.00021s latency).PORT   STATE  SERVICE80/tcp closed httpMAC Address: 00:0C:29:E4:4D:1F (VMware)Nmap done: 1 IP address (1 host up) scanned in 0.14 seconds
    • If the HTTPD service does not start, it starts. The starting method is of course your own knowledge of Apache's startup commands.
    • Finally, the mail.py script in my blog, the Daily Shell exercise (04), can continue to be used in the mail-sending method. Using the method, you can go through the article.
3. Detection scripts
[[email protected] work]# cat check_80.sh #!/bin/bashma="[email protected]"if netstat -lntp | grep -q ‘:80 ‘then  exit 0fi/usr/local/apache2.4/bin/apachectl restart >/dev/null 2> /dev/nullpython /usr/local/sbin/work/mail.py $ma "port_80" "prot 80 down"n=`pgrep -l httpd | wc -l` echo $nif [ $n -eq 0 ];then  /usr/local/apache2.4/bin/apachectl start 2> /tmp/http.errorfiif [ -s /tmp/http.error ]then  python /usr/local/sbin/work/mail.py $ma "apache_restart_wrong" "`cat /tmp/http.error`"fi

Script Analysis:

  1. The IF condition's judgment can be a command, when the command execution succeeds the condition is established. For example:

    [[email protected] work]# ls /etc/passwd/etc/passwd[[email protected] work]# ls /etc/okamals: cannot access /etc/okama: No such file or directory

    The first command is not faulted, and the second command is wrong. If they are judged as if, the effect is as follows:

    # if ls /etc/passwd >/dev/null 2>/dev/null ; then echo ok;fiok# if ls /etc/okma >/dev/null 2>/dev/null ; then echo ok;else  echo error; fierror

    ">/dev/null 2>/dev/null" This is to make the judgment quiet, redirect the excess information, and then our if command only determines the status code returned.

  2. The 2nd to note is that NETSTAT-LNPT | Grep-q ': 80 ', is used to determine whether the system's 80 port is listening, and in ': 80 ' Finally there is a space, this is to match exactly. Because it is possible to match to port 8080.

    Grep-q only to match, will not print out the results, used in if judgment, the effect is exactly, do not add ">/dev/null 2>/dev/null";

  3. If the 80 port does not start, we will start the HTTPD service as required. After starting the service, we should also check if the HTTPD service has actually started successfully. The simple method is to detect if the httpd process exists.

  4. The pgrep-l httpd command, in addition to the PID that lists httpd, also lists the process names together

  5. If the httpd does not start properly, the error message is output to a file. Then, when the warning message is sent, the error message is sent as the message content.
4. Conclusion

This exercise is not difficult, but there are some areas that can be figured out to keep you diligent. such as the flexible use of if conditions, the use of pgrep, as well as the combination of mail scripts and so on.

Daily Shell Exercises (06)--Detection port service

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.