期待脚本 +如果没有出现如何忽略字符串
我编写了以下 Expect 脚本,以便自动 ssh 登录到远程 Linux 计算机 并运行命令“cat /etc/APP_VERSION.txt file”
有时我没有从 ssh 命令询问 -
"Are you sure you want to continue connecting (yes/no)?"
所以在这种情况下,第一个期望仍然等待字符串“(是/否)?” -:(
并且不继续第二个期望 - “密码:”?
如何解决这个问题?,
我的意思是如果该行“您确定要继续连接(是/否)吗?” 是不是从 ssh 出现了
那么我们就必须进入下一个 Expect "password:" ,但是如何做到这一点呢?
remark1 - 如果出现“您确定要继续连接吗(是/否)?”的问题,然后出现期望脚本工作正常,没有任何问题 。
remark2 - 我不能使用超时正值(如超时 10),因为 ssh 本身有延迟
。 。 。 。
我的期望脚本:(这个期望是我的 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
.
.
.
。 。 。 。 。
我的 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)?
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"
Sometime I not asked from ssh command about-
"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
Then we must go to the next expect "password:" , but how to do this?
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
.
remark2 - I can’t use timeout positive value (as timeout 10) because ssh itself have delay
.
.
.
.
My expect script: (this expect is part of my ksh script)
.
.
.
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
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置超时时间
将等待预期行 10 秒,如果在指定超时内未收到,则转到脚本中的下一行。
参考
http://linuxshellaccount.blogspot.com/2008/ 01/take-look-at-expect-on-linux-and-unix.html
更新:
如果超时不是可接受的解决方案,您可以尝试使用替代响应列表来推动脚本前进。
Set a timeout
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.
Reference
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.
当您生成 SSH 时,请执行以下操作:
When you spawn SSH do this:
您可能需要使用 Python 的 pexpect 模块来执行此操作,因为它可以为您提供类似 case 的东西:)
有关更多详细信息,请参阅: byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/” rel="nofollow">http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/
You may want to use the pexpect module for Python to do this, as it can provide you with something like a case :)
For more details, see: http://linux.byexamples.com/archives/346/python-how-to-access-ssh-with-pexpect/