shell脚本练习

本文提供四个shell脚本练习案例:1. 统计并阻止连接失败超3次的主机;2. 实现跳板机功能,限制非许可主机操作;3. 自动控制apache服务的启停及状态显示;4. 数据库备份脚本,交互式处理已存在备份文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习1:在/var/log/secure查找连接失败的主机,并统计次数,累计连接失败超过3次,将此主机加入系统黑名单(/etc/hosts.deny)

#!/bin/bash
cat /var/log/secure | grep "Failed " | awk '{print $11}' | uniq -c | awk '{print $2"="$1}' > /tmp/blacklist

MAXCOUNT="3"

for i in `cat /tmp/blacklist`
do
        IP=`echo $i | awk -F= '{print $1}'`   ##取出主机IP
        NUM=`echo $i | awk -F= '{print $2}'`  ##取出失败次数
if [ $NUM -gt $MAXCOUNT ];then
        ##屏蔽IP前确认次IP是否已经存在
        grep $IP /etc/hosts.deny > /dev/null
        if [ $? -gt 0 ];then
                echo "sshd:$IP" >> /etc/hosts.deny
        fi
fi
done

练习2:写一个跳板机的脚本,当连接我的主机不是我允许的主机时,设定主机跳转到规定界面,只能执行有限操作,当连接主机设定是允许连接的可以任意操作

vim tiaoban.sh
#!/bin/bash
function trapper() {
    trap "" INT EXIT TSTP TERM HUP  ##中断信号
}

function main() {
while true
do
    trapper
    clear
    cat <<menu
        1)host1-172.25.254.100
        2)host2-172.25.254.200
        3)exit                  ##能执行的有限操作
menu

read -p "Please input a num:" num
case $num in
    1)
        echo "login in 172.25.254.100..."
        ssh 172.25.254.100
        ;;
    2)
        echo "login in 172.25.254.200..."
        ssh 172.25.254.200
        ;;
    3)
        exit
        ;;
esac
done
}

main
------------------------------------------------------------------------------------------------------------
cd /etc/profile.d 
vim user_check.sh 
#!/bin/bash 
[ $UID -ne 0 ]&& sh /mnt/tiaoban.sh  ##当uid不为0就执行跳板机脚本

练习3:写脚本根据相关输入,执行命令开启,重启,关闭,显示状态,安装apache服务退出脚本

#!/bin/bash
while [ ture ]
do
	echo -e "
    	\033[31m start: start apache service \033[0m
    	\033[32m restart: restart apache service \033[0m
    	\033[33m stop: stop apache service \033[0m
    	\033[34m status: display the current state of apache service \033[0m
    	\033[35m install: install apache service \033[0m
    	\033[36m q: 退出系统 \033[0m
	"                  
##可执行的操作:开启,重启,关闭,显示状态,安装apache服务,退出脚本
        read -p "please input:" a
        case $a in
        start|START)
                result=$(systemctl status httpd.service | grep "running" | awk '{print $3}')
		result1=$(systemctl status httpd|awk -F ' ' '{print $2}'|grep 'not-found')
		 if [ "$result" == "(running)" ];then
                        echo -e "\napache service already running!\n"
		elif [ "$result1" == "not-found" ];then	
			echo -e "\napache service not installed,please input install to installing apache!\n"
		else
                        systemctl start httpd 
                        echo -e "\napache service starting...\n"
			echo -e "\napache service alredy started!\n"
                fi  
   		;;
##如果已开启显示正在运行;如果没有则安装apache,再显示已开启
	stop|STOP)
		result2=$(systemctl status httpd|awk -F ' ' '{print $2}'|grep 'not-found')
                if [ "$result2" == "not-found" ];then
		echo -e "\napache service not installed,please input install to installing apache!\n"
		else
		systemctl stop httpd 
		echo -e "\napache service stoped!\n"
		fi
		;;
##如果没装服务显示未安装并提示输入install去安装,否则显示服务已关
	restart|RESTART)
		result3=$(systemctl status httpd|awk -F ' ' '{print $2}'|grep 'not-found')
                if [ "$result3" == "not-found" ];then
		echo -e "\napache service not installed,please input install to installing apache!\n"
		else
		systemctl restart httpd
		echo -e "\napache service restarted!\n"
		fi
		;;	
##如果没装服务显示未安装并提示输入install去安装,否则执行重启
	status|STATUS)
		result4=$(systemctl status httpd|awk -F ' ' '{print $2}'|grep 'not-found')
                if [ "$result4" == "not-found" ];then
		echo -e "\napache service not installed,please input install to installing apache!\n"
		else
		systemctl status httpd
		fi
		;;
##如果没装服务显示未安装并提示输入install去安装,否则显示服务状态
	install|INSTALL)
		echo -e "\nplease configured your yum source!\n"
		read -p "continue?[y/n]" r 
		if [ "$r" == "y" ];then
		echo -e "\nwaiting...\n"
		yum clean all &> /dev/null
		yum repolist &> /dev/null
		result9=$(systemctl status httpd|awk -F ' ' '{print $2}'|grep 'not-found')
                result5=$(yum install httpd | grep "Nothing to do") 
		if [ "$result9" == "not-found" ];then
                         echo -e "\ninstalling apache service...\n"
                         yum install httpd -y &> /dev/null
                         echo -e "\napache service installed!\n"
		elif [ "$result5" == "Nothing to do" ];then
			echo -e "\napache already installed\n"
		else
			echo -e "\nsofterware not found\n"
		fi	
		else
			exit 0
		fi
		;;
##检查yum源,有源看需求安装否则提示软件不存在
	q|Q)
		echo -e "\nexit system!"
		exit 0
		;;
	*)
		echo "Error input! please try again!"
		;;
##退出脚本
        esac
done

练习4:数据库备份,执行script.sh $dbpasswd 备份数据库中所有库到/mnt/mysqldump目录中,备份文件名称为“库名称.sql”,当此文件存在时进入交互模式,询问动作,输入“s”跳过备份,输入“b”,备份“库名称.sql”为“库名称_backup.sql”,输入“O”时,覆盖原文件,e表示退出

#!/bin/bash

mkdir -p /mnt/mysqldump
DATABASE=`mysql -uroot -pwestos -e "show databases;" | sed '1,2d' |egrep -v "mysql|schema"`
for MYSQL_NAME in $DATABASE
do
        if [ -e /mnt/mysqldump/${MYSQL_NAME}.sql ];then
        read -p "${MYSQL_NAME} has been dumped!
        [S]kip [B]ackup [O]verwrite [E]xit
        please input the action:" ACTION
        case $ACTION in
        s|S)
        ;;
        B|b)
        mysqldump -uroot -pwestos $MYSQL_NAME > /mnt/mysqldump/${MYSQL_NAME}_backup.sql
        ;;
        o|O)
        mysqldump -uroot -pwestos $MYSQL_NAME > /mnt/mysqldump/${MYSQL_NAME}.sql
        ;;
        e|E)
        echo -e "ByeBye"
        exit 0
        ;;
        esac
else
        mysqldump -uroot -pwestos $MYSQL_NAME > /mnt/mysqldump/${MYSQL_NAME}.sql
        echo -e "$MYSQL_NAME is backuped!"
        fi
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值