Friday 28 December 2012

bash code snippet to ping a public IP range in Linux

I was having an issue where traffic was not making it to the public interface of an ASA firewall that I was working on. I created this simple script to ping the public IP range. I was listening on the ASA side to see if the traffic made it through. This helped me track down that the issue was somewhere in the service providers equipment. Clearing the arp table on their equipment resolved the issue temporarily but it would re-appear. I believe we hit a bug in the equipment. The service providers equipment was replaced and the issue was resolved. I'm sure there is a smarter,cleaner or cooler way of doing this but I was in a hurry :)

First I made a file called "ping_list" and entered the IP addresses in the public range

xxx.xxx.xxx.69
xxx.xxx.xxx.70
xxx.xxx.xxx.71
xxx.xxx.xxx.72
xxx.xxx.xxx.73
xxx.xxx.xxx.74
xxx.xxx.xxx.75
xxx.xxx.xxx.76
xxx.xxx.xxx.77
xxx.xxx.xxx.78

Then I made a second file "ping-ip-range.sh" and entered the following code which just pings each IP only once then moves onto the next one.

for i in `cat ping_list`; do ping -c1 $i; done
Now I could ping the range by just running

./ping-ip-range.sh

You may need to chmod the .sh file to make it executable

chmod u+x ping-ip-range.sh

Thursday 27 December 2012

How to get your public external IP from the CLI in Linux

Linux Public IP

If you are trying to find out what your public IP address is, you typically go to a web site specifically for this purpose, such as whatsmyip.org. Sometimes you don't have a GUI web browser installed and have to use the text based web browser lynx.

Thanks to cURL, you can easily do this from the command line too. To install cURL, run the following command:

Ubuntu: sudo apt-get install curl
openSUSE: sudo zypper install curl

To find out what public IP you are using, run this command:
curl ifconfig.me
A single line will be returned containing your public external IP address.

You can get the same results
You can get the same results using wget:
wget -qO- ifconfig.me/ip
wget -qO- ipecho.net/plain

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