基于linux expect处理linux自动化中交互问题

在工作过程中遇到这样的问题,利用IOzone对linux 存储压力测试,测试其存储带宽和IOPS时,无法利用shell脚本向多台linux主机上自动传输文件IOzone,导致shell无法传输文件的原因在于scp传输文件中需要交互的输入yes和password,对于这些交互式的命令在shell无法解决。

通过Google调研,在linux中安装基于tcl的expect很好的解决linux多台主机间传输自动文件中交互问题,下面对此次试验梳理总结。

  • 软件安装
安装gcc

挂载redhat ISO 

mount /dev/cdrom /mnt

yum install gcc(前提条件配置好,yum源到/mnt)

安装tcl

1.下载源码包
wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz

2.解压缩源码包
tar xfvz tcl8.4.11-src.tar.gz

3.安装配置
cd tcl8.4.11/unix
./configure --prefix=/usr/tcl --enable-shared
make
make install

安装expect

1.下载源码包
wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

2.解压缩源码包
tar xzvf expect5.45.tar.gz

3.安装配置
cd expect5.45
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/generic

make
make install
ln -s /usr/tcl/bin/expect /usr/expect/bin/expect

  • 脚本测试

#!/usr/expect/bin/expect
set ip [lindex $argv 0]
set localfile [lindex $argv 1]
spawn scp -r $localfile root@$ip:/root/$localfile
set timeout 300
expect {
 "yes/no" { send "yes\r" ;exp_continue}
 "password:" {send "passw0rd\r"}
}
set timeout 300
send "exit\r"
expect eof

  • 运行脚本


  • 参考资料

http://www.cnblogs.com/daojian/archive/2012/10/10/2718390.html

http://linuxcommand.org/man_pages/expect1.html

更加深入的了解expect可以参考 

Exploring Expect这本书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值