http://www.cnblogs.com/kevingrace/p/5900303.html
1 概述
expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率
2 语法
expect命令
expect 语法:
expect [选项] [ -c cmds] [ [ -[f|b] ] cmdfile] [ args]
选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:expect -c 'expect "\n" {send "pressed enter\n"}
-d:可以输出输出调试信息
示例:expect -d ssh.exp
expect中相关命令
spawn:启动新的进程
send:用于向进程发送字符串
expect:从进程接收字符串
interact:允许用户交互
exp_continue匹配多个字符串在执行动作后加此命令
expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
expect “hi” {send “You said hi\n"}
匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
expect "hi" { send "You said hi\n" }
"hehe" { send “Hehe yourself\n" }
"bye" { send “Good bye\n" }
匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
expect {
"hi" { send "You said hi\n"}
"hehe" { send "Hehe yourself\n"}
"bye" { send “Good bye\n"}
}
3 脚本
以下是在expect在shell脚本中调用常用的几个用法,自动拷贝脚本,下发脚本,ssh 和telnet 连接远程主机.
!/bin/bash
******************************************************************************
Author: Sunny
Date: 2017-09-03
FileName: scp.sh
version: 1.0
Your change info:
Description: For
Copyright(C): 2017 All rihts reserved
*****************************************************************************
echo "1 copy wang home dir,usage:$0"
echo "2 copy file under wang home,usage: $0 file_to_copy"
echo "3 send file to wang home,usage: $0 file_to_send"
echo "4 login other host by ssh,usage: $0 login_ip"
echo "5 login other host by telnet,usage: $0 login_ip login_user"
read -p "input the remote ip: " ip
read -p "input full path file_name or dir: " file
read -p "input host password: " passwd
read -p "Please input your choice: " choice
case $choice in
- expect -c "
spawn scp -r root@$ip:$file "$file_$(date +%F-%H-%M)"
expect {
# "assword" {set timeout 300; send "$passwd\r"; }
"assword" {set timeout 300; send "$passwd\r"; }
"yes/no" { send "yes\r"; exp_continue; }
}
expect eof"
;; - expect -c "
spawn scp root@$ip:$file /root/
expect {
# "assword" {set timeout 500; send "$passwd\r"; }
"assword" {set timeout 500; send "$passwd\r"; }
"yes/no" { send "yes\r"; exp_continue; }
}
expect eof"
;; - expect -c "
spawn scp $file root@$ip:/root
expect {
# "assword" {set timeout 500; send "$passwd\r"; }
"assword" {set timeout 500; send "$passwd\r"; }
"yes/no" { send "yes\r"; exp_continue; }
}
expect eof"
;;
expect -c "
spawn ssh $ip
expect {
"yes/no" { send "yes\r"; exp_continue; }
"*assword" {set timeout 500; send "$passwd\r"; }
}
interact
expect eof"
;;
- expect -c "
spawn telnet $ip
expect {
"*assword" {set timeout 500; send "$passwd\r"; }
"login" { send "sunny\r";exp_continue; }
}
interact
expect eof"
;;
*)
echo "Your input is wrong,please check"
exit
;;
esac
4 总结
expect可以实际静默的操作,这个在脚本中经常用到,读者根据对应的功能,拷贝相关代码,可以直接把变量换成固定的值,不需要每次运行脚本都需要人工输入变量值,实现静默操作。1 概述
expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率
2 语法
expect命令
expect 语法:
expect [选项] [ -c cmds] [ [ -[f|b] ] cmdfile] [ args]
选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:expect -c 'expect "\n" {send "pressed enter\n"}
-d:可以输出输出调试信息
示例:expect -d ssh.exp
expect中相关命令
spawn:启动新的进程
send:用于向进程发送字符串
expect:从进程接收字符串
interact:允许用户交互
exp_continue匹配多个字符串在执行动作后加此命令
expect最常用的语法(tcl语言:模式-动作)
单一分支模式语法:
expect “hi” {send “You said hi\n"}
匹配到hi后,会输出“you said hi”,并换行
多分支模式语法:
expect "hi" { send "You said hi\n" }
"hehe" { send “Hehe yourself\n" }
"bye" { send “Good bye\n" }
匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:
expect {
"hi" { send "You said hi\n"}
"hehe" { send "Hehe yourself\n"}
"bye" { send “Good bye\n"}
}
3 脚本
以下是在expect在shell脚本中调用常用的几个用法,自动拷贝脚本,下发脚本,ssh 和telnet 连接远程主机.
!/bin/bash
******************************************************************************
Author: Sunny
Date: 2017-09-03
FileName: scp.sh
version: 1.0
Your change info:
Description: For
Copyright(C): 2017 All rihts reserved
*****************************************************************************
echo "1 copy wang home dir,usage:$0"
echo "2 copy file under wang home,usage: $0 file_to_copy"
echo "3 send file to wang home,usage: $0 file_to_send"
echo "4 login other host by ssh,usage: $0 login_ip"
echo "5 login other host by telnet,usage: $0 login_ip login_user"
read -p "input the remote ip: " ip
read -p "input full path file_name or dir: " file
read -p "input host password: " passwd
read -p "Please input your choice: " choice
case $choice in
-
expect -c "
spawn scp -r
"$file_$(date +%F-%H-%M)"
expect {
# \"*assword\" {set timeout 300; send \"$passwd\r\"; }
\"*assword\" {set timeout 300; send \"$passwd\r\"; }
\"yes/no\" { send \"yes\r\"; exp_continue; }
}
expect eof"
;;
- expect -c "
spawn scp
root@$ip:$file /root/
expect {
# \"*assword\" {set timeout 500; send \"$passwd\r\"; }
\"*assword\" {set timeout 500; send \"$passwd\r\"; }
\"yes/no\" { send \"yes\r\"; exp_continue; }
}
expect eof"
;;
- expect -c "
spawn scp $file
root@$ip:/root
expect {
# \"*assword\" {set timeout 500; send \"$passwd\r\"; }
\"*assword\" {set timeout 500; send \"$passwd\r\"; }
\"yes/no\" { send \"yes\r\"; exp_continue; }
}
expect eof"
;;
expect -c "
spawn ssh $ip
expect {
\"yes/no\" { send \"yes\r\"; exp_continue; }
\"*assword\" {set timeout 500; send \"$passwd\r\"; }
}
interact
expect eof"
;;
-
expect -c "
spawn telnet $ip
expect {
"*assword" {set timeout 500; send "$passwd\r"; }
"login" { send "sunny\r";exp_continue; }
}
interact
expect eof"
;;
*)
echo "Your input is wrong,please check"
exit
;;
esac
4 总结
expect可以实际静默的操作,这个在脚本中经常用到,读者根据对应的功能,拷贝相关代码,可以直接把变量换成固定的值,不需要每次运行脚本都需要人工输入变量值,实现静默操作。
这是一个使用shell和expect无需做任何配置一键就实现批量分发密钥和文件的脚本:
!/bin/bash
this scripts comes from linuxidc trainning's student.
function: remote dis ssh key.
version:1.1
. /etc/init.d/functions
file="$1"
remote_dir="$2"
if [[ $# -ne 2 ]];then
echo "usage:$0 argv2"
echo "must have one argvs"
exit
fi
function KNOWN_HOST_REBUILD()
{
确保本机存在known_hosts列表
[ ! -e ~/.ssh/known_hosts ] && mkdir -p ~/.ssh/ && touch ~/.ssh/known_hosts
local i=$1
sed -i "/^${i} /d" ~/.ssh/known_hosts
expect -c "
spawn /usr/bin/ssh linuxidc@${i} echo ok;
expect "yes/no)?";
send "yes\r";
expect eof " >/dev/null 2>&1
return 0
[[ $? -ne 0 ]] && echo "$i know host rebuild fail,maybe the server connect error"
}
function PASS_PASSWD()
{
ip=$1
expect -c "
set timeout -1
spawn ssh-copy-id -i id_dsa linuxidc@$ip
expect "password:"
send "linuxidc123\r"
expect eof" >/dev/null 2>&1
}
function FENFA_id_dsa()
{
for ip in awk '/^[^#]/{print $1}' all_client.txt
do
KNOWN_HOST_REBUILD $ip
PASS_PASSWD $ip
if [[ $? -eq 0 ]];then
action "$ip send id_dsa is successful" /bin/true
else
action "$ip send id_dsa is failed copied" /bin/false
fi
done
}
function FENFA_config()
{
for ip in awk '/^[^#]/{print $1}' all_client.txt
do
port=$(grep $ip all_client.txt|awk '{print $2}')
scp -P${port} -r -p ${file} linuxidc@${ip}:~ >/dev/null 2>&1 && \
ssh -p${port} -t linuxidc@$ip sudo rsync ~/basename ${file}
$remote_dir >/dev/null 2>&1
if [[ $? -eq 0 ]];then
action "$ip send $file is successful!!" /bin/true
else
action "$ip send $file is failed!!" /bin/false
fi
done
}
FENFA_id_dsa
FENFA_config
网友评论