runcode
runcode

Reputation: 3643

bash expect how to save respond output to a variable during the ssh connection

i ssh into a machine. and i do a send command. I want to capture all those output to a variable.

I tried

output=$(send "rpm -i mypkg.rpm\r")

But it doesnt work. any idea?

Error Message

")": no such variablean't read "(send ""rpm -i mypkg.rpm
while executing
"output=$(send "rpm -i mypkg.rpm\r")"

Upvotes: 0

Views: 591

Answers (2)

Ricky Levi
Ricky Levi

Reputation: 7997

The syntax of creating variables in Expect is for example: set output "some content"

To set the variable output with the last command run you can do:

send "rpm -i mypkg.rpm\r"          // send the command
expect '#'                         // wait for the prompt
set output $expect_out(buffer);    // store the output in 'output' variable

I don't think that Bash syntax will work here ( output=$( command )

Upvotes: 0

Aleks-Daniel Jakimenko-A.
Aleks-Daniel Jakimenko-A.

Reputation: 10653

As already stated by user000001, you have a space character after = which does not work in bash, because bash will interpret it as a command with parameters.
But what is the point of capturing the command output inside ssh session? Most probably you want it from your client machine, so here is the code:

output=$(ssh myhost 'rpm -i mypkg.rpm')

Some programs will freak out if you're executing them this way, that's because there is no terminal. You can force pseudo-tty allocation by using -t flag with ssh.

You have updated your question:

"output=$(send "rpm -i mypkg.rpm\r")" - The problem here is your quoting. You can solve that by mixing different types of quotes. For example:

"output=$(send 'rpm -i mypkg.rpm\r')"

Upvotes: 1

Related Questions