目录
介绍:实用、简单的批处理脚本
expect是处理交互式命令的命令,它可以将类似ssh登录,ftp登录等等需要交互式的场景自动输入登录账号密码, 使其完成自动化运行。
案例一:批量下发远程执行命令
#Remote execution tool
vim remote.sh
#!/bin/bash
_work_dir=/root/tmp
#Build workdir directory
if [ ! -d ${_work_dir} ]
then
mkdir ${_work_dir}
fi
_log_dir=${_work_dir}/log
#Build log directory
if [ ! -d ${_log_dir} ]
then
mkdir ${_log_dir}
fi
touch ${_log_dir}/check.log
_check_msg=${_log_dir}/check.log
function info() {
echo -e "\033[32m [INFO] \033[0m$@ "
}
function warn() {
echo -e "\033[31m [WARN] \033[0m$@"
}
# LAN SERVERS
LANSERVER=(
192.168.31.214,test01
192.168.31.215,test02
) #填写IP与主机名
_REMOTE_UN="" #填写登入用户名
_REMOTE_PW="" #填写登入密码
_ROOT_PW="" #填写root密码
echo -e "\033[36m+++Please enter the shell command to be executed+++\033[0m"
read -p "Command: " _CMD #输入需要执行的指令
# CHECK
for ((i=0; i< ${#LANSERVER[*]}; i++))
do
_REMOTE_IP=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $1}'`
_REMOTE_NAME=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $2}'`
if [ "$_ROOT_PW" = "" ]; then
expect common_user.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_CMD}"
else
expect root_user.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_ROOT_PW}" "${_CMD}"
fi
if [ $? -eq 0 ];then
info "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} ************* Command ************** [success]" |tee -a ${_check_msg}
else
warn "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} ************* Command ************** [failure]" |tee -a ${_check_msg}
continue
fi
done
#remote common_user.exp
vim common_user.exp
#!/usr/bin/expect -f
set timeout 5
set host_name [lindex $argv 0]
set host_ip [lindex $argv 1]
set host_un [lindex $argv 2]
set host_pw [lindex $argv 3]
set CMD [lindex $argv 4]
set login_flag 1
spawn ssh ${host_un}@${host_ip}
expect {
"yes/no" {send "yes\r"; exp_continue}
"password:" {send "${host_pw}\r"}
"Password:" {send "${host_pw}\r"}
}
expect {
"${host_name}" {set login_flag 0 ;send "${CMD}\r"}
expect eof
}
send "exit\r"
expect eof
exit $login_flag
#remote root_user.exp
vim root_user.exp
#!/usr/bin/expect -f
set timeout 5
set host_name [lindex $argv 0]
set host_ip [lindex $argv 1]
set host_un [lindex $argv 2]
set host_pw [lindex $argv 3]
set root_pw [lindex $argv 4]
set CMD [lindex $argv 5]
set login_flag 1
spawn ssh ${host_un}@${host_ip}
expect {
"yes/no" {send "yes\r"; exp_continue}
"password:" {send "${host_pw}\r"}
"Password:" {send "${host_pw}\r"}
}
expect {
"${host_name}" {set login_flag 0 ;send "su -\r"; exp_continue}
"password:" {send "${root_pw}\r"}
"Password:" {send "${root_pw}\r"}
}
expect {
"*${host_name}*#" {set login_flag 0 ;send "${CMD}\r"}
expect eof
}
send "exit\r"
expect eof
exit $login_flag
执行结果如下:
案例二:批量下发文件
vi scp_file.sh
##################################################################
#!/bin/bash
_work_dir=/root/tmp
#Build workdir directory
if [ ! -d ${_work_dir} ]
then
mkdir ${_work_dir}
fi
_log_dir=${_work_dir}/log
#Build log directory
if [ ! -d ${_log_dir} ]
then
mkdir ${_log_dir}
fi
touch ${_log_dir}/check.log
_check_msg=${_log_dir}/check.log
function info() {
echo -e "\033[32m [INFO] \033[0m$@ "
}
function warn() {
echo -e "\033[31m [WARN] \033[0m$@"
}
# LAN SERVERS
LANSERVER=(
192.168.31.214,test01
192.168.31.215,test02
) #填写IP与主机名
_REMOTE_UN="luojie" #填写登入用户名
_REMOTE_PW="123456" #填写登入密码
_SRC_FILE="1.txt" #填写源文件
_DST_FILE="/tmp/1.txt" #填写目的文件
echo -e "\033[36m+++ shell SCP to be executed+++\033[0m"
# CHECK
for ((i=0; i< ${#LANSERVER[*]}; i++))
do
_REMOTE_IP=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $1}'`
_REMOTE_NAME=`echo "${LANSERVER[$i]}" |awk -F ',' '{print $2}'`
expect myscp.exp "${_REMOTE_NAME}" "${_REMOTE_IP}" "${_REMOTE_UN}" "${_REMOTE_PW}" "${_SRC_FILE}" "${_DST_FILE}"
if [ $? -eq 0 ];then
info "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} ************* SCP ************** [success]" |tee -a ${_check_msg}
else
warn "`date +'[%Y-%m-%d %H:%M:%S]'` Check ${_REMOTE_IP} ************* SCP ************** [failure]" |tee -a ${_check_msg}
continue
fi
done
#################################################################
vi myscp.exp
#!/usr/bin/expect -f
set timeout 5
set host_name [lindex $argv 0]
set host_ip [lindex $argv 1]
set host_un [lindex $argv 2]
set host_pw [lindex $argv 3]
set src_file [lindex $argv 4]
set dst_file [lindex $argv 5]
spawn scp ${_src_file} ${host_un}@${host_ip}:${_dst_file}
expect {
"yes/no" {send "yes\r"; exp_continue}
"password:" {send "${host_pw}\r"}
"Password:" {send "${host_pw}\r"}
expect eof
}
send "exit\r"
expect eof
执行结果: