expect返回值给shell,Expect脚本返回值

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$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值