Executing remote command and saving input to file

473 views Asked by At

I need to access to multiple hosts through SSH, execute a specific command (show ms info) and capture the output to a file. I need to copy that file back to my linux machine

I want to use ssh and expect to supply the password

My problem is saving the output to a text file and looping around 100 machines simultaneously.

1

There are 1 answers

1
Anders Lindahl On

It is more straightforward than you think:

host1 $ ssh user@host2 ls > remote-output.txt
Enter passphrase for key '/home/user/.ssh/id_rsa':
host1 $ ls
remote-output.txt
host1 $

To do it for multiple hosts, I suggest using ssh-agent and setting up autorization keys:

$ ssh-agent bash
$ ssh-add
Enter passphrase for /home/user/.ssh/id_rsa:
$ for h in host1 host2;do ssh $h ls > $h.txt; done
$ ls
host1.txt host2.txt
$