1

I'm using the following script to loop through a list of servers then output free disk space info. SSH keys are already in place and working.

But it's only outputting the first server name, then existing...I'm stumped.

#!/bin/bash

PATH=/bin:/usr/bin:/usr/sbin
export PATH

while IFS='|' read hostname nickname; do
    echo "$hostname: $nickname"  
    ssh $hostname "df -Pkhl"

done < server-list.dat

And the list:

% cat server-list.dat
serverone|ONE
servertwo|TWO
serverthree|THREE
Mat
  • 202,337
  • 40
  • 393
  • 406
Dan
  • 931
  • 2
  • 18
  • 31
  • Does the host you are connecting to require a password? – lurker Apr 07 '15 at 22:42
  • No passwords are being used -- SSH keys, and these are working. – Dan Apr 07 '15 at 22:44
  • I'm pretty sure questions essentially the same as this have been asked, and answered, before. Finding it, of course, will be hard -- unless the Related Questions list helps out, and it does. – Jonathan Leffler Apr 07 '15 at 22:49

1 Answers1

1

This happens because you are attaching stdin to server-list.dat, but ssh also expects to read from stdin.

To solve this, you can open the file with a different descriptor:

while IFS='|' read -u 3 hostname nickname; do
    echo "$hostname: $nickname"  
    ssh $hostname "df -Pkhl"

done 3< server-list.dat

The use of -u 3 and 3< opens server-list.dat as file descriptor 3.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285