3

I am having to put together a script that will ssh into devices to run a command such as "show running-config" and save the output to a file on my local machine. I have done similar tasks like this right from the command line and have it save the file to my local system. For example,

ssh [email protected] ls > ls_from_remotes_sys

And the file ls_from_remotes_sys is on my local system. However, I will need to script this and the only way I know how to do that is with expect. So I have put together this:

#!/usr/bin/expect -f

spawn ssh [email protected] ls > ls_from_remotes_sys
expect "[email protected]'s password:"
send "password\r"
interact

The expect script works but the file gets saved to the remote system, which is not what I want.

Question 1 - Why does the file get saved to the local system from command line and why does it get saved to the remote system with expect?

Question 2 - Is there a way to send the file to my local system? (ssh back is not an option)

I was thinking that maybe instead of redirecting into a file I could just have the script output the command results to my screen. So,

Question 3 - If I do this, how can I capture the stdout on my screen from the remote system and send it to a file on the local system?

4
  • 1
    ssh [email protected] 'some commands' | tee output_from_remotes_sys Commented Jul 21, 2016 at 17:01
  • Does not work in expect. File gets saved to remote system.
    – user53029
    Commented Jul 21, 2016 at 17:12
  • 1
    Then run ssh with pulic keys instead of expect? Commented Jul 21, 2016 at 17:13
  • Not an option. However I did just try the script utility. Run "script /path/to/file". Then invoke expect. Then "exit" to stop script and the output is in /path/to/file.
    – user53029
    Commented Jul 21, 2016 at 17:18

1 Answer 1

5

The expect command you use:

spawn ssh [email protected] ls > ls_from_remotes_sys

This, effectively calls

exec("ssh","[email protected]","ls",">","ls_from_remotes_sys")

That means the three parameters (ls, > and the filename) are sent to the remote system; ie the redirection happens on the remote system.

A kludge could be to call it via sh -c "ssh test@....".

Another alternative would be to have the redirection done outside of the expect script

e,g: if you called this "get_ls"

#!/usr/bin/expect -f

spawn ssh [email protected] ls
expect "[email protected]'s password:"
send "password\r"
interact

Then you could do get_ls > ls_from_remotes_sys.

3
  • 1
    though redirecting the output of expect also gets the spawn ssh... and ...password:in the file
    – ilkkachu
    Commented Jul 21, 2016 at 17:27
  • Tested both and both work. I like the "sh -c" option as it seems to be the cleanest.
    – user53029
    Commented Jul 21, 2016 at 17:27
  • @ilkkachu Good point; you can do log_user 0 at the beginning and then log_user 1 before the interact to block that from showing. Commented Jul 21, 2016 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .