Running a For Loop over SSH


Most of my tasks involve logging on to multiple servers & fetching details.  And sometimes, these details can be quite exhaustive to fetch 😦 One such scenario was logging on to a few servers & checking details of the users. There were many servers & many users. Logging in manually & checking would take a lot of time. So, I decided to make use of a “for loop” to login to the servers & another for loop to fetch details. The idea was to first enable password-less SSH for my ID across all the servers, get all the host-names in a text file & then getting list of all the users in another text file 😉

Below is how I did it 🙂 /etc/hostnames.txt contains all the servers & /home/shashank/id.txt contains all the users. Both the files need to be present on all servers. You may wish to run a for loop & pass the file to copied through scp. Below command prints user details only.

for hst in `cat /etc/hostnames.txt`; do ssh $hst 'for id in `cat /home/shashank/id.txt` ; do id -a $id; done'; done;

Below command prints hostnames & then the details of users.

for hst in `cat /etc/gridhosts`; do ssh $hst 'hostname; for id in `cat /home/tcs_admin/id.txt` ; do id -a $id; done'; done;

You can also use finger instead of id -a. Make sure it is installed on all the servers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s