Search code examples
amazon-web-servicescommand-linegrepamazon-eksnslookup

Getting EKS Service IP Address from DNS using nslookup using commandline and grep


I am having a question on how to retrieve the IP address of an EKS Service using the commandline using grep.

This is the nslookup command I used, and well it shows me the key Address: and the IPs but I only need one specific IP in that index and nothing else:

nslookup internal-a0cn71b0675e49.eu-central-1.elb.amazonaws.com | grep Address

This gives me something like this:

Address: 10.178.0.8#53
Address: 10.107.206.89
Address: 10.107.205.124

I just need the middle IP part without the Address in front, how can I get one IP using grep on the commandline?

For example how can I get only:

10.107.206.89

using nslookup and grep in a linux commandline


Solution

  • central-1.elb.amazonaws.com | awk '/Address/ { addr[cnt++]=$2 } END { print addr[2] }'
    

    Search the output from nslookup for lines with "Address" using awk, create an array addr with the index incrementing and the value equal to the ip address. At the EnD, given we are only interested in the second address, print the second index of the addr array.