Run a command on multiple hosts

I created a script to check for tsm backup status on linux hosts. Script uses a source file to connect to each host and run a dsmc command on each host and write the output back to a output file located on the parent server where the script is running. The script works fine for the first 2 hosts including the parent and then fails with the following error.

Pseudo-terminal will not be allocated because stdin is not a terminal.

#!/bin/bash

fdate=$(date "+%Y-%m-%d")
export curr_host=`hostname`

if [ ! -s ${HOME}/hosts.txt ]
  then
  echo "Hosts file is missing or a zero-byte file. Exiting the process" >> log.txt
  exit 1
  else
 echo $HOSTNAME >> tsm_bkp_$fdate.txt
   dsmc q fi | awk '{print $2,$5}'|sed '0,/----/d' >> tsm_bkp_$fdate.txt

 while read line
  do

  echo $line >> tsm_bkp_$fdate.txt
     ssh -t $line dsmc q fi| awk '{print $2,$5}'|sed '0,/----/d' >> tsm_bkp_$fdate.txt
 done < ${HOME}/hosts.txt
fi

I searched for the above error in other *nix forums and tried using 'ssh -tt or -T' in the script per others suggestions. But I still get the same error.

Thanks.

Hi, the while-read loop is taking stdin away from the ssh command. Try:

while read line <&3
do
  ....
done 3<${HOME}/hosts.txt 
1 Like

You might find it easier to write a bash script that does what you want it to do,
scp it to each of the servers, then run that script remotely with the '-C' parameter.
Here is some sample code.

for line in `cat ${HOME}/hosts.txt`
do
   ssh $line -C "/home/myuser/myscript.sh"
   scp $line:/home/myuser/myscript.log myscript_${line}.log
done

You should use the exec command to redirect all of the output to a log file.
Then you can copy the log file back and look at it after the script finishes
on all of the servers.

#!/bin/bash
exec >/home/myuser/myscript.log
exec 2>&1
# do some stuff
exec 1>&-
exec 2>&-

You should also look at your .bashrc and .bash_profile scripts on the third
server to see if there is anything weird going on. Finally are you running a
different OS on the third system than the first two. If that OS is older that
might explain why it doesn't work there.

1 Like

Thanks a lot. Can you please explain me about '<&3' in the script?

The 3 is a file descriptor used to open the input file on, and read from. man bash :

Default fds are 0: stdin, 1: stdout, 2: stderr.