实战一、硬件巡检,对cpu、内存、磁盘超过阈值报警
#!/bin/bash
disk=$(df -Th | grep "/$" | awk '{print $6}' | awk -F% '{print $1}')
mem=$(expr $(free | grep "Mem:" | awk '{print $3}') \* 100 / $(free | grep "Mem:" | awk '{print $2}'))
cpu=$(expr 100 - $(mpstat | tail -1 | awk '{print $12}' | awk -F. '{print $1}'))
alert_file=/root/alert.txt
mailbox=xxxxxx@qq.com
if [ $disk -ge 10 ]
then echo "磁盘占有率超过85%" >> $alert_file
fi
if [ $mem -ge 5 ]
then echo "内存占有率超过90%" >> $alert_file
fi
if [ $cpu -ge 2 ]
then echo "cpu占有率超过90%" >> $alert_file
fi
if [ -f $alert_file ]
then
cat $alert_file | mail -s "alert report" $mailbox
rm -rf $alert_file
fi
实战二、判断你输入的字符是数字、字母还是其他字符
#!/bin/bash
read -p "请输入:" i
if [[ $i =~ ^[a-zA-Z]+$ ]]
then
echo "$i是字母"
elif [[ $i =~ ^[0-9]+$ ]]
then
echo "$i是数字"
else
echo "$i是其他字符"
fi
实战三、打印菱形图形
#!/bin/bash
for ((i=4; i>=1; i--))
do
for ((j=1; j<=$i; j++))
do
echo -n " "
done
for ((a=4; a>=$i; a--))
do
echo -n "*"
done
for ((b=3; b>=$i; b--))
do
echo -n "*"
done
echo " "
done
for ((i=2; i<=4; i++))
do
for ((a=1; a<=$i; a++))
do
echo -n " "
done
for ((b=4; b>=$i; b--))
do
echo -n "*"
done
for ((c=3; c>=$i; c--))
do
echo -n "*"
done
echo " "
done
实战四、冒泡算法排序
#!/bin/bash
num=(66 3 2 15 100 70 160 8)
for ((i=1;i<${#num[*]};i++))
do
for ((j=0;j<${#num[*]}-$i;j++))
do
if [ ${num[j]} -lt ${num[(($j+1))]} ]
then temp=${num[j]}
num[j]=${num[$j+1]}
num[(($j+1))]=$temp
fi
done
done
echo ${num[*]}
实战五、实现ssh的免密登录
#!/bin/bash
yum -y install expect
CLIENT1=$1
USER=$2
PASSWD=$3
expect << -EOF
spawn ssh-keygen -t rsa
expect {
"save the key" {send "\r";exp_continue}
"Overwrite" {send "y\r";exp_continue}
"Enter passphrase" {send "\r";exp_continue}
"same passphrase" {send "\r"}
}
spawn ssh-copy-id ${USER}@${CLIENT1}
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "$PASSWD\r"}
}
expect eof
-EOF
实战六、实现dns及apache服务的一键化部署,要求apche服务器绑定域名,客户机实现访问。
#!/bin/bash
yum -y install bind*
yum -y install httpd
sed -i 's/127.0.0.1/192.168.10.30/g' /etc/named.conf
sed -i 's/localhost/any/g' /etc/named.conf
touch /var/www/html/index.html
cat > /var/www/html/index.html <<EOF
<html><title>web</title><body><h1>Hello World!!!!</h1></body></html>
EOF
cat > /etc/resolv.conf <<EOF
nameserver 192.168.10.30
EOF
cat >> /etc/named.rfc1912.zones <<EOF
zone "web.com" IN {
type master;
file "web.com.zone";
allow-transfer { 192.168.10.30; };
also-notify { 192.168.10.30; };
};
zone "30.168.192.in-addr.arpa" IN {
type master;
file "web.com.local";
allow-transfer { 192.168.10.30; };
};
EOF
cd /var/named/
cp -p named.localhost web.com.zone
cp -p named.loopback web.com.local
cat > /var/named/web.com.zone << EOF
\$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 192.168.10.30
www IN A 192.168.10.30
ftp IN A 192.168.10.30
mail IN CNAME www
EOF
cat > /var/named/web.com.local << EOF
\$TTL 1D
@ IN SOA web.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS web.com.
A 192.168.10.30
10 IN PTR www.web.com.
11 IN PTR ftp.web.com.
EOF
service named start
service httpd start