一、简介
expect是一个免费的编程工具,是一套用来实现自动交互功能的软件,expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的。
expect基础:
命令: send 用于向进程发送字符串
expect 从进程接收字符串
spawn 启动新的进程
interact 允许用户交互,主要用于退出自动化,进入人工交互。
二、基本使用方法:
#!/usr/tcl/bin/expect
set timeout 30
set host “101.200.241.109”
set username “root”
set password “123456”
spawn ssh u s e r n a m e @ username@ username@host
expect “password” {send “$password\r”}
interact
#!/usr/tcl/bin/expect:使用expect来解释该脚本;
set timeout 30:设置超时时间,单位为秒,默认情况下是10秒;
set host “101.200.241.109”:设置变量;
spawn ssh u s e r n a m e @ username@ username@host:spawn是进入expect环境后才可以执行的expect内部命令,它主要的功能是给ssh运行进程加个壳,用来传递交互指令;
expect “password”:这里的expect也是expect的一个内部命令,这个命令的意思是判断上次输出结果里是否包含“password”的字符串,如果有则立即返回;否则就等待一段时间后返回,这里等待时长就是前面设置的30秒;
send “$password\r”:当匹配到对应的输出结果时,就发送密码到打开的ssh进程,执行交互动作;
interact:执行完成后保持交互状态,把控制权交给控制台。
模式-动作
即匹配到一个模式,就执行对应的动作;
exp_continue表示循环式匹配,可以不断循环匹配,输入多条命令;
在expect中,$argc表示参数个数,而参数值存放在**$argv**中;
expect [选项] [ -c cmds] [ [ -[f|b] ] cmdfile] [ args]
选项
-c:从命令行执行expect脚本,默认expect是交互地执行的
示例:expect -c 'expect “\n” {send “pressed enter\n”}
-d:可以输出输出调试信息
案例: 使用SSH登录,免交互式。
#!/bin/bash
expect << EOF
spawn ssh 172.25.010
expect “password:” { send “redhat\r” }
expect “#” { send “touch /tmp.txt\r” }
expect “#” { send “exit\r” }
EOF
或者,另一个版本
#!/bin/bash
for i in 10 11
do
expect << EOF
spawn ssh 172.25.0.$i
expect “password:” { send “redhat\r” }
expect “#” { send “touch /tmp.txt\r” }
expect “#” { send “exit\r” }
EOF
done
案例:ssh 和telnet 连接远程主机.
#!/bin/bash
#******************************************************************************
#Author: Sunny
#Date: 2017-09-03
#FileName: scp.sh
#version: 1.0
#Your change info:
#Description: For
#Copyright©: 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 c h o i c e i n 1 ) e x p e c t − c " s p a w n s c p − r r o o t @ choice in 1) expect -c " spawn scp -r root@ choicein1)expect−c"spawnscp−rroot@ip: f i l e " file " file"file_KaTeX parse error: Expected '}', got '#' at position 43: …t { #̲ \"*assword\" {…passwd\r”; }
“*assword” {set timeout 300; send “KaTeX parse error: Can't use function '\r' in math mode at position 7: passwd\̲r̲\"; } …ip:KaTeX parse error: Expected '}', got '#' at position 38: …t { #̲ \"*assword\" {…passwd\r”; }
“*assword” {set timeout 500; send “$passwd\r”; }
“yes/no” { send “yes\r”; exp_continue; }
}
expect eof”
;;
3)
expect -c "
spawn scp f i l e r o o t @ file root@ fileroot@ip:/root
expect {
# “*assword” {set timeout 500; send “KaTeX parse error: Can't use function '\r' in math mode at position 7: passwd\̲r̲\"; } …passwd\r”; }
“yes/no” { send “yes\r”; exp_continue; }
}
expect eof”
;;
4)
expect -c "
spawn ssh KaTeX parse error: Can't use function '\"' in math mode at position 30: … { \̲"̲yes/no\" { send…passwd\r”; }
}
interact
expect eof"
;;
5)
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