Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have many servers like sdeuu1, sdeuu2, sdeuu3,.. etc.

I want to run a query on each one of them and the query is :

grep -i 'SponsoredLinksIFrame.jsp' /home/nextag/httpd_logs/access_log.1360* | 
  grep -ic 'nextag.co.uk'

this query is returning perfect results when ran on separate servers.

Now i have one server named sdalp1 through which i can ssh to sdeuu1, 2, 3 etc.. and go to that server to run the query. Now instead of doing that i want to run the query from sdalp1 for all the sdeuu servers in one go and i tried the following command and a few other versions of it:

for i in sdeuu1 sdeuu2 sdeuu3 sdeuu4 sdeuu5      \
         sdeuu6 sdeuu7 sdeuu8 sdeuu9 sdeuu10     \
         sdeuu11 sdeuu12 sdeuu13 sdeuu14 sdeuu15 \
         sdeuu16 sdeuu17 sdeuu18; do 
  grep -i 'SponsoredLinksIFrame.jsp' /home/nextag/httpd_logs/access_log.1360* | 
    grep -ic 'nextag.co.uk'"
  echo $i
done

But this command is returning an error for all servers as follows:

-bash: grep -i 'SponsoredLinksIFrame.jsp' /home/nextag/httpd_logs/access_log.1360* | grep -ic 'nextag.co.uk': No such file or directory 

What is the problem here, i am not getting if this is an issue with permissions?

share|improve this question
1  
Putting hostnames in a for-loop does not mean that the enclosed commands will be run at that host. The usual way of executing remote commands is with ssh, e.g.: `ssh sdeuu1 "grep '...' | grep '...'". –  Thor Feb 13 '13 at 9:03
    
ok, but that would mean going to the each server one by one and then executing it, i have done that already and i am looking for a shortcut. What can be done to ssh and for loop together. How would you formulate the query then??? –  vipin8169 Feb 13 '13 at 9:08
1  
See RobieBasaks's answer. –  Thor Feb 13 '13 at 9:50
add comment

1 Answer 1

up vote 2 down vote accepted
for i in sdeuu1 sdeuu2 sdeuu3 sdeuu4 sdeuu5      \
         sdeuu6 sdeuu7 sdeuu8 sdeuu9 sdeuu10     \
         sdeuu11 sdeuu12 sdeuu13 sdeuu14 sdeuu15 \
         sdeuu16 sdeuu17 sdeuu18; do 
  ssh $i grep -i 'SponsoredLinksIFrame.jsp' /home/nextag/httpd_logs/access_log.1360* \| \
    grep -ic 'nextag.co.uk'
  echo $i
done

Note that I added ssh $i to the start of the command inside the loop.

I also added a backslash (\) to the pipe symbol (|), so that the second grep happens on each individual server for efficiency. And then I also needed a backslash at the end of that line to indicate a continuation line, since the escaped pipe symbol no longer does it. When you type this in, make sure you don't put any spaces after that last backslash.

If that's confusing, you can also do it with everything on one line:

for i in sdeuu1 sdeuu2 sdeuu3 sdeuu4 sdeuu5      \
         sdeuu6 sdeuu7 sdeuu8 sdeuu9 sdeuu10     \
         sdeuu11 sdeuu12 sdeuu13 sdeuu14 sdeuu15 \
         sdeuu16 sdeuu17 sdeuu18; do 
  ssh $i grep -i 'SponsoredLinksIFrame.jsp' /home/nextag/httpd_logs/access_log.1360* \| grep -ic 'nextag.co.uk'
  echo $i
done

If you haven't already, you can set up passwordless ssh login so that you don't have to type a password in for each server.

share|improve this answer
    
But escaping the second grep isn't necessary, is it? –  vipin8169 Feb 13 '13 at 10:06
1  
No, it isn't necessary; just more efficient. If you don't do it, then ssh will fetch the extra data and it'll get filtered locally instead. –  Robie Basak Feb 13 '13 at 10:16
    
also semicolon ; is required after every statement or not? –  vipin8169 Feb 13 '13 at 10:16
    
i don't know if the command is not working or taking a lot much time for execution. it is simply stuck with a > symbol on the screen –  vipin8169 Feb 13 '13 at 10:39
1  
I copied the example from yours which I assumed worked to start with without checking. Looks like you had an extra quote in it that should not be there. I've removed it so try again. Press Ctrl-C to get back to the prompt. –  Robie Basak Feb 13 '13 at 11:05
show 4 more comments

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.