Hey am new to bash scripts and was wondering how would I capture the output of the ssh
command into a bash variable? I looked around and cant seem to get it right. I have tried puts $expect_out(buffer)
but when echo
it says variable does not exist
I know the response should be just one line and if I want to save that into a variable response
and then echo
it how would I do that?
A generic idea can be something like as below.
spawn
the ssh session send
expect
Example:
spawn ssh $user@$domain
expect "password" { send "$pwd\r"}
expect "#"; # This '#' is nothing but the terminal prompt
send "$cmd\r"
expect "#"
puts $expect_out(buffer); #Will print the output of the 'cmd' output now.
The word to wait for after executing the command can vary based on your system. It can be #
or $
or >
or :
; So, make sure you are giving the correct one. Or, you can provide a generalized pattern for the prompt as such
set prompt "#|>|:|\\\$"; # We escaped the `$` symbol with backslash to match literal '$'
While using the expect
after sending the commands, it can be used as
expect -re $prompt; #Using regex to match the pattern`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With