I have a script that updates some remote systems with my laptop's IP and CIDR on the WAN, how the Internet sees me, in other words.

I use the following to get my IP on the WAN in bash:

dig +short myip.opendns.com @resolver1.opendns.com

How can I get the CIDR for my IP on the WAN in bash as well?


In general the short answer is this is not practically feasible.

In the very specific case you have access to your firewall your firewall has a public IP address on its outside interface and you can access a shall prompt on the firewall. You can do something like this:

ssh root@ 'eval $(ipcalc -np `ifconfig eth1 | sed -n "s/inet addr:\([^ ]*\).*Mask:\([^ ]*\).*/\1 \2/p"`) ; echo $NETWORK/$PREFIX'

This command is specific to Redhat6.

Remember this information is available from the firewall because a network administrator entered the information provided him by the ISP to configure the outside interface. Therefore the only reliable way to get this information is to ask your ISP. Even if you can deduce the CIDR for your ISP from DNS and whois records, you can't deduce how the ISP has subdivided the available IPs internally. This is an administrative function and must be solved administratively.

  • also this one can be used: ip route | grep src | grep eth0 | awk '{print $1}' – Psychozoic Sep 23 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.