Linux自动化运维工程师
实验所用系统为Redhat-rhel8.2。
Linux自动化运维工程师–shell脚本的练习
1、计算当前cpu总占用率
echo "`ps -eo %cpu | awk 'BEGIN{N=0}!/0.0|%CPU/{N+=$1}END{print N}'`%"
2、一分十秒倒计时
- 分页显示倒计时
vim time_1min10s.sh
///
MIN=1
SEC=10
SECS=$[$MIN*60+$SEC]
clear
for ((;SECS;SECS--))
do
echo -ne "$[$SECS/60]:$[$SECS%60] "
sleep 1
echo -ne "\r"
done
///
- 在当前页显示倒计时
vim time1_1min10s.sh
///
MIN=1
SEC=10
for ((;SEC>=0;SEC--))
do
if [ "$SEC" = "0" -a "$MIN" = "0" ]
then
exit
elif [ "$SEC" = "0" -a "$MIN" -ge "0" ]
then
echo -ne "$MIN:$SEC "
sleep 1
echo -ne "\r"
SEC=60
((MIN--))
else
echo -ne "$MIN:$SEC "
sleep 1
echo -ne "\r"
fi
done
///
3、猜字游戏:5是幸运数字,大的话报太大,小的话报太小,exit退出。(echo $RANOM 生成随机数)
vim luckynum.sh
///
NUM=$[$RANDOM%10]
while true
do
read -p "Please input luch_number: " LUCKNUM
if [ "$LUCKNUM" = "exit" ]
then
exit
elif [ "$NUM" -gt "$LUCKNUM" ]
then
echo "small"
elif [ "$NUM" -lt "$LUCKNUM" ]
then
echo large
else
echo $NUM is lusky number
exit
fi
done
///
4、用户建立脚本
脚本需求如下: ? 执行users_create.sh userlist passlist ? 建立userlist列表中的用户 ? 设定userlist列表中的密码为passlist列表中的密码 ? 当脚本后面跟的文件个数不足两时,报错 ? 当文件行数不一致时报错 ? 当文件不存在时报错 ? 当用户存在时报错
vim users_create.sh
///
if [ "$#" -lt "2" ]
then
echo "Error: Plesae input userfile or passwordfile following $0 !"
elif [ ! -e "$1" ]
then
echo "Errot: $1 is not exist !"
elif [ ! -e "$2" ]
then
echo "Errot: $2 is not exist !"
else
USER_LINE=`awk 'END{print NR}' $1`
PASS_LINE=`awk 'END{print NR}' $2`
if [ "$USER_LINE" = "$PASS_LINE" ]
then
for LINE in `seq 1 $USER_LINE`
do
USER=`sed -n ${LINE}p $1`
PASSWD=`sed -n ${LINE}p $2`
id $USER &> /dev/null && {
echo $USER is exist
} || {
useradd $USER &> /dev/null && {
echo $PASSWD | passwd --stdin $USER &> /dev/null && {
echo $USER is created
}
}
}
done
else
echo "Error: $1's lines is different from $2 !"
fi
fi
///
5、数据库脚本:
脚本需求如下:
- 执行db_dump.sh westos(数据库密码)
- 脚本执行后会备份数据库中的所有库到/mnt/mysqldump目录中
- 备份文件名称为 “库名称.sql”当此文件存在时报错并询问动作,输入“S”跳过备份,当输入“B"时备份“库名称.sql”文件为“库名称_backup.sql”,当输入“O”时,覆盖原文件。
if [ -z "$1" ]
then
echo "Please input password for mysql !"
exit
fi
rpm -q mariadb-server &> /dev/null || {
dnf install mariadb-server.x86_64 -y
}
systemctl status mariadb.service | grep "running" &> /dev/null || {
systemctl start mariadb.service &> /dev/null
}
DBS=`mysql -uroot -p$1 -EN -e "SHOW DATABASES;" 2> /dev/null | grep -vE "row|_schema$"`
if [ "$?" -ne "0" ]
then
echo "Error: Wrong password for db !"
else
mkdir -p /mnt/mysqldump
for DBNAME in $DBS
do
if [ ! -e "/mnt/mysqldump/$DBNAME.sql" ]
then
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql
else
echo "/mnt/mysqldump/$DBNAME.sql is exist !"
echo "You can do action: [S]kip [B]ackup [O]verwrite [E]xit"
while true
do
read -p "Please input action: " ACTION
case $ACTION in
s|S|skip|Skip)
break
;;
b|B|backup|Backup)
mv /mnt/mysqldump/$DBNAME.sql /mnt/mysqldump/${DBNAME}_backup_`date +%m%d%h`.sql
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql
break
;;
o|O|overwrite|Overwrite)
mysqldump -uroot -p$1 $DBNAME > /mnt/mysqldump/$DBNAME.sql
break
;;
e|E|exit|Exit)
break
;;
*)
echo Wrong action !!
continue
esac
done
fi
done
fi
6、系统性能检测脚本
脚本需求如下: 执行check_upload.sh: 显示效果如下
cpu: 实际用量%
mem: 实际用量%
- 思路解析:先查询所有进程的cpu,过滤出非0的部分,使用自加的方式得到总和。
vim check_upload.sh
///
echo -e "cpu:\t`ps -eo %cpu | awk 'BEGIN{N=0}!/0.0|%CPU/{N+=$1}END{print N}'`%"
echo -e "mem:\t`ps -eo %mem | awk 'BEGIN{N=0}!/0.0|%CPU/{N+=$1}END{print N}'`%"
///
7、用脚本在当前系统中添加一个swap分区
swap分区大小为500M并开机自动激活此分区
[ -z "$1" ] && {
echo "Please input device following swap.sh !"
exit
}
[ ! -e "$1" ] && {
echo "$1 is not exist !"
exit
}
/usr/bin/expect &> /dev/null <<EOF
spawn fdisk $1
expect {
"Command" { send "n\r";exp_continue }
"Select (default p)" { send "p\r";exp_continue }
"Partition number" { send "\r";exp_continue }
"First sector" { send "\r";exp_continue }
"Last sector" { send "+500M\r"; }
}
expect {
"signature?" { send "yes\r";exp_continue }
"Command" { send "wq\r" }
}
expect eof
EOF
DEVICE=`fdisk -l /dev/vdb | tail -n 1 | awk '{print $1}'`
DEVICE_NUM=`fdisk -l /dev/vdb | tail -n 1 | awk '{print $1}' | awk -F "/dev/vdb" '{print $2}'`
DEVICE_TYPE=`fdisk -l /dev/vdb | tail -n 1 | awk '{print $1}' | awk '{print $6}'`
if [ "$DEVICE_TYPE" != "82" ]
then
/usr/bin/expect &> /dev/null <<EOF
spawn fdisk $1
expect {
"Command" { send "t\r";exp_continue }
"Partition number" { send "$DEVICE_NUM\r";exp_continue }
"Hex code" { send "82\r" }
}
expect "Command"
send "wq\r"
expect eof
EOF
fi
mkswap $DEVICE
grep DEVICE /etc/fstab &> /dev/null
if [ "$?" -ne "0" ]
then
echo "$DEVICE swap swap defaults 0 0" >> /etc/fstab
fi
8、DNS A记录文件的生成
捕捉十台主机的A记录信息 172.25.254.1-10 当网络畅通,将主机ip和主机名称的对应关系生成到/mnt/hostlist文件中
for IP in 172.25.254.{1..10}
do
Auto_Ssh()
{
/usr/bin/expect <<EOF
spawn ssh -l root $IP hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "westos\r" }
}
expect eof
EOF
}
ping -c1 -w1 $IP &> /dev/null && {
grep $IP /mnt/hostlist &> /dev/null
if [ "$?" -ne "0" ]
then
Auto_Ssh $IP | echo -e "$IP\t `tail -n 1`" >> /mnt/hostlist ##成功则输出IP和主机名
fi
} || {
grep $IP /mnt/hostlist &> /dev/null
if [ "$?" -ne "0" ]
then
echo -e "$IP\t down" >> /mnt/hostlist
fi
}
done
|