0

Here's the problem. I'm reading from my server (centos) visit log and use Sarel Botha's ipv4 address extract grep command from here.

As my server is running a java in nohup to serve connection, and all client's ipv4 address is written in nohup.out.

Combined, if I run

tail -f nohup.out|grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'

then I would have a list of ip address following new record on nohup.out, like

111.111.111.111
222.222.222.222

and each ipv4 address would be output in a single line.

Now, I need to fetch each ipv4 address 'greped' from nohup.out and send it to a n ip geolocation query API, the formats required for query are

GET http://example.com/api/json/111.111.111.111

or

curl http://example.com/api/json/111.111.111.111 

can any one tell me how can I save the ipv4 address, then use it for query, and have the query output following my tail -f command?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Lewis Tsao
  • 13
  • 1

2 Answers2

0

You can pipe your grepped output to a curl command like this:

tail -f nohup.out | grep --line-buffered -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' | while read EVERY_IP
do
    curl "http://example.com/api/json/${EVERY_IP}";
done 

NOTE: do not forget to add --line-buffered option to grep command

Mojtaba Kamyabi
  • 3,440
  • 3
  • 29
  • 50
0
awk '{print "URL"$0}' 

You can get string like URL+ip but I do not know how to query use shell.

You can see this question:
How to implement a pythonic equivalent of tail -F?

Elletlar
  • 3,136
  • 7
  • 32
  • 38
毕洁凯
  • 1
  • 2