Run a command on multiple hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run a command on multiple hosts
# 1  
Old 02-18-2016
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.

Code:
#!/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.
# 2  
Old 02-18-2016
Hi, the while-read loop is taking stdin away from the ssh command. Try:
Code:
while read line <&3
do
  ....
done 3<${HOME}/hosts.txt


Last edited by Scrutinizer; 02-18-2016 at 11:35 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-18-2016
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.

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.

Code:
#!/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.
This User Gave Thanks to gandolf989 For This Post:
# 4  
Old 02-19-2016
Quote:
Originally Posted by Scrutinizer
Hi, the while-read loop is taking stdin away from the ssh command. Try:
Code:
while read line <&3
do
  ....
done 3<${HOME}/hosts.txt

Thanks a lot. Can you please explain me about '<&3' in the script?
# 5  
Old 02-19-2016
The 3 is a file descriptor used to open the input file on, and read from. man bash:
Quote:
Redirecting Input
Redirection of input causes the file whose name results from the expansion of word to be opened for reading on file descriptor n, or the standard input (file descrip‐
tor 0) if n is not specified.

The general format for redirecting input is:

[n]<word
Default fds are 0: stdin, 1: stdout, 2: stderr.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run simple single command on multiple Linux servers?

Hi All, How can i run a single command on multiple servers with or without giving credentials. I have a file(servers.txt) which has got list of servers and i want to run a command lsb_release -dr on all these servers and get output of those servers against each server. I tried below code... (9 Replies)
Discussion started by: darling
9 Replies

2. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

3. Shell Programming and Scripting

How to use a loop for multiple files in a folder to run awk command?

Dear folks I have two data set which there names are "final.map" and "1.geno" and look like this structures: final.map: gi|358485511|ref|NC_006088.3| 2044 gi|358485511|ref|NC_006088.3| 2048 gi|358485511|ref|NC_006088.3| 2187 gi|358485511|ref|NC_006088.3| 17654 ... (2 Replies)
Discussion started by: sajmar
2 Replies

4. UNIX for Dummies Questions & Answers

How to run multiple command in a single line?

Normally i would do this- cd abc ls -ltr I wish to run above command in a single line, like this- cd abc | ls -ltr But above command doesn't works, it simply runs the second command, ignoring the 1st one. :confused: (4 Replies)
Discussion started by: boy18nj
4 Replies

5. Ubuntu

run multiple command at the same time in one window terminal using multiplexer

Hi, I would like to ask if someone knows or accomplished this task in the terminal multiplexer in a single window with multiple splitted pane: In the script run multiple command at the same time in diff splitted pane or simulatneously. As an example: I would like to run iptraf, iotop, htop,... (2 Replies)
Discussion started by: jao_madn
2 Replies

6. Shell Programming and Scripting

Script to run command against multiple specific value in one file

Hi all, I am trying to create a shell script from solaris 10 server to run a command into multiple specific value in one file. The command is related to Oracle/Sun JES2005Q4 directory server. #this is the command, #from path /jes/ds/slapd-rldap1 ./ns-inactivate.pl -h mldap1 -p 389 -D... (12 Replies)
Discussion started by: Mr_47
12 Replies

7. Shell Programming and Scripting

Receiving 'ambiguous redirect' when trying to run command against multiple files

I came across the command string on https://www.unix.com/shell-programming-scripting/141885-awk-removing-data-before-after-pattern.html which was what I was looking for to be able to remove data before a certain pattern. However, outputting the result to a file seems to work on an individual basis... (4 Replies)
Discussion started by: HLee1981
4 Replies

8. UNIX for Dummies Questions & Answers

Command to run multiple commands from a file.

I need a command, which could run mutliple commands from a file. Let's say, I have mv fileA1 fileB1 mv fileA2 fileB2 ..... mv fileA20 fileB20 I put these commands in a file, then I need a command to run the file as a whole so that I don't need to type 20 times... Anyone tell me how to... (8 Replies)
Discussion started by: kaixinsjtu
8 Replies

9. UNIX for Dummies Questions & Answers

How to run multiple command in single command?

Dear Unix Guru, I have several directories as below /home/user/ dir1 dir2 dir3 Each directory has different size. I want to print each directory size (Solaris command du -hs .) Can you please guide me how to achieve this? Thanks Bala (2 Replies)
Discussion started by: baluchen
2 Replies

10. Shell Programming and Scripting

How to run awk command having multiple lines

Hi, Can u see the code below. set xyz = `cat testt1.txt | awk '/-----/{\ print $1 }\ ' | tail -1` I need to execute it in c shell . What is wrong with the above command. When i write everything on a single line then it is working. Can anybody help me . (0 Replies)
Discussion started by: nani_g
0 Replies
Login or Register to Ask a Question