bash 期望脚本返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32341234/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:31:34  来源:igfitidea点击:

Expect Script Return Value

bashauthenticationsshexpect

提问by MEric

I'm including simple Expect commands within a bash script (I know I could be just writing a pure Expect script but I would like to get it to work from within bash).

我在 bash 脚本中包含了简单的 Expect 命令(我知道我可能只是在编写一个纯 Expect 脚本,但我想让它在 bash 中工作)。

The script is below:

脚本如下:

#!/bin/bash

OUTPUT=$(expect -c '
spawn ssh [email protected]
expect "password:"
send "dog\r"
')

Upon ssh'ing to the above address, it will return something of the form mihail911's password:on the prompt so I think my expect line is valid. When I run this my script does not print anything. It does not even show the password:prompt. In general, even if I manually provide an incorrect password, I will receive a Incorrect password-type response prompt. Why is nothing printing and how can I get my script to execute properly? I have tried debugging by using the -dflag and it seems to show that at least the first expect prompt is being matched properly.

在通过 ssh 连接到上述地址后,它会mihail911's password:在提示中返回某种形式的内容,所以我认为我的期望行是有效的。当我运行它时,我的脚本不打印任何内容。它甚至不显示password:提示。一般情况下,即使我手动提供了错误的密码,我也会收到Incorrect password-type 的响应提示。为什么没有打印,如何让我的脚本正确执行?我已经尝试使用该-d标志进行调试,它似乎表明至少第一个期望提示匹配正确。

In addition, what values should I expect in the OUTPUTvariable? When I echothis variable, it simply prints the first the first command of the expect portion of the script and then mihail911's password:. Is this what it's supposed to be printing?

此外,我应该在OUTPUT变量中期望什么值?当我使用echo这个变量时,它只是打印脚本期望部分的第一个命令,然后打印mihail911's password:. 这是应该打印的吗?

回答by Dinesh

#!/bin/bash
OUTPUT=$(expect -c '
        # To suppress any other form of output generated by spawned process
        log_user 0
        spawn ssh [email protected]
        # To match some common prompts. Update it as per your needs.
        # To match literal dollar, it is escaped with backslash
        set prompt "#|>|\$"
        expect {
                eof     {puts "Connection rejected by the host"; exit 0}
                timeout {puts "Unable to access the host"; exit 0;}
                "password:"
        }
        send "root\r"
        expect {
                timeout {puts "Unable to access the host"; exit 0;}
                -re $prompt
        }
        send "date\r"
        # Matching only the date cmd output alone
        expect {
                timeout { puts "Unable to access the host";exit 0}
                -re "\n(\[^\r]*)\r"
        }
        send_user "$expect_out(1,string)\n"
        exit 1
')
echo "Expect's return value : $?"; # Printing value returned from 'Expect'
echo "Expect Output : $OUTPUT"

Output :

输出 :

dinesh@MyPC:~/stackoverflow$ ./Meric 
Expect's return value : 1
Expect Output : Wed Sep  2 09:35:14 IST 2015
dinesh@MyPC:~/stackoverflow$