简体   繁体   English

生成后会出现第一个提示

[英]Expect take first prompt after spawn

I want to make a script that it take as expect first promt after spawn "ssh user@server" . 我想制作一个脚本,它在spawn "ssh user@server"之后首先按照expect

Am example: 我的例子:

I have 5 servers but all differs the initial promt and i can't modify: 我有5台服务器,但所有不同的初始promt我不能修改:

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

You got the point. 你明白了。

This is how i think but i can't figure out how to get that 这是我的想法,但我无法弄清楚如何得到它

    set timeout -1
    spawn "ssh root@server"

    expect "assword:"
    send "password\n"

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

    expect eof

How can i get those promts to a expect script that can recognize that is the promt to follow? 我怎样才能将这些承诺提供给一个可以识别出可以遵循的承诺的期望脚本?

I would just keep an array of regular expressions: 我只保留一系列正则表达式:

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

spawn ssh $user@$host

expect assword:
send "$password\r"

expect -re $prompt_re($host)

Or, you could mash those up into a single regex 或者,你可以把它们混合成一个正则表达式

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

Try this (pseudo code): 试试这个(伪代码):

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

& your expect-script would looks something like: &您的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.

Note: I haven't tested this. 注意:我没有测试过这个。 So apologies for any syntax errors :) 所以语法错误道歉:)

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

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