5

How do I ssh into multiple hosts (e.g host1, host2, host3, etc) and cat /etc/fstab to generate report.txt?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
user2503273
  • 51
  • 1
  • 1
  • 2

2 Answers2

6

Yes, you can ssh hostname command and redirect the output to your report.txt

The following script to get this report from all of your hosts. servername.dat contains all the host names.

 #!/bin/sh
 SERVERLIST=servername.dat
 ICMD='cat /etc/fstab'
 while read SERVERNAME
 do
    ssh -n $SERVERNAME $ICMD > $SERVERNAME_report.txt
 done < "$SERVERLIST"
jordanm
  • 41,988
  • 9
  • 116
  • 113
Raza
  • 4,059
  • 7
  • 28
  • 33
6

You can do that like that:

for i in username1@host1 username@host2; do ssh $i cat /etc/fstab >> report.txt; done

Provided that you have ssh public key authentication set on your hosts (host1 & host2), if not you will be prompted for the password for each host.

mzet
  • 411
  • 3
  • 5