1

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 test@192.168.142.15 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 test@192.168.142.15 ls > ls_from_remotes_sys
expect "test@192.168.142.15'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?

  • ssh test@192.168.142.15 'some commands' | tee output_from_remotes_sys – Satō Katsura Jul 21 '16 at 17:01
  • Does not work in expect. File gets saved to remote system. – user53029 Jul 21 '16 at 17:12
  • 1
    Then run ssh with pulic keys instead of expect? – Satō Katsura Jul 21 '16 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 Jul 21 '16 at 17:18
4

The expect command you use:

spawn ssh test@192.168.142.15 ls > ls_from_remotes_sys

This, effectively calls

exec("ssh","test@192.168.142.15","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 test@192.168.142.15 ls
expect "test@192.168.142.15's password:"
send "password\r"
interact

Then you could do get_ls > ls_from_remotes_sys.

  • 1
    though redirecting the output of expect also gets the spawn ssh... and ...password:in the file – ilkkachu Jul 21 '16 at 17:27
  • Tested both and both work. I like the "sh -c" option as it seems to be the cleanest. – user53029 Jul 21 '16 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. – Stephen Harris Jul 21 '16 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.