SSH into multiple linux servers

Hi All,

Okay, I need help.
I need to ssh in to multiple linux servers execute certain commands and get them to email and print on the screen when the script is being executed.
So below is my script. Its not working :-(.

#!/bin/bash
#linux/UNIX box with ssh key based login
SERVERS="1.2.3.4"
# SSH User name
USR="abc"
 
# connect each host
for host in $SERVERS
do
ssh $USR@$host
mail -s "1234" abc.xyz@mmmm.com
echo "in centos" 
exit
done

It does prompt for password after entering the password It will enter the appropriate server and wait for some commands to be given :wall:...

I wanted it to go ahead and execute the below mail and echo commands.

Kindly help ..

Thanks
xytiz

ssh user@host <<EOF
echo "Logged into ${HOSTNAME} from \${HOSTNAME}"
# EOF must be at the BEGINNING of the line.  Don't indent it!
EOF

Hi Corona,

I did what you suggested but that wont solve my problem.
The problem i am facing is I need to execute couple of commands after I ssh into the host .

Right now none of the commands Between <<EOF and EOF are getting executed :-(..

But thank you for your reply.

xytiz

The syntax works fine in a variety of shells from bash to dash to pdksh. I can't tell why your code isn't working until you post it. (but I guess that either you messed up the end of the here-document, or you're getting accidental substitution going on.)

Hi Corona,

THANK YOU SO MUCH ..

I finally got it working ..
Here is what i was trying to do ..
ssh-agent , ssh add was creating whole lot of issues ..
I initially thought of puting the servers in a loop call them one by one and execute commands cylicly on all the servers..
Did not have any idea of backquote etc etc ..
I was also wrong to be using pipes when i tried two commands with no relation what so ever... Then I learned the o/p of first command will be the input of another .. Yup I relatively new to Unix too :slight_smile: ..

HARNESS_SCRIPT_GREATNESS="255.255.255.255"

###################################################
for host in $HARNESS_SCRIPT_GREATNESS
do
echo ------------------------------------------
echo $host

##################################################
ssh -t $host 'eval `ssh-agent`;ssh-add;cd /x/y/z/; svn checkout svn+ssh://a/b/c/d';something else ; something else ; something else;
echo ------------------------------------------
done

xytiz

---------- Post updated at 09:48 PM ---------- Previous update was at 06:02 PM ----------

Hi Corona,

I am getting slightly greedy with what my script can do here. So the situation is .

I am trying to run a upgrade script I know the exact folderstructure and the extension of the file (.rpm). But I do not know the file name and There will be only 1 file with .rpm extension in the folder I am interested.

So can i get the file name assigned to a variable ?
sudo rpm -Uvh /a/b/c/d/*.rpm < THIS DOES NOT WORK
sudo rpm -UVH /a/b/c/d/${}.rmp < What should I put in the braces ?

Thanks for your time
xytiz

The line as given should work. The way you're sending it over ssh might not. I can't tell what you're doing from here. For the nth time, please post your code!

In the 'general help' department, I'd try single statements by hand before adding them to the whole domino chain, like ssh username@host ls "/path/to/*.rpm" Until you can get that to work there's no point adding the big rpm command.

If your code gets complex enough, you might want to just send entire, regular script files over ssh and dispense with all this quoting junk.

ssh username@host exec /bin/sh -s arg1 arg2 arg3 < script.sh

Substitute /bin/sh with your shell of choice if you need something specific since the #! line gets ignored when you run shell scripts this way.

Hi Corona,

Sorry, I thought my post was descriptive enough. Please find my code below. I have got it working. The issue was I was trying..

CENTOS="a.b.c.d
x.y.z.w"
ab_rel="v.1"
####################################################
####################################################
for host in $CENTOS
do
echo ------------------------------------------
echo $host

ssh -t $host 'eval `ssh-agent`;ssh-add;cd /home/xytiz/work/; svn co svn+ssh://a/etc/t/g/r/'$ab_rel'; sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/'*'.rpm'
echo -----------------------------------------
done

I was using the below three forms and it would not work at all ..
sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/.rpm <--- Incorrect
sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/"
".rpm <--- Incorrect
sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/${}*.rpm<--- Incorrect

Then I accidentally tried the below
sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/'*'.rpm <--- Correct !

I am still improvising the script to do many more htings like sending emails once all the update has been done etc ..

But THANK YOU VERY MUCH for the help ..
Its a great website and people like you who offer to help out is what makes it great ..

xytiz

sudo rpm -ivh /home/xytiz/work/'$ab_rel'/x/y/build/x86/"*".rpm

This one ought to have worked for the same reason single-quotes did: It prevents your own system from trying to process the * for you before it even gets sent over ssh.

This kind of amplifies my point about testing things in a small way before adding them to a big script. Unrelated problems can easily throw you for a loop unless you know for a fact the statement does work on the remote end.