shell图形化界面

shell图形化界面

whiptail

在CentOS6系统中,我们可以使用setup指令来修改网卡的IP等信息,交互起来十分方便,在CentOS7系统中,setup命令已经没有了,但是还有nmtui命令,可以让我们修改IP和主机名。

whiptail命令的作用就是可以出现一个图形化界面,并且样式有很多

消息框

#!/bin/bash
whiptail --title "$HOSTNAME disk info" --msgbox "`df -h`" 30 60

--title		# 指定标题内容
--msgbox	# 指定信息内容
30			# 指定消息框为30
60			# 指定信息框宽度为60

$? 0

布尔值选择框

# 布尔值选择框
#!/bin/bash
whiptail --title "你确定要这么做" --yesno "请做出你的选择" 30 60
echo $?

yes :0
no : 1

# 案例
#!/bin/bash
whiptail --title "你的性别"  --yes-button "男" --no-button "女"  --yesno "请做出你的选择" 30 60
echo $?

交互式输入框

语法:
whiptail --title "标题" --inputbox "信息"  <高度> <宽度> <默认值>


#!/bin/bash
a=`whiptail --title "跳板机"  --inputbox "请输入ip地址" 10 60 10.0.0.7 3>&1 1>&2 2>&1 `
if [ $? -eq 0 ];then
        ssh $a
else
        echo 'bey bey'
fi

# 选项
--inputbox: 交互式
10	:高度
60 :宽度
10.0.0.7 :默认值

# 返回值
选择OK : 0
选择cancel : 1

命令本身带输入内容,输入内容可以保存在变量中

密码输入框

pass=`whiptail --title "pass" --passwordbox "请输入你的密码:" 10 60 3>&1 1>&2 2>&3`
if [ $? -eq 0 ];then
        if [ $pass == '1111' ];then
                echo "密码是$pass"
        else
                echo '密码错误'
        fi

else
        echo "bey bey"
fi



# 返回值
选择OK : 0
选择cancel: 1

命令本身带输入内容可以保存在变量中

菜单栏

# 菜单栏
a=$(whiptail --title "家里" --menu "根据菜单选" 30 60 3 \
"1" "lb01"  \
"2" "lb2" \
"3" "lb03" \
"4" "lb04" \
"5" "lb05" \
"6" "lb06"  3>&1 1>&2 2>&3)

--menu		# 菜单栏
30			# 高度
60			# 宽度
3			# 菜单显示多少行内容

# 返回值
OK :0
Cancel 	:1

选择的值会将序号保存在变量中

单选框

# 语法
a=$(whiptail --title "家里" --radiolist "根据菜单选" 30 60 3 \
"1" "lb01" OFF  \
"2" "lb2"  ON \
"3" "lb03" OFF \
"4" "lb04" OFF \
"5" "lb05" OFF \
"6" "lb06" OFF 3>&1 1>&2 2>&3)

# 选项
--rediolist : 单选框
OFF : 默认没有被选中
ON	:默认被选中

多选没有意义,后面的值会覆盖前面的选项

# 返回值
OK :0
Cancel 	:1

多选框

# 语法

a=$(whiptail --title "家里" --checklist "根据菜单选" 30 60 3 \
"1" "lb01" OFF  \
"2" "lb2"  ON \
"3" "lb03" OFF \
"4" "lb04" OFF \
"5" "lb05" OFF \
"6" "lb06" OFF 3>&1 1>&2 2>&3)

进度条

{
        for ((i = 0 ; i <= 100 ; i+=1));do
                sleep 10
                echo $i
        done
} | whiptail --gauge "让子弹飞一会" 6 60 10

作业

搭建图形化跳板机

#!/bin/bash
nginx (){
	ps=`ssh 172.16.1.$1 "ps -ef | grep [n]ginx |wc -l"`
	if [ $ps -ne 0 ];then
		whiptail --title "172.16.1.$1 nginx状态" --msgbox "nginx进程存活 $ps" 30 60
	else
		whiptail --title "172.16.1.$1 nginx状态" --msgbox "nginx不存活 已经没有进程" 30 60
	fi
}
free (){
	ssh 172.16.1.$1 free -m &>/dev/null
	if [ $? -eq 0 ];then
	  whiptail --title "172.16.1.$1 内存" --msgbox "`ssh 172.16.1.$1 free -m`" 30 60
	else
	  whiptail --title "172.16.1.$1 内存" --msgbox "主机不存活" 30 60
	fi
}
key (){
	ls -l ~/.ssh/id_rsa &>/dev/null 
	if [ $? -eq 0 ];then
		key=`ssh 172.16.1.$1 "cat ~/.ssh/authorized_keys |grep -w m01|wc -l"`
		if [ $key == '1' ];then
			ssh 172.16.1.$1
		else
			whiptail --title "你确定要这么做吗?" --yesno "客户端主机还没有创建密钥是否创建" 10 60
			if [ $? -eq 0 ];then
				 sshpass -p 1 ssh-copy-id -o 'StrictHostKeyChecking no' -i ~/.ssh/id_rsa.pub root@172.16.1.$1 &>/dev/null && \
                                 action "172.16.1.$1 send public key " /bin/true  return 1 || \
                                 action "172.16.1.$1 send public key " /bin/false
				 ssh 172.16.1.7
			fi
		fi
	else
		whiptail --title "你确定要这么做吗?" --yesno "主机还没有创建密钥是否创建" 10 60
			if [ $? -eq 0 ];then
			ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa &>/dev/null
		fi
	fi
}

while true;do
	a=$(whiptail --title "菜单" --menu "根据菜单选" 30 60 5 \
	"1" "查看磁盘信息"  \
	"2" "连接虚拟机" \
	"3" "传输文件" \
	"4" "game" \
	"5" "查看内存" \
	"6" "退出" \
	"7" "查看nginx状态" 3>&1 1>&2 2>&3)
	case $a in
	1)
		b=$(whiptail --title "选择要查看的硬盘的主机" --radiolist "根据菜单选" 30 60 3 \
		"1" "web01-172.16.1.7" OFF  \
		"2" "web02-172.16.1.8"  ON \
		"3" "nfs-172.16.1.31" OFF \
		"4" "backup-172.16.1.41" OFF \
		"5" "db01-172.16.1.51" OFF 3>&1 1>&2 2>&3)

		case $b in
			1)
				whiptail --title "$HOSTNAME disk info" --msgbox "`ssh 172.16.1.7 df -h`" 30 60
			;;
			2)
				whiptail --title "$HOSTNAME disk info" --msgbox "`ssh 172.16.1.8 df -h`" 30 60
			;;
			3)
				whiptail --title "$HOSTNAME disk info" --msgbox "`ssh 172.16.1.31 df -h`" 30 60
			;;
			4)
				whiptail --title "$HOSTNAME disk info" --msgbox "`ssh 172.16.1.41 df -h`" 30 60
			;;
			5)
				whiptail --title "$HOSTNAME disk info" --msgbox "`ssh 172.16.1.51 df -h`" 30 60
			;;
		esac		

	;;
	2)
	         c=$(whiptail --title "菜单" --radiolist "你要连接的主机" 30 60 3 \
                "1" "web01-172.16.1.7" OFF  \
                "2" "web02-172.16.1.8"  ON \
                "3" "nfs-172.16.1.31" OFF \
                "4" "backup-172.16.1.41" OFF \
                "5" "db01-172.16.1.51" OFF 3>&1 1>&2 2>&3)	
		case $c in 
			1)
				key 7
			;;
			2)
				key 8
			;;
			3)
				key 31
			;;
			4)
				key 41
			;;
			5)
				key 51
		esac

	;;
	3)
		fire=$(whiptail --title "选择要下发文件的主机" --checklist "请在下面内容选择一项,上下左右移动,空格选中" 20 60 10 \
		"1" "web01-172.16.1.7" OFF \
		"2" "web02-172.16.1.8" OFF \
		"3" "nfs-172.16.1.31" OFF \
		"4" "backup-172.16.1.41" OFF \
		"5" "db01-172.16.1.51" OFF 3>&1 1>&2 2>&3)
		yuan_fire=`whiptail --title "xxxx" --inputbox "请输入源文件名路径:" 10 60 /etc/passwd 3>&1 1>&2 2>&3`
        mu_fire=`whiptail --title "xxxx" --inputbox "请输入目标文件名路径:" 10 60 /etc/passwd 3>&1 1>&2 2>&3`
			for i in $fire;do
			   if [ ${i//\"/''} == '1' ];then
			   	scp  $yuan_fire  172.16.1.7:$mu_fire
			   elif [ ${i//\"/''} == '2' ];then
			   	scp  $yuan_fire   172.16.1.8:$mu_fire	
			   elif [ ${i//\"/''} == '3' ];then
                                scp  $yuan_fire   172.16.1.31:$mu_fire
                           elif [ ${i//\"/''} == '4' ];then
                                scp  $yuan_fire   172.16.1.41:$mu_fire
                           elif [ ${i//\"/''} == '5' ];then
                                scp  $yuan_fire   172.16.1.51:$mu_fire
				
			   fi	
			done
	;;
	4)

	;;
	5)
                d=$(whiptail --title "选择要查看的内存的主机" --radiolist "根据菜单选" 30 60 3 \
                "1" "web01-172.16.1.7" OFF  \
                "2" "web02-172.16.1.8"  ON \
                "3" "nfs-172.16.1.31" OFF \
                "4" "backup-172.16.1.41" OFF \
                "5" "db01-172.16.1.51" OFF 3>&1 1>&2 2>&3)	
		case $d in
			1)
				free 7
			;;
			2)
				free 8
			;;
			3)
				free 31
			;;
			4)	
				free 41
			;;
			5)
				free 51
			;;
		
		esac
	;;
	6)
	pass=`whiptail --title "请输入密码" --passwordbox "请输入你的密码: " 10 60 3>&1 1>&2 2>&3`
	if [ $pass == 'qqqqq' ];then
		exit
	else
		whiptail --title "密码错误" --msgbox "密码错误" 30 30
	fi
	;;	
	7)
                e=$(whiptail --title "选择要查看的nginx的主机" --radiolist "根据菜单选" 30 60 3 \
                "1" "web01-172.16.1.7" OFF  \
                "2" "web02-172.16.1.8"  ON \
                "3" "nfs-172.16.1.31" OFF \
                "4" "backup-172.16.1.41" OFF \
                "5" "db01-172.16.1.51" OFF 3>&1 1>&2 2>&3)
		case $e in
			1)
				nginx 7
			;;
			2)
				nginx 8
			;;
			3)
				nginx 31
			;;
			4)
				nginx 41
			;;
			5)
				nginx 51
			;;		
		esac
	esac
done
posted @ 2022-07-27 19:22  FYytfg  阅读(1064)  评论(0编辑  收藏  举报