-1

I have to use dig to get the IP for a domain. I can run dig and I get output like the following (using www.google.no for an example):

1
2 ; <<>> DiG 9.10.3 <<>> www.google.no A
3 ;; global options: +cmd
4 ;; Got answer:
5 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2264
6 ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
7 
8 ;; OPT PSEUDOSECTION:
9 ; EDNS: version: 0, flags:; udp: 1280
10 ;; QUESTION SECTION:
11 ;www.google.no.          IN  A
12
13 ;; ANSWER SECTION:
14 www.google.no.       129 IN  A   74.125.232.111
15 www.google.no.       129 IN  A   74.125.232.119
16 www.google.no.       129 IN  A   74.125.232.127
17 www.google.no.       129 IN  A   74.125.232.120
18
19 ;; Query time: 0 msec
20 ;; SERVER: 10.69.201.10#53(10.69.201.10)
21 ;; WHEN: Tue Jan 19 13:37:41 Vest-Europa (normaltid) 2016
22 ;; MSG SIZE  rcvd: 106

How can I obtain just the IP addresses from lines 14-17 of the above output?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Leifus
  • 41
  • 9
  • Possible duplicate of [PHP check if file contains a string](http://stackoverflow.com/questions/9059026/php-check-if-file-contains-a-string) – urfusion Jan 19 '16 at 12:54
  • @urfusion that is not exactly what i am looking for, i have to read a sertant location in the file. since the IP can differ and i am going to use this on over 200 domains – Leifus Jan 19 '16 at 13:10

2 Answers2

1

I managed to solve this - add +short to the end of the dig command:

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Leifus
  • 41
  • 9
0

You can easily parse it with a regex.

$text = "return of dig";
$regex = "/[a-z\.]+\s*[0-9]+\s*IN\s*A\s*(([0-9]+.){4})/g";
preg_match_all($text, $regex, $results);
var_dump($results);
mamadrood
  • 739
  • 5
  • 12