脚本期望太快:在从文件读取的每一行之间添加一个睡眠时间

脚本期望太快:在从文件读取的每一行之间添加一个睡眠时间

我正在尝试自动执行 switch 命令。几乎一切都很好,但是当 expect 脚本从包含我的 switch 命令的文件中读取每一行(逐行列出)时,switch 似乎在大约 10 或 15 个命令后停止,我认为缓冲区太小了。

如何在从文件读取的每个命​​令之间添加睡眠?谢谢!

set fp [open "/home/room.txt" r]
set data [read $fp]

set timeout -60
spawn telnet 10.91.60.14
match_max 100000
sleep 2
expect *
send -- "^Y"
sleep 2
send -- "password\r"
sleep 2
send -- "^[\[A"
send -- "^[\[A"
send -- "\r"
sleep 1
send -- "enable\r"
send -- "configure terminal\r"
sleep 1
expect *
sleep 2
**send -- "$data"**
sleep 2
interact

答案1

expect所以,这是一个比较老的讨论,但经过过去一两天的思考,并收集了来自这里和其他地方的用户的一些有用提示后,我决定发布我发现的内容。我也在Mac 并非原生 Linux为此,有些事情很古怪。此脚本从以下 bash脚本调用:

expect -d <filename>.expect

如果在文件#!<PATH to expect> -f的第一行实现*.expect你:

chmod +x <filename>.expect您的文件将起作用。这-d是附加调试信息。

在您的 bash 脚本中调用expectexpect -df <filename>.expect来实现相同的效果,并且您不需要对文件的可执行权限。

调试信息是非常有助于查看您的expect语句、变量等,如下所示:

spawn <program> <args>
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {29747}

expect: does "" (spawn_id exp10) match glob pattern "Please enter 
passphrase:"? no
Please enter passphrase:
expect: does "Please enter passphrase: " (spawn_id exp10) match glob 
pattern "Please enter passphrase:"? yes
expect: set expect_out(0,string) "Please enter passphrase:"
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "Please enter passphrase:"

这是简短的 bash 脚本,(仅作为示例,但如果您需要它来执行更复杂的事情或只是出于bash某种原因调用它,它会很有帮助)

#!/bin/bash

expect -d exp.expect "$WORD"
RET=$?

if [ $RET -eq 1 ]; then
    #mac specific, sheer laziness, allows me to do other stuff...
    #use esay or similar on Linux
    say "Query was found, expect returned success" 
    echo *************************************************** 
    echo ******************FOUND!***************************
fi
exit 0

脚本如下expect

#!/usr/bin/expect -f
#capture logs for debugging
log_file -a log_file.txt

#dont timeout, let the program run its course, (was mandatory for me)
set timeout -1;

#capture all of our output for debugging
set output [ open "output.txt" "RDWR" ];

#This procedure is called repeatedly with the next word
proc check_word {w} {

#kickoff our other program we are going to talk to
spawn <program> <args>

#we want this one to go regardless, the next 2 are mutex
expect "Please enter passphrase:" { send "$w\r"; send_user "\nSENDING: $w\r"; send_user "\nOutput BUFFER: $expect_out(buffer)\n" }

#These are mutually exclusive, either worked or not, can be grouped
expect {
"scrypt: Passphrase is incorrect" { send_user "\n*FAILED*\n"; close }
-re {anders} { send_user "$expect_out(buffer)\n"; close; exit 1 }
}
#wait for the process to end, timeout is set to never expire
wait
}

#open the file to take the words from, (happens before proc above)
set input [ open "words.txt" "RDONLY" ];

#while there are still words, (we exit on a match) ...keep going....
while {[gets $input word] != -1} {
        check_word $word;
}
#close input file, TODO do this upon success in expect call too?
close $input
close $words

#EOF

希望这可以帮助一些人/任何人节省一些时间!

答案2

使用while循环:

set fp [open "datfile"]
while {[gets $fp line] >= 0} {
  puts $line
  #sleep 3
  # but better to wait for the prompt
  expect #
}

答案3

你的脚本看起来应该是这样的:

set passwd {your_login_password_here}
set en "enable"
#
spawn telnet $rtr
expect {
    timeout { puts " ###   $rtr  TIMEOUT   ###"
              exit
    } 
    "ername:" { send "$env(USER)\r"  
                expect {
                   {*ssword} { send "$passwd\r" }
                }
     }
}
expect {
   {*>}  { send "$en\r"
           expect {
              "ername:" { send "$env(USER)\r"
                         expect {
                             {*ssword} { send "$passwd\r" }
                         }
               }
               "*ssword" { send "$passwd\r" }
           }
   }
}
expect {
   "#"  { send "conf t\r" }
}
set load_cmd [open "open "/home/room.txt" r]
set cmd_list [split [read $load_cmd] "\n"]
close $load_cmd
expect {
        {#} { foreach command $cmd_list {
                if {$command != ""} {
                   send "$command\n"
                }
              } 
        } 
}
interact

相关内容