1. 程式人生 > >初學者的幾道shell指令碼題!!!!

初學者的幾道shell指令碼題!!!!

1.取磁碟的使用率 大於百分%2 就報警或者把結果輸入一個檔案裡

#!/bin/sh
Disk_free=$(df -h|grep /$|awk -F "[% ]+" '{print $(NF-1)}')
if [ $Disk_free -ge 3 ]
then
echo "Disk Is use $Disk_free ERROR" > /opt/disk.txt
else
echo "Disk Is $Disk_free good" > /opt/disk.txt
fi

2.負載超過百分之10 則報警 或者傳送到檔案

#!/bin/bash
while true
do
Load=$(w|awk 'NR==1'|awk -F, '{print $(NF-2)}'|awk '{print $NF}')
if [ ${Load%.*} -ge 1 ];then
echo "load is dinger $Load"

fi
done

3.用指令碼寫跳板機jumpserver
#!/bin/bash
#jumpserver
LB02=10.0.0.6
Mysql=10.0.0.52
NFS=10.0.0.7
web01=10.0.0.8
web02=10.0.0.9

caidan () {
cat <<o
+++++++++++++++++++++++++++++++++++++
\ 1) LB02
\ 2) Mysql
\ 3) NFS
\ 4) web01
\ 5) web02
\ 6) h
+++++++++++++++++++++++++++++++++++++
o
}
caidan

trap "" HUP INT TSTP

while true
do
read -p "請輸入你要登陸的伺服器編號:" num
case $num in
1|LB02)
ssh

[email protected]$LB02
;;
2|Mysql)
ssh [email protected]$Mysql
;;
3|NFS)
ssh [email protected]$NFS
;;
4|web01)
ssh [email protected]$web01
;;
5|web02)
ssh [email protected]$web02
;;
6|h)
caidan
;;
lihongqing)
exit 1
;;
esac
done

4.啟停nginx

#!/bin/sh
#nginx_start
source /etc/init.d/functions

TE=$1
ST(){
if [ $? -eq 0 ]
then
action "Nginx $TE" /bin/true
else
action "Nginx $TE" /bin/false
fi
}

start(){
/usr/sbin/nginx
ST
}

stop(){
/usr/sbin/nginx -s stop
ST
}

restart(){
/usr/sbin/nginx -s stop
sleep 5
/usr/sbin/nginx
ST
}

reload(){
/usr/sbin/nginx -s reload
ST
}
status(){
pro=$(ps axu|grep nginx|grep master|awk '{print $2}')
echo "Nginx PID is $pro"
port=$(netstat -tnulp|grep -v tcp6|grep nginx|awk '{print $4}')
echo "Nginx port is $port"
ST
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
*)
echo "USAGE $0: Please Input start|stop|restart|reload|status"
esac

#########################互動登陸#################################
#!/bin/bash

spawn ssh [email protected]

expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "123456\r" };
}
interact
用:[[email protected] ~]# expect jiaohu.sh 命令登陸