Expect脚本返回值 [英] Expect Script Return Value

查看:2005
本文介绍了Expect脚本返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括简单的bash脚本(我知道我可以只是写一个纯粹的Expect脚本,但我想获得它从庆典中工作)。

中期待的命令

该脚本如下:

 #!/斌/庆典输出= $(预计-c'
产卵SSH mihail911@blah.org
预计密码
发送狗\\ R
')

一旦ssh'ing上面的地址,它会返回形式的东西 mihail911的密码:上的提示,所以我觉得我的期望线是有效的。当我运行此我的脚本不打印任何东西。它甚至没有显示密码:提示符。一般情况下,即使我手动提供的密码不正确,我将获得密码不正确型反应迅速。为什么没有打印,我怎么能得到我的脚本正确执行?我已经使用 -d 标记尝试调试,这似乎表明,至少在第一期望提示正在正确匹配。

另外,我应该期望在输出变量什么样的价值观?当我回声这个变量,它只是打印第一个脚本的期望部分,然后的第一个命令 mihail911的密码:。请问这是什么它应该是打印?

感谢您的帮助!


解决方案

 #!/斌/庆典
输出= $(预计-c'
        #共进晚餐preSS通过衍生过程中产生的输出的任何其他形式
        LOG_USER 0
        产卵SSH dinesh@xxx.xxx.xx.xxx
        #为了配合一些常用的提示。更新根据您的需要。
        #要匹配字面美元,这是转义反斜杠
        设置提示#|> | \\\\ $
        预计{
                EOF {看跌期权连接主机拒绝;退出0}
                超时{看跌期权无法访问主机出口0;}
                密码:
        }
        发送根\\ r
        预计{
                超时{看跌期权无法访问主机出口0;}
                -re $提示符
        }
        发送日期\\ R
        #匹配唯独日期CMD输出
        预计{
                超时{看跌期权无法访问主机退出0}
                -re\\ n(\\ [^ \\ r] *)\\ r
        }
        send_user$ expect_out(1,字符串)\\ n
        1号出口
')
回声期待的返回值:$?;从返回#打印值'期待'
回声期望输出:$ OUTPUT

输出:

 迪内希@ MYPC:〜/计算器$ ./Meric
预期的返回值:1
期望输出:星期三09月02日九时35分14秒2015年IST
迪内希@ MYPC:〜/计算器$

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).

The script is below:

#!/bin/bash

OUTPUT=$(expect -c '
spawn ssh mihail911@blah.org
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 -d flag and it seems to show that at least the first expect prompt is being matched properly.

In addition, what values should I expect in the OUTPUT variable? When I echo this 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?

Thanks for the help!

解决方案

#!/bin/bash
OUTPUT=$(expect -c '
        # To suppress any other form of output generated by spawned process
        log_user 0
        spawn ssh dinesh@xxx.xxx.xx.xxx
        # 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$ 

这篇关于Expect脚本返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆