文章目录
dialog 简介
- 1993年0.1版本推出仅支持 --inputbox --msgbox --yesno三个组件
- whiptail 和dialog 一样用于cli图形组件显示,两者语法几乎一致,只是底层依赖库完全不同
- zenity 是基于gtk的gui界面的图形组件
- kdialog 是基于kde的gui界面图形组件
dialog实例代码
msgbox(提示框)
#!/bin/bash
function msgbox(){
local title
local content
title="${1}"
content="${2}"
dialog \
--title "${title}" \
--msgbox "${content}" \
10 50
}
msgbox "this is msg" "Hello word!"
tailbox(信息展示框)
#!/bin/bash
function textbox(){
local title
local filepath
title="${1}"
filepath="${2}"
dialog \
--title "${title}" \
--textbox "${filepath}" \
10 50
}
textbox "this is file content" /etc/os-version
input(用户输入框)
#!/bin/bash
function input(){
local info
info="$(dialog \
--stdout \
--inputbox 'Please input some info:' \
10 50)"
clear
echo -e "Input info is:\n\n\t${info}\n"
}
input
passwd input(密码输入框)
#!/bin/bash
function passwd_input(){
local passwd
passwd="$(dialog \
--stdout \
--insecure \
--passwordbox 'Please input passwd:' \
10 50)"
clear
echo -e "Input passwd is:\n\n\t${passwd}\n"
}
passwd_input
yes no(询问框)
#!/bin/bash
function yes_no(){
dialog \
--title "yes/no" \
--yesno "Please choose yes or no" \
10 30
return_code="$?"
clear
return ${return_code}
}
yes_no
OPTION=${?}
case "${OPTION}" in
0)
state="YES"
;;
1)
state="NO"
;;
255)
state="QUIT"
;;
esac
echo -e "\n\nYou choosed:\n\n\t${state}\n"
file select(文件选择框)
#!/bin/bash
function fselect(){
local filepath
filepath="$(dialog \
--stdout \
--fselect '/home/' \
15 50)"
clear
echo -e "File path is:\n\n\t${filepath}\n"
}
fselect
progress(进度条)
#!/bin/bash
function progress(){
dialog \
--title "this is progress" \
--progressbox \
15 50
}
{
for ((i=0;i<100;i++));do
echo -e "\nThis is test"
sleep 0.5
done
}|progress
program(文本过程展示)
#!/bin/bash
function programbox(){
dialog \
--title "this is progress" \
--programbox \
15 50
}
{
for ((i=0;i<10;i++));do
echo -e "\nThis is test"
sleep 0.5
done
}|programbox
gauge(进度条2)
#!/bin/bash
function gauge(){
dialog --gauge "this is gauge" \
10 50
}
action=(\
"sleep 1"\
"sleep 1"\
"sleep 1"\
"sleep 1"\
)
let progress=(100/${#action[*]})
{
state=0
for (( i = 0; i < ${#action[*]}; i++ )); do
bash -c "${action[${i}]} >/dev/null 2>&1"
echo "${progress}"
let progress=progress+100/${#action[*]}
done
}|gauge
menu(菜单)
#!/bin/bash
function menu(){
OPTION=$(\
dialog\
--backtitle "Main menu V0.1"\
--title "--Menu--"\
--stdout \
--ok-label OK\
--cancel-label EXIT\
--menu "" 15 30 $((${#MAINMENU[*]}/2+2))\
"${MAINMENU[@]}"
)
return ${?}
}
MAINMENU=(\
"1" "show msg box"\
"2" "show text box"
"3" "show input box"\
"4" "show passwd box"\
"5" "show yes & no box"\
"6" "show file select box"\
"7" "show program box"\
"8" "show gauge box"\
)
menu
expect简介
Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。Expect的作者Don Libes在1990年开始编写Expect时对Expect做有如下定义:Expect是一个用来实现自动交互功能的软件套件(Expect [is a] software suite for automating interactive tools)。使用它,系统管理员可以创建脚本来对命令或程序进行输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。甚至可以实现简单的BBS聊天机器人。 😃
Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为系统管理员的的一个强大助手。Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。
expect实例代码
自动连接多个远程ssh
#!/bin/bash
IP_LIST=(
127.0.0.1
10.8.12.102
10.8.12.103
127.0.0.1
)
USER_NAME="uos"
PASSWD="Unit@123"
for ((i=0;i<${#IP_LIST[*]};i++)); do
expect <<EOF
set timeout 1
spawn ssh ${USER_NAME}@${IP_LIST[i]}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"*password:" { send "${PASSWD}\r" }
}
expect -re ".*\[\$#\]" {
send "df -h\r"
}
expect eof
EOF
done
expect脚本传递参数
#!/usr/bin/expect
set timeout 2
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
puts "name --> $username"
puts "passwd --> $password"
puts "ip addr --> $hostname"
puts "----------------------\n"
spawn ssh ${username}@${hostname}
expect "yes/no" { send "yes\r"; exp_continue }
expect "password:" { send "$password\r" }
expect eof
expect if判断
#!/usr/bin/expect
set arg_a "1"
set arg_b "2"
puts "p1 --> ${arg_a}"
puts "p2 --> ${arg_b}"
if { $arg_a > $arg_b } {
puts "p1 great than p2"
} elseif { $arg_a < $arg_b } {
puts "p1 less than p2"
} else {
puts "p1 equal p2"
}
except for
#!/usr/bin/expect
for { set i 0 } { $i < 10 } { incr i} {
sleep 0.5
puts "print i --> $i"
}
except while循环
#!/usr/bin/expect
set arg_i 0
while { true } {
puts "print arg_i --> $arg_i"
incr arg_i 1
if { $arg_i > 10 } {
puts "--end--"
break
}
}