Linux 期望脚本 + 如果没有出现,如何忽略字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8818383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expect script + how to ignore strings if not appears
提问by
I write the following expect script in order to automate ssh login to remote Linux machine And run the command "cat /etc/APP_VERSION.txt file"
我编写了以下期望脚本,以便自动 ssh 登录到远程 Linux 机器并运行命令“cat /etc/APP_VERSION.txt file”
Sometime I not asked from ssh command about-
有时我没有从 ssh 命令中询问-
"Are you sure you want to continue connecting (yes/no)?"
So in this case the first expect still wait for the string "(yes/no)?" -:(
所以在这种情况下,第一个期望仍然等待字符串“(是/否)?” -:(
And not continue to the second expect - "password:" ?
而不是继续第二个期望 - “密码:”?
How to resolve this? ,
如何解决这个问题?,
I mean if the line "Are you sure you want to continue connecting (yes/no)?" Isn't appears from ssh
我的意思是如果“您确定要继续连接(是/否)吗?” 不是从 ssh 出现的
Then we must go to the next expect "password:" , but how to do this?
那么我们必须去下一个expect "password:" ,但是怎么做呢?
remark1 - in case the question "Are you sure you want to continue connecting (yes/no)?" , Appears then the expect script work fine without any problem .
备注1 - 如果出现问题“您确定要继续连接(是/否)吗?” ,然后出现预期脚本工作正常,没有任何问题。
remark2 - I can't use timeout positive value (as timeout 10) because ssh itself have delay
备注2 - 我不能使用超时正值(如超时 10),因为 ssh 本身有延迟
. . . .
. . . .
My expect script: (this expect is part of my ksh script)
我的期望脚本:(这个期望是我的 ksh 脚本的一部分)
.
.
.
APP_MACHINE=172.12.6.190
set timeout -1
spawn ssh $APP_MACHINE
expect {
"(Are you sure you want to continue connecting yes/no)?"
{ send "yes\r" }
}
expect password: {send PASS123\r}
expect > {send "cat /etc/APP_VERSION.txt\r"}
expect > {send exit\r}
expect eof
.
.
.
. . . . .
. . . . .
example of ssh from my linux machine
我的 linux 机器上的 ssh 示例
ssh APP_MACHINE
The authenticity of host 'APP_MACHINE (172.12.6.190 )' can't be established.
RSA key fingerprint is 1e:8a:3d:b3:e2:8c:f6:d6:1b:16:65:64:23:95:19:7b.
Are you sure you want to continue connecting (yes/no)?
采纳答案by Kristofer
Set a timeout
设置超时
set timeout 10
设置超时 10
Will wait for the expected line for 10 seconds, and then move on to the next line in the script if not received within the specified timeout.
将等待预期的行 10 秒,如果在指定的超时时间内未收到,则移至脚本中的下一行。
Reference http://linuxshellaccount.blogspot.com/2008/01/taking-look-at-expect-on-linux-and-unix.html
参考 http://linuxshellaccount.blogspot.com/2008/01/taking-look-at-expect-on-linux-and-unix.html
UPDATE: If a timeout is not an acceptable solution you could try using a list of alternative responses to drive the script forward.
更新:如果超时不是可接受的解决方案,您可以尝试使用替代响应列表来推动脚本前进。
expect {
"(Are you sure you want to continue connecting yes/no)?" { send "yes\r"; exp_continue }
password: {send PASS123\r; exp_continue}
}
回答by Felix Yan
You may want to use the pexpect module for Python to do this, as it can provide you with something like a case:)
您可能希望使用 Python 的 pexpect 模块来执行此操作,因为它可以为您提供类似案例的内容:)
For more details, see: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
有关更多详细信息,请参阅:http: //linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
回答by someguy
When you spawn SSH do this:
当您生成 SSH 时,请执行以下操作:
spawn ssh -o "StrictHostKeyChecking no" "$user\@ip"