nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

run script on remote machine

This is a discussion on run script on remote machine within the Shell scripting forums, part of the Development/Scripting category; Hello, I use a script to run commands on several servers. manually add the key exec ssh-agent bash ssh-add /root/.ssh/key ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-06-2009, 06:06 PM
Junior Member
User
 
Join Date: Jul 2008
Location: SM
OS: CentOs
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
cosminnci is on a distinguished road
Default run script on remote machine

Hello,

I use a script to run commands on several servers.
manually add the key
exec ssh-agent bash
ssh-add /root/.ssh/key
then run the script:

SERVERS="server1 server2 server3 ..."
for b in $SERVERS
do
echo $b;
ssh -tq $b echo "test message $HOSTNAME" |mail mail @... -s "tstmsg";

done

usually running a grep works great, but the above command fails, more exactly repeats for each SERVERS on the server it is being ran, and not on the remote machine.

what would be the solution?
any other optimization to the above would be appreciated.

regards,
Reply With Quote
  #2 (permalink)  
Old 26-10-2009, 06:04 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hello,

Give this a try.


Code:
#!/bin/bash

for servers in "server1" "server2" "server3" ; do

ssh $servers echo "test message $HOSTNAME" |mail mail @... -s "tstmsg";


done
Play with the semi colon in red. Not sure if it is needed.

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #3 (permalink)  
Old 29-10-2009, 02:27 AM
Junior Member
User
 
Join Date: Jul 2008
Location: SM
OS: CentOs
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
cosminnci is on a distinguished road
Default

Hello,

same thing,
I use this script for find, grep, cat
but it seems there is something with echo and |
Reply With Quote
  #4 (permalink)  
Old 29-10-2009, 04:54 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Try something like this. I used 2 test servers.

Run it like this:

Here is the script.

Code:
Prompt>./script.sh w
I ran the command w on 2 servers with ssh keys setup.

Code:
#!/bin/bash

if [ -z "$1" ]; then
    echo -e usage: "Command \n" 
    exit
fi



#loop through servers executing commands
for server in "radio25" "radio27" ;
do
  echo -e "$server \n"
  ssh -l root $server "$1"
done
Here is the output.


Code:
[root@radio5 bin]# ./jason.sh w
radio25 

 19:20:03 up 260 days,  7:30,  0 users,  load average: 0.58, 0.85, 0.79
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
radio27 

 19:20:03 up 260 days,  9:01,  0 users,  load average: 0.69, 0.63, 0.64
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
[root@radio5 bin]#
Also this works:

Code:
[root@radio5 bin]# ./script.sh "echo testing >> jason*"
radio25 

radio27
Code:
[root@radio25 ~]# cat jason1 
testing
[root@radio25 ~]#
Code:
[root@radio27 ~]# cat jason2
testing
[root@radio27 ~]#



HTH,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 29-10-2009 at 05:10 AM.
Reply With Quote
  #5 (permalink)  
Old 29-10-2009, 11:05 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Scripting language: BASH - Learning Ruby
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Well,

A little birdie told me to check your mail command. It appears to be repeated twice. This may be your issue.

Try this.

Code:
SERVERS="server1 server2 server3 ..."
for b in $SERVERS
do
echo $b;
ssh -tq $b echo "test message $HOSTNAME" |mail mail @... -s "tstmsg";

done

Remove the mail in red. And try again. Everything else looks good.
HTH,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Script for Linux Machine in Windows Network imran83 Shell scripting 1 02-06-2009 04:36 PM
Script to delete a set of files in Remote machine ashok333 Shell scripting 4 09-05-2008 07:24 PM
script hangs when a remote server is down vikas027 Shell scripting 2 11-11-2007 03:47 PM
Shell script to perform operation on remote server vivekv Shell scripting 3 24-10-2007 12:10 AM
Shell script to check the disk space on remote systems vijayscripts Shell scripting 5 21-10-2007 06:29 PM


All times are GMT +5.5. The time now is 07:36 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38