1

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?

Thor
  • 3,578
Vipin Verma
  • 5,454
  • 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 09: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??? – Vipin Verma Feb 13 '13 at 09:08
  • 1
    See RobieBasaks's answer. – Thor Feb 13 '13 at 09:50

1 Answers1

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

Robie Basak
  • 15,670
  • But escaping the second grep isn't necessary, is it? – Vipin Verma 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? – Vipin Verma 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 – Vipin Verma 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
  • yes, now it seems to work fine :) – Vipin Verma Feb 13 '13 at 11:08
  • But if i want to enter the whole command in a single line then i'll have to use ; after for, do and echo. is it? – Vipin Verma Feb 13 '13 at 11:09
  • i.e. 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 – Vipin Verma Feb 13 '13 at 11:13
  • for i in lu1 lu2 lu3 lu4 lu5 lu6 lu7 lu8 lu9 lu10; do ssh $i mysql -ulogger -pnextag nextaglogs select * from click2 where external_click_id=-8502077001203266303 \G; done how do i run this query ??? what is the error here?? Please help – Vipin Verma Apr 01 '13 at 12:04