LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-24-2013, 01:00 PM   #1
wlandymore
LQ Newbie
 
Registered: Dec 2005
Posts: 6

Rep: Reputation: 0
just wondering if my code is sound for running a command remotely on server list


I have a list of IP's that I've put into a file called servers.txt. These boxes all have the same root password for testing this.

Then I was going to run something like:

Code:
file = servers.txt
for i in $file
do
ssh $i -i identityfile.txt 

##run commands here

done
Would that work for this type of exercise or have I missed something?
 
Old 05-24-2013, 01:21 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
No. That will just read:

Code:
for i in servers.txt
do 
ssh servers.txt -i ..
..
I imagine you'll ask how I would do it.

Code:
for i in `cat servers.txt`
do 
ssh $i ....
...

Last edited by szboardstretcher; 05-24-2013 at 01:24 PM.
 
Old 05-24-2013, 11:42 PM   #3
wlandymore
LQ Newbie
 
Registered: Dec 2005
Posts: 6

Original Poster
Rep: Reputation: 0
makes sense...thanks.
 
Old 05-24-2013, 11:58 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
It's recommended to use while+read instead of for loop for reading input from a file:
Code:
#!/bin/bash
file=servers.txt
while read -r ip
do
ssh $ip -i identityfile.txt 
##run commands here
done < $file
Just check this out:
http://mywiki.wooledge.org/BashFAQ/001
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a command that will list all running services baronobeefdip Linux - General 7 10-27-2014 04:48 PM
get list of thread of running process using C api (in my c code) Jodha Linux - Newbie 1 01-29-2010 08:33 AM
Access Denied when I try to login remotely to my SSH server running on Cygwin. andrew_cz General 0 05-02-2006 10:46 AM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 02:38 AM
Command to list all running processes GSX Linux - General 4 02-23-2005 04:42 AM


All times are GMT -5. The time now is 04:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration