初學者的幾道shell腳本題!!!!
#!/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
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
\ 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 root@$LB02
;;
2|Mysql)
ssh root@$Mysql
;;
3|NFS)
ssh root@$NFS
;;
4|web01)
ssh root@$web01
;;
5|web02)
ssh root@$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
用:[root@m01 ~]# expect jiaohu.sh 命令登陸
初學者的幾道shell腳本題!!!!