初学者的几道shell脚本题!!!!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初学者的几道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 命令登陆
以上是关于初学者的几道shell脚本题!!!!的主要内容,如果未能解决你的问题,请参考以下文章