expect
2016-03-17 20:34
183 查看
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
...................................................................
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
...................................................................
相关文章推荐
- 微信和WeChat的合并月活跃账户达6.97亿
- RelativeLayout(相对布局)的分析
- crontab任务计划
- 实验室中搭建Spark集群和PyCUDA开发环境
- 实习半个月的感受
- python之import机制详解
- ireport 中文乱码无法显示
- 自定义Android标题栏修改TitleBar的布局
- hdu1010
- 两个有序单向链表的合并
- STLpair
- chomp 跟 chop
- perl 高级操作符 <=>
- 一行代码解决 perl 出入,排序,输出
- Unity3D 语音接入适用于pc、ios、android
- 解决 Cocos2d-x 3.2 error C1041: 无法打开程序数据库vc120.pdb
- 【BZOJ1009】[HNOI2008]GT考试【KMP】
- 关于腾讯云服务器不能用公网ip访问的解决方案
- 面向对象
- *hdu1078(记忆化搜索)