我正在尝试在Linux中编写一个期望脚本,该脚本需要完成以下工作。
在步骤1中,我尝试发送一个命令来检查文件是否存在,但是它不起作用
log_user 1
spawn ssh -o "StrictHostKeyChecking=no" $username@$hostname
expect {
..... user and password checks
.....
send "IF EXIST C:\\path\\to\\file\\temp.zip (echo FOUND) else (echo NOTFOUND)\r"
expect "path" {
set result $expect_out(buffer)
puts $result
if{$result=="FOUND"} {
#compare with temp2.zip here
}
}
结果始终包含我正在发送的命令,而不是输出FOUND或NOTFOUND。有人可以让我知道我在做什么错吗?
最佳答案
您的脚本未使用模式匹配,而是尝试手动处理缓冲区,但错误地假设缓冲区将仅包含文本“(NOT)FOUND”。缓冲区实际上将包含自上次使用expect
命令以来收到的所有内容。即使它确实正确匹配了缓冲区(例如,使用string match *NOTFOUND* $result
),它也会受到the echo problem的影响:字符串“FOUND”和“NOTFOUND”在您发送的命令中,很可能由SSH服务器。
脚本的以下修改通过不发送期望的文字字符串来解决回声问题。
它与Windows上的Bitvise SSH服务器一起为我工作。
log_user 1
spawn ssh -o "StrictHostKeyChecking=no" $username@$hostname
# Log in here.
send "set prefix=___\r" ;# Combat the echo problem.
send "IF EXIST C:\\path\\to\\file\\temp.zip (echo %prefix%FOUND) else (echo %prefix%NOTFOUND)\r"
expect {
___NOTFOUND {
error {file not found}
}
___FOUND {
send_user Found!\n
# Do things with the file.
}
}
关于windows - 检查远程文件是否存在,并在Expect脚本中执行其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37032389/