expect的脚本交互异常并且卡死

foxandhuzh 2015-05-07 07:22:46
expect脚本如下所示:

clean_env.exp:

#!/usr/bin/expect

set LOCAL_TYPE [lindex $argv 0]
set FTP_ROOT [lindex $argv 1]
set IP [lindex $argv 2]
set HANDLE [lindex $argv 3]
set USRNAME [lindex $argv 4]
set PASSWD [lindex $argv 5]
set timeout 420

spawn /usr/bin/ssh $USRNAME@$IP
expect {
"(yes/no)?" {send "yes\r"; exp_continue}
"Password" {send "$PASSWD\r"}
"Last login:" {send "\r"}
}
send "\r"
expect "#" {send "ifconfig | grep $IP\r"}
expect "$IP" {send "\r"}
expect "#" {send "cd $FTP_ROOT\r"}
expect "#" {send "dos2unix -q clean_env.sh\r"}
expect "#" {send "chmod 755 clean_env.sh\r"}
expect "#" {send "./clean_env.sh $LOCAL_TYPE\r"}
expect "#" {send "vgremove xxx\r"}
expect {
"\[y/n\]" {send "y\r"; exp_continue}
"#" {send "\r"}
}
send "\r"
send "exit\r"
expect eof

如上脚本,调用命令为:

./clean_env.exp FTP /root/Install_Tool 192.168.4.2 CLEAN_ENV xxxx xxxx

其中,vgremove的效果是:
image-001:~ # vgremove xxxx
Do you really want to remove volume group "xxxx" containing 1 logical volumes? [y/n]:

expect "#" {send "vgremove xxx\r"}
expect {
"\[y/n\]" {send "y\r"; exp_continue}
"#" {send "\r"}
}

期望中,vgremove应该会正常交互,但实际上,输出了一大堆的:
……
image-001:~/Install_Tool # y
If 'y' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found y
-bash: y: command not found
image-001:~/Install_Tool # y
If 'y' is not a typo you can run the following command to lookup the package that contains the binary:
command-not-found y
-bash: y: command not found
image-001:~/Install_Tool # e
If 'e' is not a typo you can run the following command to lookup the package that contains the binary:
image-001:~/Install_Tool #

并且脚本卡死,直到到达超时时间才退出。

百思不得其解,请教各位大牛,是原因导致这个问题?
...全文
1037 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
foxandhuzh 2015-05-07
  • 打赏
  • 举报
回复
expect ssh的时候,send的内容也会被expect捕捉到,导致 expect "#" {send "ifconfig | grep $IP\r"} expect "$IP" {send "\r"} 完全没有效果,因为expect "$IP"会捕获到send的grep $IP。 最初的send "\r"与expect "$IP" {send "\r"}是罪魁祸首,导致后续的expect "#",全部错位,最后 expect "#" {send "vgremove xxxx\r"} expect { "\[y/n\]" {send "y\r"} "#" {send "\r"} } 这里,expect直接从"#"出口出去了。 修改为 expect "#" {send "vgremove xxxx\r"} expect { "y/n\]:" {send "y\r"} } 可以解决问题。 expect "$IP" {send "\r"} 修改为:expect "inet addr:*$IP" {} 可以解决错位问题。 expect ssh的时候,有没有什么好的实践,可以避免这些情况?
foxandhuzh 2015-05-07
  • 打赏
  • 举报
回复
expect: set expect_out(0,string) "/" expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) " \u001b[m\u000f./" 看来是"\[y/n\]",有什么东西被转义了
foxandhuzh 2015-05-07
  • 打赏
  • 举报
回复
如果将 expect "#" {send "vgremove xxx\r"} expect { "\[y/n\]" {send "y\r"; exp_continue} "#" {send "\r"} } 中的exp_continue去掉,则会卡在交互界面上: image-001:~/Install_Tool # ./clean_env.exp FTP /root/Install_Tool 192.168.4.2 CLEAN_ENV root root spawn /usr/bin/ssh root@192.168.4.2 Password: Last login: Thu May 7 19:25:15 2015 from 192.168.4.1 image-001:~ # ifconfig | grep 192.168.4.2 inet addr:192.168.4.2 Bcast:192.168.4.255 Mask:255.255.255.0 image-001:~ # image-001:~ # cd /root/Install_Tool image-001:~/Install_Tool # dos2unix -q clean_env.sh chmod 755 clean_env.sh image-001:~/Install_Tool # chmod 755 clean_env.sh ./clean_env.sh FTP image-001:~/Install_Tool # ./clean_env.sh FTP vgremove xxxx n exit start to exec ./clean_env.sh FTP ... start to clean FTP ... chattr: No such file or directory while trying to stat * finish to clean FTP ... finish to exec ./clean_env.sh ... image-001:~/Install_Tool # vgremove xxxx Do you really want to remove volume group "xxxx" containing 1 logical volumes? [y/n]:

18,777

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧