Script to execute command to get info from multiple servers

Hi,

I want to collect info from a no. of servers whether there grub.conf contain "elevator" parameter or not.

What I want is

sudo cat /etc/grub.conf | grep -q "elevator=noop"; echo $?

If output is "0", I want name of that host in a file called "present"
if its not "0", I want that hostname in a file called "not-present".

Now, Password less key authentication is something I can not do as of now. I am okay with specifying password in script.

Can someone please help me achieving this?

Create a list of servers, call it servers.dat

> ./present
> ./not-present
while read node
do
      ssh $node 'sudo  grep -q  "elevator=noop" /etc/grub.conf'
      if [ $? -eq 0 ] ; then
         echo "$node" >> ./present
      else
         echo "$node" >> ./not-present
      fi
done < servers.dat

This requires that your account be set up to sudo on all of the servers, and that you have set up ssh-keys (from the box where you execute the above script) for your account on all servers in the respective ~/.ssh/authorized_keys files.

Thanks for the reply mate but We don't have key-authentication here,

I want to give password through the script. I guess it could be done through "except" but I dont know how to use except in script.

You could use Expect to handle the password prompting.

Following is my script :

#!/bin/bash
export BASEDIR=/home/whoami
FILE=$BASEDIR/login.txt

MyServer=cat $FILE
MyUser="whoami"
MyPassword="password"

expect -c 'spawn ssh '$MyUser'@'$MyServer' ; expect password ; send "'$MyPassword'\n" ; 'sudo  grep -q  "elevator=noop" /etc/grub.conf' '

      if [ $? -eq 0 ] ; then
         echo "$node" >> ./present
      else
         echo "$node" >> ./not-present
      fi

It never got executed, I just got a million "y" as o/p on screen. Ignore this error .. please tell me what is wrong with script.