加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – while while中的循环

发布时间:2020-12-15 22:23:44 所属栏目:安全 来源:网络整理
导读:我在bash中使用expect.我希望我的脚本telnet到一个框中,期待一个提示,发送一个命令.如果现在有不同的提示,则必须继续,否则它必须再次发送该命令. 我的脚本是这样的: #!bin/bash //I am filling up IP and PORT1 here expect -c " set timeout -1 spawn tel
我在bash中使用expect.我希望我的脚本telnet到一个框中,期待一个提示,发送一个命令.如果现在有不同的提示,则必须继续,否则它必须再次发送该命令.
我的脚本是这样的:

#!bin/bash  
//I am filling up IP and PORT1 here  
expect -c "    
set timeout -1  
spawn telnet $IP $PORT1  
sleep 1  
send "r"  
send "r"  
set temp 1  
while( $temp == 1){    
expect {  
Prompt1 { send "command" }  
Prompt2 {send "Yes"; set done 0}  
}  
}  
"

输出:

invalid command name "while("  
    while executing  
"while( == 1){"

请帮助我.
我尝试将其更改为[$temp == 1] {

我仍然面临以下错误:

输出:

invalid command name "=="  
    while executing  
"== 1"  
    invoked from within  
"while [  == 1] {  
expect {

解决方法

这就是我实现这个的方法:

expect -c '
  set timeout -1  
  spawn telnet [lindex $argv 0] [lindex $argv 1]  
  send "r"  
  send "r"  
  expect {  
    Prompt1 {
      send "command"
      exp_continue
    }  
    Prompt2 {
      send "Yesr"
    }  
  }  
}  
'  $IP $PORT1

>在expect脚本周围使用单引号来保护期望变量>将shell变量作为参数传递给脚本.>使用“exp_continue”来循环而不是显式的while循环(无论如何你都有错误的终止变量名)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读