Practical shell compiling case

Source: Internet
Author: User

1. you can set the number of servers to be checked and the server to check. You can set the IP address of the non-Online host and the corresponding time of the non-Online host, and collect statistics on the number of non-Online hosts saved to the file. Then, the file content is displayed on the screen.

1 #! /Bin/bash 2 read-P "Enter the number of detection servers:" x 3 S = 0 4 B = 0 5 for (I = 1; I <= X; I ++) 6 do 7 read-P "Enter the address of the host to be checked. after entering the address, please wait:" IP [$ I] 8 9 Ping-C 1 192.168.1. $ {IP [$ I]} &>/dev/null 10 if [$? -Ne 0] 11 then 12 Date>/root/Shell/offline.txt 13 echo "IP: 192.168.1. $ {IP [$ I]} is offline ">/root/Shell/offline.txt 14 Let s ++ 15 else echo" 192.168.1. $ {IP [$ I]} is online "16 fi 17 done 18 19 echo" Number of disconnections this time: "$ S>/root/Shell/offline.txt 20 tail-10/root/Shell/offline.txt

2. Write a STARTUP script for the source code HTTP service

 

  1 #! /bin/bash  2 # chkconfig: 2345 75 25  3 # description: apache http  4 # processname: httpd  5 path="/usr/local/httpd/bin/apachectl"  6 case $1 in  7 start)  8     $path start &> /dev/null  9     sleep 1 10     echo -n "apache is start..ok";; 11 stop) 12     $path stop 13     sleep 2 14     echo -n "apache is stop..ok";; 15 restart) 16     $path stop 17     echo -n "apache is stop..ok" 18     sleep 3 19     $path start 20     echo -n "apache is start..ok";; 21 status) 22     netstat -anptu | grep httpd$ 23     if [ $? -eq 0 ];then 24     echo "apache is runing" 25     else echo "apache is not run" 26     fi;; 27 *) 28     echo "apache {start/stop/restart/status}" 29 esac 30 echo


3. When the system root partition can have less than 15 GB space, send a warning message "insufficient disk space for the root partition" to all terminals of the system"

1 #! /Bin/bash 2 I = 'df-H | grep/$ | awk '{print $4}' | awk-F "G" '{print $1} ''' 3 if [$ I-lt 15]; then 4 wall "root partition available space $ I g, insufficient space" 5 fi


4. When adding a user, you can set the number of users to be added, username prefix, user password, and user validity period.

1 #! /Bin/bash 2 read-P "Enter the number of users" count 3 read-P "Enter the username prefix" user 4 read-P "username and password" passwd 5 read-P" validity Period "Shi 6 S = 85 7 for (I = $ S; I <= $ S + $ count-1; I ++) 8 do 9 if [$ I-lt 10]; then 10 useradd "$ user" 0 $ I 11 echo "$ passwd" | passwd -- stdin "$ user" 0 $ I &>/dev/null 12 chage-D 0 "$ user "0 $ I 13 chage-M $ Shi" $ user "0 $ I 14 else 15 useradd $ user $ I 16 echo" $ passwd "| passwd -- stdin $ user $ I &>/dev/null 17 chage-D 0 $ user $ I 18 chage-M $ Shi $ user $ I 19 fi 20 done


5. Run the script on the slave server in MySQL master-slave synchronization to monitor the status of the IO and SQL processes. When a process is in the no status, send a warning message to the terminal.

  1 #! /bin/bash  2 a=`mysql -uroot -p123456 -e "show slave status\G"|grep -iE "*io_runing"|awk ‘{print $2}‘`  3 b=`mysql -uroot -p123456 -e "show slave status\G"|grep -iE "*sql_runing"|awk ‘{print $2}‘`  4 while :  5 do  6         if [ $a = "NO" -o $b = "NO" ];then  7         wall "mysql runing_status is NO"  8         fi  9 done


6. Practical Regular Expression

Mac :( [0-9a-fa-f] {2} :) {5} [0-9a-fa-f] {2} IP :( [0-9] | [1-9] [0-9] | 1 [0 -9] {2} | 2 [0-4] [0-9] | 25 [0-5] \.) {3} [0-9] | [1-9] [0-9] | 1 [0-9] {2} | 2 [0-4] [0-9] | 25 [0-5]


This article is from the "sleepcat" blog, please be sure to keep this source http://sw5720.blog.51cto.com/8812314/1437602

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.