Bash: Script to manage multiple servers that connect with one another.

If you want to do see how servers connect with one another this script will display those connections.
It will SSH to those servers and here is where the second for loop in the script comes to play.

The second for loop is where you can add a command/function/another script to do whatever you need to do on multiple servers.
The caveat with this script is that it does not deploy it self on all servers at the same time.
It goes on one server at a time.


#!/bin/bash
>lsof_output
for server_loop in `cat servelist-tmp`
do
echo "SSHing to server $server_loop"
ssh -qt $server_loop '/usr/sbin/lsof -i' > lsof_output
#It will print the raw lsof data of the remote server
grep $server_loop servelist-tmp >  lsof_output
server_client=`echo $server_loop | awk -F"@" '{print $2}'`
#Breaking down the serverlist to connect to.
awk '{print $8}' del | awk -F">" '{print $2}' | awk -F"." '{print $1}' | grep -v ^* | grep -v localhost | grep -v ^[0-9] | awk -F"-" '{print $1}'  | sed '/^$/d' | sort -u > F_Servers_to_ssh
for server_loop2 in `cat F_Servers_to_ssh`
do
server=`grep $server_loop2 serverlist-tmp;`
ssh $server_loop2 'ls -ltr' > results
done

#cat $server_loop > lsof_output_report;
#cat lsof_output >> lsof_output;
done;

 

 

 

Share Button

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.