SHELL訓練營--day13_shell練習
阿新 • • 發佈:2019-01-05
#輸入數字,執行對應命令。 #!/bin/bash while: do read -p "please input a number: "n if [ -z "$n" ] then echo "請輸入一個純數字,1~4" sleep 3 continue n1=`echo $n|sed 's/[0-9]//g'` if [ -n $n1 ] then echo "請輸入一個純數字,1~4" sleep 3 else break done case $n in 1) date ;; 2) ls ;; *) echo "請輸入一個純數字,1~4" ;; esac #批量建立使用者 #!/bin/bash for i in `seq -w 00 09` do useradd user_$i p=`mkpasswd -l 10 -s 0` echo "user_$i $p" >> /tmp/pass.tmp/pass echo "$p"|passwd --stdin user_$i done #監控httpd程序 #!/bin/bash check_service() { c=0 for i in `seq 1 5` do /usr/local/apache2/bin/apachectl -k restart 2> /tmp/httpd.err if [ ! $? -eq 0 ] then c=$[$c+1] else break fi done if [ $c -eq 5 ] then python mail
[email protected] "apache重啟失敗" "`head -1 /tmp/httpd.err`" exit } while: do n=`ps -C httpd --no-heading|wc -l` if [ $n -ge 500 ] then check_service sleep 60 n_new=`ps -C httpd --no-heading|wc -l` if [ $n_new -ge 500 ] then python mail[email protected] "apache重啟失敗" "請檢查伺服器" exit fi fi sleep 10 done # 根據日誌封IP #!/bin/bash block_ip() { t1=`date -d "-1 min" +%Y:%H:%M` log=/data/logs/access_log egrep "$t1:[0-9+]" $log > /tmp/tmp_last_min.log awk '{print $1}' /tmp/tmp_last_min.log |sort -n |uniq -c |sort -n |awk `$1>100 {print $2}` >/tmp/bad_ip.list n=`wc -l /tmp/bad_ip.list|awk '{print $1}'` if [ $n -ne 0 ] then for ip in `cat /tmp/bad_ip.list` do iptables -I INPUT -s $ip -j REJECT done fi } unblock_ip() { iptables -nvL INPUT |sed `1d` |awk '$1<5 {print $8}' > /tmp/good_ip.list n=`wc -l /tmp/good_ip.list|awk '{print $1}'` if [ $n -ne 0 ] then for ip in `cat /tmp/good_ip.list` do iptables -D INPUT -s $ip -j REJECT done fi iptables -Z } t=`date +%M` if [ $t =="00" -o $t =="30" ] then unblock_ip block_ip else block_ip fi #算數字 #!/bin/bash x=10 y=21 for i in `seq 0 15` do echo $x x=$[$x+$y] z=$[2**$i] y=$[$y+$z] done