LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   just wondering if my code is sound for running a command remotely on server list (https://www.linuxquestions.org/questions/linux-newbie-8/just-wondering-if-my-code-is-sound-for-running-a-command-remotely-on-server-list-4175463332/)

wlandymore 05-24-2013 12:00 PM

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?

szboardstretcher 05-24-2013 12:21 PM

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


wlandymore 05-24-2013 10:42 PM

makes sense...thanks.

shivaa 05-24-2013 10:58 PM

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


All times are GMT -5. The time now is 11:20 AM.