SSH into multiple linux servers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SSH into multiple linux servers
# 1  
Old 03-04-2011
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 :-(.

Code:
#!/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 Smilie...

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


Kindly help ..

Thanks
xytiz

Last edited by xytiz; 03-07-2011 at 03:46 PM.. Reason: code tags please
# 2  
Old 03-05-2011
Code:
ssh user@host <<EOF
echo "Logged into ${HOSTNAME} from \${HOSTNAME}"
# EOF must be at the BEGINNING of the line.  Don't indent it!
EOF

# 3  
Old 03-07-2011
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
# 4  
Old 03-07-2011
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.)
# 5  
Old 03-07-2011
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 :-) ..


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
# 6  
Old 03-09-2011
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.

Code:
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.

Last edited by Corona688; 03-09-2011 at 12:37 PM..
# 7  
Old 03-09-2011
Java

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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. UNIX for Beginners Questions & Answers

ssh multiple servers

Hi folks. I'm pretty new to unix, while I'm learning a lot I'm finding bash scripting quite confusing. Im sure it's not really, my head just hasn't clicked with it. Anyway, I need a script to loop the ip addresses stored in a file and run a "pgrep <process>" and return the pid or some... (2 Replies)
Discussion started by: MuntyScrunt
2 Replies

3. Shell Programming and Scripting

How to run simple single command on multiple Linux servers?

Hi All, How can i run a single command on multiple servers with or without giving credentials. I have a file(servers.txt) which has got list of servers and i want to run a command lsb_release -dr on all these servers and get output of those servers against each server. I tried below code... (9 Replies)
Discussion started by: darling
9 Replies

4. Shell Programming and Scripting

Appending authorized_keys on multiple servers using ssh

Hi I have an ssh 'for' loop script to login and put a key on multiple servers. I need to append a file on each server but the command which works ok from the prompt does not work via the script. I have cat filename | ssh user@servername "cat >>append.file.name" I have tried to 'spawn' this in... (0 Replies)
Discussion started by: Grueben
0 Replies

5. Shell Programming and Scripting

Script to add new users to a group on multiple servers using SSH

Hi Experts, I am new to scripting. We have around 400 Linux servers in our environment. I want to add a new user to a perticular group on all the servers using SSH. Requirements: 1) Need to take the server names from a text file. 2) Login into each server and check whether perticular... (1 Reply)
Discussion started by: Satya1983
1 Replies

6. Shell Programming and Scripting

Logging in to multiple Linux servers and running the command.

Hi, I am trying to write a script to run a command on multiple linux based servers and get the o/p. I am using ssh to login. It is a celerra box and EMC NAS product. I am able login but i am not able to run nas command nas_pool -size -all the NAS server. I am getting the following error. ... (2 Replies)
Discussion started by: jpkumar10
2 Replies

7. Shell Programming and Scripting

Shell script to connect to multiple ssh servers

Hello, I have access to several linux servers (mostly centos based) located in a DC in another country. from day to day I need to login to each of them to do some work (they dont have gui/window manager installed, I work only from console), or even to just do a check like df -h for disc usage.... (3 Replies)
Discussion started by: MaRiOsGR
3 Replies

8. Shell Programming and Scripting

Shell script to change the password on multiple servers in linux

Can any one please let me know the shell script to change the password for a particular user on multiple linux servers. (2 Replies)
Discussion started by: s_madras
2 Replies

9. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

10. UNIX for Advanced & Expert Users

Script to access multiple linux servers to get system details such as CPU usage

Hi Is there any shell script that accesses multiple linux servers to get details such as CPU usage, RAM used etc. The access of the servers must be parallel not serial in the sense it must ping all the servers at a time to get information.The script has to be triggered from a host system and get... (7 Replies)
Discussion started by: mssrivatsa
7 Replies
Login or Register to Ask a Question