Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a text file with various IP addresses.

I would like to cycle through each, and issue a command:

cat ips.txt | xargs -L 1 `ssh me@__IP__ echo "text" > file; reboot;`

But I'm unsure how to pass the xargs value to _IP_.

Could someone explain how this could be accomplished?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

You don't need either cat or xargs for this: a simple read in a loop would suffice:

while read -r IP;do
    ssh me@$IP echo "text" > file
    reboot
done < ips.txt

For future reference, what you want for xargs can be achieved with the -I option: you supply a name after -I and any instances of that name in the command itself will be replaced by the arguments xargs receives on the pipe:

... | xargs -L 1 -I myip ssh me@myip 'echo "text">file;reboot'
share|improve this answer
    
Thanks! I was using -I earlier, which was actually rebooting my server by mistake for some reason :) thanks this is very helpful! –  d-_-b Dec 24 '13 at 22:55
2  
@d-_-b This is because the shell (rightfully) interprets the semicolon as terminating the xargs command. To avoid this, quote your arguments: ... shh me@myip 'echo text > file;reboot' –  Joseph R. Dec 24 '13 at 23:05
add comment

With GNU Parallel you can do it in parallel:

parallel --slf ips.txt --nonall 'echo "text" > file; reboot;'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.