DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 20th January 2009
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Thanked 1 Time in 1 Post
Default Running a command with input from a file.

Hi guys,

Here's what I'm trying to do:

I have a backup server in my network that has passwordless ssh connection to all the servers in the network. I have a script called "load_checker.sh" that I wanna use to monitor the load on all the servers. Now, I wanna copy load_checker.sh to all of the servers in the network.

I have a list of all of the servers in the network, so to make my job easier, it would be alot faster if I could just run the following command from the backup server:

scp load_checker.sh user@$serverlist:/some/directory/load_checker.sh

The variable $serverlist will be all of the servers that are listed in the serverlist file. That way, load_checker.sh will be sent to every server that's listed on the file. The serverlist file will have each server separated by a line, example:

server1
server2
server3
.....

So as you guys may have noticed, I know the concept what needs to be done, but I just don't know what I have to do.

Conclusion: What would be the best way to scp load_checker.sh to all of the servers that are listed in the serverlist file?

Kind regards,
--Bigb89
Reply With Quote
  #2   (View Single Post)  
Old 20th January 2009
phoenix's Avatar
phoenix phoenix is offline
Risen from the ashes
 
Join Date: May 2008
Posts: 699
Thanked 90 Times in 81 Posts
Default

Start a Bourne shell (sh, bash, zsh) and use the for loop:
Code:
# for server in $( cat /path/to/serverlist ); do
scp /path/to/load_checker.sh user@${server}:/some/directory/
done
The $( ) construct tells the shell to run the command(s) between the $( ) and to substitute the values in place of the $( ).

The for loop will replace ${I} with each value returned by $( ).
__________________
Freddie

Help for FreeBSD: Handbook, FAQ, man pages, mailing lists.

Last edited by phoenix; 21st January 2009 at 06:36 PM. Reason: Fix typo in for loop, using $I instead of $server
Reply With Quote
  #3   (View Single Post)  
Old 20th January 2009
bigb89 bigb89 is offline
Fdisk Soldier
 
Join Date: May 2008
Posts: 69
Thanked 1 Time in 1 Post
Default

Thanks Phoenix!

I'm gonna try that out and see how it goes.

Regards
Reply With Quote
  #4   (View Single Post)  
Old 21st January 2009
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 563
Thanked 14 Times in 13 Posts
Default

A minor correction to phoenix' solution: Replace ${I} with ${server} .
Reply With Quote
  #5   (View Single Post)  
Old 21st January 2009
phoenix's Avatar
phoenix phoenix is offline
Risen from the ashes
 
Join Date: May 2008
Posts: 699
Thanked 90 Times in 81 Posts
Default

D'oh! Can you tell how I usually setup my for loops?

Post has been corrected.
__________________
Freddie

Help for FreeBSD: Handbook, FAQ, man pages, mailing lists.
Reply With Quote
Reply

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Input foreign characters under X11 Beastie General software and network 5 30th August 2009 11:51 AM
Command to find and replace, but not creating a new file 18Googol2 Programming 4 22nd September 2008 10:28 PM
how to enable file/command completion for ksh93? TerryP General software and network 1 31st August 2008 08:23 AM
C and file input/output 18Googol2 Programming 3 20th August 2008 04:02 PM
shell script with input c0mrade Programming 5 13th July 2008 04:33 AM


All times are GMT. The time now is 06:24 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick