expect

来源:互联网 发布:先导爱知出现集数 编辑:程序博客网 时间:2024/05/20 04:10

expect的核心是spawn expect send set

spawn    调用要执行的命令
expect    等待命令提示信息的出现,也就是捕捉用户输入的提示:
send       发送需要交互的值,替代了用户手动输入内容
set          设置变量值
interact   执行完成后保持交互状态,把控制权交给控制台,否则会退出,而不是留在远程终端上。
expect eof 这个一定要加,与spawn对应表示捕获终端输出信息终止,类似于if....endif

expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了。

设置expect永不超时
set timeout -1

设置expect 300秒超时,如果超过300没有expect内容出现,则退出
set timeout 300

expect编写语法,expect使用的是tcl语法。

一条Tcl命令由空格分割的单词组成. 其中, 第一个单词是命令名称, 其余的是命令参数
cmd arg arg arg

$符号代表变量的值

方括号执行了一个嵌套命令. 例如, 如果你想传递一个命令的结果作为另外一个命令的参数, 那么你使用这个符号
[cmd arg]

双引号把词组标记为命令的一个参数. "$"符号和方括号在双引号内仍被解释
"some stuff"

大括号也把词组标记为命令的一个参数. 但是, 其他符号在大括号内不被解释
{some stuff}

反斜线符号是用来引用特殊符号. 例如:n 代表换行. 反斜线符号也被用来关闭"$"符号, 引号,方括号和大括号的特殊含义

 

一、ssh登陆

1、查看是否安装expect

[root@test1 ~]# rpm -qa |grep expect
expect-5.44.1.15-5.el6_4.x86_64

2、安装expect

yum install -y expect

3、编写脚本

vim test.expect

...................................................................

#! /usr/bin/expect
set host "ip adress"
set passwd "password"


spawn ssh -p 22 root@$host


expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact 

...................................................................

4、执行脚本2种方式

expect test.expect

chmod a+x test.expect

./test.expect

 

 二、ssh登陆、执行命令后自动退出

vim test.expect

...................................................................

#!/usr/bin/expect

set user "root"

set passwd "123456"

 

spawn ssh $user@192.168.11.18

 

expect {

"yes/no" { send "yes\r"; exp_continue}

"password:" { send "$passwd\r" }

}


expect "]*"

send "touch /tmp/12.txt\r"

expect "]*"

send "echo 1212 > /tmp/12.txt\r"

expect "]*"

send "exit\r"

...................................................................

#执行方式同上

 

三、expect传递参数

vim test.expect

...................................................................

#!/usr/bin/expect
set user [lindex $argv 0]    //传递第一个参数
set host [lindex $argv 1]    //传递第二个参数
set passwd "123456"
set cm [lindex $argv 2]    //传递第三个参数

spawn ssh $user@$host    //引用第一二个参数

expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"    //引用第三个参数
expect "]*"
send "exit\r"

...................................................................

expect test.expect root 192.168.0.123 "ls /tmp/"    //设置3个参数

 

四、自动同步并指定Host和file

vim test.expect

...................................................................

#!/usr/bin/expect

set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]


spawn rsync -av $file root@$host:$file


expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

...................................................................

expect test.expect

 

五、文件分发系统

vim rsync.expect

...................................................................

#!/usr/bin/expect
set passwd "123456"

set host [lindex $argv 0]

set file [lindex $argv 1]


spawn rsync -av --files-from=$file / root@$host:/    //同步文件列表


expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }
}

expect eof

...................................................................

chmod a+x rsync.expect

 

cat ip.list    //同步IP列表

192.168.11.18

192.168.11.19
......

 

vim rsync.sh

...................................................................

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done

...................................................................


0 0
原创粉丝点击