Capture awaits ssh variable output

Hi, new to bash scripts and wondered how can I capture the output of the ssh command into a bash variable? I looked around and could not figure out how to. I tried puts $expect_out(buffer) , but when echo it says the variable does not exist

I know that the answer should be only one line, and if I want to save this in the response variable and then echo , how would I do it?

+5
source share
1 answer

The general idea may be something like the one shown below.

  • spawn ssh session
  • enter correct login
  • Send each command with send
  • Wait for the desired exit with 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 after executing the command may vary depending on your system. It can be # or $ or > or:; So, make sure you give the right one. Or you can provide a generic template for a tooltip per se

 set prompt "#|>|:|\\\$"; # We escaped the `$` symbol with backslash to match literal '$' 

When using expect after sending commands, it can be used as

 expect -re $prompt; #Using regex to match the pattern` 
+4
source

Source: https://habr.com/ru/post/1207158/


All Articles