Technical Discussion
  >> Linux Issues


Register (or login) on our website and you will not see this ad.


  Print Thread
Standard User metalhead41
(knowledge is power) Fri 09-Dec-11 11:06:57
Print Post

For loop help


[link to this post]
 
I'm trying to create a simple for loop to log onto a set of servers defined in a file, cat a file to a temporary directory, copy that file back to one server and then move onto the next.

So far, my loop works (sort of), but I have to manually press ctrl+d do logout of the session for the loop to continue on the next server.

Here is my code:
Text
1
23
45
6
for i in $(cat host.txt)
dossh -q $i
cat /proc/meminfo > /tmp/$i.txtscp -pr /tmp/$i.txt blade38:/tmp/$i.txt
done


I'm sure I'm missing something basic in order to get the loop to continue once it is finished on one server...

Cheers

----------------------------------------------------
We may be human But we're still animals

My Blog | follow me @metalhead41 | Surfin' Bird Dance
Standard User john2007
(legend) Fri 09-Dec-11 11:15:21
Print Post

Re: For loop help


[re: metalhead41] [link to this post]
 
Nothing wrong with the for loop.

I suggest one of the commands within the for loop is the problem. Try commenting out the scp line.
Standard User mixt
(experienced) Fri 09-Dec-11 11:24:47
Print Post

Re: For loop help


[re: metalhead41] [link to this post]
 
A nicer way to do this is the following (and this also works, tested it just now):

Text
1
23
4
for i in $(cat host.txt)
dossh -q $i 'cat /proc/meminfo' | ssh -q blade38 "cat > /tmp/$i.txt"
done


Now on <aaisp.net>
Previous ISPs: Virgin Media (50Mb/Cable), Be* Un Limited, ZeN
Is Linux routing your internet connection?
Need to make BIND geo-aware?

Edited by mixt (Fri 09-Dec-11 11:27:21)


Register (or login) on our website and you will not see this ad.

Standard User john2007
(legend) Fri 09-Dec-11 11:25:19
Print Post

Re: For loop help


[re: metalhead41] [link to this post]
 
Add the command exit after line 5 to return to the host machine. You are still logged in to the remote (in effect this is what you do when you enter ctrl D).
Standard User gavsdavs
(experienced) Sat 17-Dec-11 09:13:38
Print Post

Re: For loop help


[re: metalhead41] [link to this post]
 
In reply to a post by metalhead41:
Text
1
23
45
for i in $(cat host.txt)
dossh -q $i "cat /proc/meminfo" 2> /dev/null > /tmp/$i.txt
scp -pr /tmp/$i.txt blade38:/tmp/$i.txtdone
  Print Thread

Jump to