初学者的几道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 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 命令登陆

猜你喜欢

转载自blog.51cto.com/13859426/2315020