技术流ken

运维拯救世界

shell脚本案列–技术流ken

1.系统指标巡检

[root@ken1 ~]# cat check_sys.sh 
#!/bin/bash
menu(){
cat <<EOF
+---------------------------------------------+
|		日常巡检脚本		      |
|		1.查看磁盘使用率	      |	
|		2.查看内存使用率	      |
|		3.查看系统负载		      |
|		4.统计系统连接数	      |
|		5.统计系统进程数              |
|		6.统计当前磁盘分区信息        |
|		7.按q退出                     |
|		8.按h帮助                     |
+---------------------------------------------+
EOF
}
menu
while true
do
echo -en "\033[34m请输入你要查看的内容:\033[0m"
read  action
	if [ "$action" == "q" ];then
		echo "quiting...."
		break
	fi
	if [ "$action" == "h" ];then
		menu
	fi
	case $action in 
	1)
		df -h
		;;
	2)
		free -h;;
	3)
		uptime;;
	4)
		ss -tan | grep -i "estab" | wc -l ;;
	5)
		ps aux | grep -v "grep" | wc -l;;
	6)
		fdisk -l ;;		

	*)
		echo "Usage: $0 {1|2|3|4|5|6|q|h}"
	esac
done

 

 

2.自动创建用户脚本

[root@ken1 ~]# cat user_addv2.sh 
#!/bin/bash
user (){
printf "
+----------------------------------------------------------------------------+
|	                        自动创建用户小程序			     |
|	1.请在如下的提示中输入用户名前缀,用户密码,用户数量。例如:ken 123 5|
|	2.按q退出							     |
|	3.按h提供帮助信息                                                    |
|  			制作者:1904班全体同学				     |
+----------------------------------------------------------------------------+
"
}
clear
user
while true
do
	
	echo -en "\033[34m请输入用户前缀、密码及数量: \033[0m"
	read name pass num
	if [ "$name" == "q" ];then
		break
#		user 
#	else
#		echo -en "\033[35m请输入q退出,h帮助,或输入正确格式!\033[0m\n请重新运行该程序!\nquiting...\n"
#		break
	fi
	if [ "$name" == "h" ];then
		user
		continue
	fi
	if [ -z "$name" -o -z "$pass" -o -z "$num" ];then
		echo -en "\033[35m请输入q退出,h帮助,或输入正确格式!\033[0m\n请重新运行该程序!\nquiting...\n"
		break
	fi	
	for i in `seq -w $num`
	do
		useradd $name$i &>/dev/null
		if [ $? -eq 0 ];then
			echo "$name$i is created."
		else
			echo "useradd: user $name$i already exists"
		fi
		echo $pass | passwd --stdin $name$i &>/dev/null
	done
done

 

3.跳板机案例

[root@ken1 ~]# cat jump_server.sh 
#!/bin/bash
trap "" HUP INT QUIT
web1=192.168.64.6
web2=192.168.64.7
mysql=192.168.64.10
menu () {
printf "
+------------------------------------------------------------+
|	`echo -en "\033[33m请输入你要登录管理的主机\033[0m"`                             |
|		1.web1	                                     |
|		2.web2					     |
|		3.mysql
|		4.管理员入口				     |
|	`echo -en "\033[36m1904班研发跳板机应用程序\033[0m"`                             |
+------------------------------------------------------------+
"
}
while true
do
clear
menu
echo -en "\033[31m请输入你要登录主机编号:\033[0m"
read num
	case $num in 
	1)
		ssh  $web1;;
	2)
		ssh $web2;;
	3)
		ssh $mysql;;
	4)
		read -s -p "请输入操作密码:" pass
		pre_pass=`cat /root/.pass`
		if [ $pass -eq $pre_pass ];then
			break
		fi
		;;
	*)
		echo "请输入正确编号!"
	esac
done

 

发表评论

电子邮件地址不会被公开。