How to find the public IP of your linux node without using an external DNS server
Without much ado:
#!/bin/bash
#Get the public IP for the network in use
#This IP will be accessible through this gateway if the gateway is open to the Internet accessible from the Internet as well
#Get the gateway address
gatewayIP=`ip route | grep default | awk '{print $3}'`
#Find the public IP for the network
ip route get $gatewayIP | awk '{print $5}'
#If you want to know the public IP open to the Internet you can do
#ip route get 10.0.136.116 | awk '{print $5}' #10.0.136.116 is an IBM nameserver
The idea is to use the Gateway of your network to find the IP accessible from your network. i.e public to your network. If the Gateway is open to the Internet that will be the public IP in general as well.
Regards
Ragu
#!/bin/bash
#Get the public IP for the network in use
#This IP will be accessible through this gateway if the gateway is open to the Internet accessible from the Internet as well
#Get the gateway address
gatewayIP=`ip route | grep default | awk '{print $3}'`
#Find the public IP for the network
ip route get $gatewayIP | awk '{print $5}'
#If you want to know the public IP open to the Internet you can do
#ip route get 10.0.136.116 | awk '{print $5}' #10.0.136.116 is an IBM nameserver
The idea is to use the Gateway of your network to find the IP accessible from your network. i.e public to your network. If the Gateway is open to the Internet that will be the public IP in general as well.
Regards
Ragu
Comments