繁体   English   中英

生成后会出现第一个提示

[英]Expect take first prompt after spawn

我想制作一个脚本,它在spawn "ssh user@server"之后首先按照expect

我的例子:

我有5台服务器,但所有不同的初始promt我不能修改:

[root@test home]#
root@server2:~$
User: root Server: server3 ~ !

你明白了。

这是我的想法,但我无法弄清楚如何得到它

    set timeout -1
    spawn "ssh root@server"

    expect "assword:"
    send "password\n"

#
    var=getpromt
#   
    expect "$var"
    send "stuff\n"

    expect eof

我怎样才能将这些承诺提供给一个可以识别出可以遵循的承诺的期望脚本?

我只保留一系列正则表达式:

array set prompt_re {
    test    {#\s*$}
    server2 {$\s*$}
    server3 {!\s*$}
}

spawn ssh $user@$host

expect assword:
send "$password\r"

expect -re $prompt_re($host)

或者,你可以把它们混合成一个正则表达式

expect -re {[#$!]\s*$}   ;# expect the prompt.

试试这个(伪代码):

echo commands-to-be-executed-on-ssh | expect-script

&您的expect-script看起来像:

set timeout -1
spawn "ssh root@server"

expect "assword:"
send "password\n"
interact # <~~~~~~~~~~~ At this point, expect would pass the redirected/piped stdin to ssh process.

注意:我没有测试过这个。 所以语法错误道歉:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM