1

I'm trying to get the address from the below result of my nslookup

Here I want to get is the 2nd address which is 10.0.45.45

Server:         10.152.183.10
Address:        10.152.183.10#53

Name:   pg-master-0.pg-master-headless.postgres.svc.cluster.local
Address: 10.1.45.45

Here is my code

MASTER_HOST=$(nslookup pg-master-0.pg-master-headlesss | awk '/^Address:/ {A=$2}; END {print A}');

echo $MASTER_HOST

Unfortunately, my output is:

10.152.183.10#53

Here I'm logged into the pod enter image description here then ran the nslookup that way.

Jayson Gonzaga
  • 140
  • 1
  • 4
  • 11

2 Answers2

1

If you want the 2nd "Address:" from the nslookup output, you can simply do:

awk '/^Address/{n++; if (n==2){print $2; exit}}'

Which checks if the line begins with Address, then increments a counter n++ and when n == 2 it outputs the second field and exits.

Example Use/Output

With your data in the file called nslookup.txt, you would receive the following:

$ awk '/^Address/{n++; if (n==2){print $2; exit}}' nslookup.txt
10.1.45.45

Of course, using nslookup you would just pipe the output to awk. For example, if I wanted the IP of the machine valkyrie on my local subnet, I would use:

$ nslookup valkyrie | awk '/^Address/{n++; if (n==2){print $2; exit}}'
192.168.6.135

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • I tried this ```MASTER_HOST=$(nslookup pg-master-0.pg-master-headlesss | awk '/^Address/{n++; if (n==2){print $2; exit}}');``` then output it ```echo $MASTER_HOST``` but it gave me blank result – Jayson Gonzaga Aug 04 '20 at 13:28
  • Show me the full output of `nslookup pg-master-0.pg-master-headlesss` Add it to your question -- at the end. There is something different about the `nslookup` output. If you get blank output, that is because there is no 2nd `Address` at the beginning of a line. – David C. Rankin Aug 04 '20 at 14:13
  • Hi David, I addd screenshot to the question. to show how i ran the ```nslookup``` – Jayson Gonzaga Aug 04 '20 at 14:27
  • No reason what is shown in the screenshot shouldn't have worked the same way? If there maybe whitespace before `"Address"`, you can change the regex to `/^\s*Address/` to handle that possibility. But I don't see whitespace with `nslookup` here. – David C. Rankin Aug 04 '20 at 15:21
  • The work around that i could think of is this ```nslookup pg-master-0.pg-master-headless | awk 'FNR == 5 {print $2}'``` which gives me the 5th line and the second parameter that is the address i'm looking for however would like to check the possiblity if the return i get is really comes from ```Address``` – Jayson Gonzaga Aug 04 '20 at 16:25
  • Yes, you really need to anchor to `Address` at the beginning of the line. Another way would be `$1 ~ /Address/` as the check to verify the first field contains `"Address"`. Try `nslookup host | awk '$1~/Address/{n++; if (n==2){print $2; exit}}'` and see if that doesn't remedy things. You can add an `END` rule for some error checking `END {if (n<2) print "2nd Address not found"}` – David C. Rankin Aug 04 '20 at 16:51
1

It looks like nslookup cannot resolve pg-master-0.pg-master-headlesss. You might be running the operation from a different pod or from your personal server/machine which would have no idea of the services running in a cluster. (Your laptop doesn't use CoreDNS in your K8s cluster)

You can try running the script from the pod in your cluster and with the full FQDN to be safe:

$ kubectl run -i --tty --rm debug --image=ubuntu --restart=Never -- bash
#
# apt update; apt -y install dnsutils # Installs dnslookup ...
# export MASTER_HOST=$(nslookup pg-master-0.pg-master-headless.postgres.svc.cluster.local | awk '/^Address:/ {A=$2}; END {print A}'); echo $MASTER_HOST"
Rico
  • 58,485
  • 12
  • 111
  • 141
  • i tried doing ```exec``` in my 2nd pod then tried ```nslookup``` and it gave me results ```Name: pg-master-0.pg-master-headless.postgres.svc.cluster.local Address: 10.1.45.45``` – Jayson Gonzaga Aug 04 '20 at 13:29
  • based on my initial lookup i'm getting ```10.152.183.10#53``` which is not the actual ip but i think the server ip.. i need to get ```10.1.45.47``` which is the second address from my above command – Jayson Gonzaga Aug 04 '20 at 15:15
  • hmm so you still have a problem or not? Did you try the awk script from the other answer? – Rico Aug 04 '20 at 16:22
  • yes i did.. so kind of ended up something like this ```nslookup pg-master-0.pg-master-headless | awk 'FNR == 5 {print $2}'``` Not sure though if the ```Address``` i'm looking for is always on the 5th line. But this would suffice as of the moment. – Jayson Gonzaga Aug 04 '20 at 16:31