Remotly running the script

Hi Experts,

I have one script which I have to run on multiple servers.
One way is to keep that script on every unix box and run it. I am looking for some best practise to do it. Like remotly login to the server using credentials and execute the script.

below is the snippet from the script:-

 
grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' Myfile | awk '{for(i=2;i<=NF;i++)print $1,$i}' > temp
printf "Host/AliasName\t\t\t\t\t\tIPAddress\t\t\tResolved\tReplied\n" >log.txt
while read ip host
do
ip1=$(ping -q -c 1 -t 1 "$host" |grep PING | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
ping_stat=`ping -c 4 useipapd01 | grep loss | awk '{print $6}'`
if [[ "$ip1" = "$ip" && "$ping_stat" = "0%" ]]
then
echo "$host $ip Y Y" | awk '{printf "%-50s\t%-15s\t%8s\t%8s\n",$1,$2,$3,$4}' >>log.txt
else
echo "$host $ip N N"| awk '{printf "%-50s\t%-15s\t%8s\t%8s\n",$1,$2,$3,$4}' >>log.txt
fi

I came to know about SSH but in the script itslef i have to give username and password. Is there a way to read the servers name, username and password from the file and login to the server and execute that script mentioned above.

Thanks in advance.

Keeping password on a file is not a best practice. If you need to give password in clear text, i guess you can use expect.

Thanks Rajamadhavan.

But question here is like same script I have to run on 5 servers. So thinking of keeping the script on one server and remotely log in to rest of five servers and execute it.
Any guidance on how we can do it in shell scripting?

Hi, u can use ssh key sharing. In this method, u can share ssh keys among specific users in the client and server machines. Then place the scripts in each on the client servers. Then finally execute the remote script using ssh.
That the simplest and best or the next option is doing something with TCP.

i strongly suggest you use one script and run it on the remote servers using ssh ... this way you only have to maintain 1 version of the script ... you can make copies of the script on some backup on a remote server just to make sure you do not lose work should you lose your "primary" server ...

1 Like

Thanks Just Ice,

I am keeping the host file on one server and running the script using the below command.

 
 ssh hostname@useipapd03 'bash -s' < TestPing.sh

it is running fine but problem is in my script am logging output results to one file. But that file is getting generated in the remote server. So if am dng it for 10 servers I have to log into 10 servers and see the output results.

Is there any way to log the output of script into the local server instead of output server.

you have some options ...

  1. have your remote commands' output sent to the console and redirect all output from ssh command to a logfile. you may want to use some kind of separator to make compiled logfile reading easier.
for host in $(< host.list)
do
     echo "\n=== $host ===\n"
     ssh -n root@$host "cd /dir; pwd; ls -lrt"
done > /dir/log
  1. have your script collect the remote hosts' log files at the end of processing
for host in $(< host.list)
do
    ssh -n root@$host "hostname; some_command > /dir/log;"
    scp -p root@$host:/dir/log /localdir/log.$host
done

whatever suffises ur need is the best solution. But if u keep it in remote machines, its easier to customise them according to it. So i prefer that.