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_sysAnd 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 -fspawn ssh test@192.168.142.15 ls > ls_from_remotes_sysexpect test@192.168.142.15's password:send password\rinteractThe 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?
Capture stdout from ssh session to local machine
ssh;io redirection;file copy;stdout;expect
The expect command you use:spawn ssh test@192.168.142.15 ls > ls_from_remotes_sysThis, effectively callsexec(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 scripteg if you called this get_ls#!/usr/bin/expect -fspawn ssh test@192.168.142.15 lsexpect test@192.168.142.15's password:send password\rinteractThen you could do get_ls > ls_from_remotes_sys.