Saturday, May 27, 2017

linux - sequential SSH command execution not working in Ubuntu/Bash

My requirement is I will have a set of commands that needs to be executed in a text file. My Shell script has to read each command, execute and store the results in a separate file.



Here is the snippet which does the above requirement.



while read command
do
echo 'Command :' $command >> "$OUTPUT_FILE"
redirect_pos=`expr index "$command" '>>'`
if [ `expr index "$command" '>>'` != 0 ];then

redirect_fn "$redirect_pos" "$command";
else
$command
state=$?
if [ $state != 0 ];then
echo "command failed." >> "$OUTPUT_FILE"
else
echo "executed successfully." >> "$OUTPUT_FILE"
fi
fi

echo >> "$OUTPUT_FILE"
done < "$INPUT_FILE"


Sample Commands.txt will be like this ...



tar -rvf /var/tmp/logs.tar -C /var/tmp/ Commands_log.txt
gzip /var/tmp/logs.tar
rm -f /var/tmp/list.txt



This is working fine for commands which needs to be executed in local machine. But When I am trying to execute the following ssh commands only the 1st command getting executed.



Here are the some of the ssh commands added in my text file.



ssh uname@hostname1 tar -rvf /var/tmp/logs.tar -C /var/tmp/ Commands_log.txt
ssh uname@hostname2 gzip /var/tmp/logs.tar
ssh .. etc



When I am executing this in cli it is working fine. Could anybody help me in this?

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...