Link to home
Start Free TrialLog in
Avatar of projects
projects

asked on

bash script - how many hops to the internet?

I have a situation where I need to when I am actually reaching public IPs rather than private internet IPs. In other words, most of the hops will be private network IPs and what I want is to know when I am actually hitting a public routable IP.

My current function is as follows;

function get_hops()
{
        HOPTIME="$(date +"%F %T")"
        LIST=$( traceroute -n someserver.com | awk '{if(NR>1)print $2}')
        set -f
        for IP in $LIST; do
        COUNT=$(($COUNT + 1))
        DATA="$DATA$COUNT=$IP+$HOPTIME&"
        done
        $CURL -F function=add_hop -F data="${DATA:: -1}"
}

Open in new window


I believe I need to add two variables.

1 - a function which can identify between private and public IPs
1 - a variable which allows me to set how many hops to traceroute once I reach a public IP

I don't know enough about programming to add this myself so am asking for the actual finished code.

Thanks very much.
Avatar of Garry Glendown
Garry Glendown
Flag of Germany image

Apart from the fact that mostly not necessarily the number of hops, but the latency is important, you need to define "the Internet" ... also, not all private networks stick to RFC1918 IPs for internal use, so just checking whether an IP is RFC1918 or not isn't necessarily reliable. Add to that that features like MPLS may hide the actual hop count even on "external" areas of the backbone, and you end up with an unreliable information with little significance ...
Maybe you could elaborate on what you hope to gain by the hop-count information?
A function to determine if an IP address is public or not can be implemented with grep like this:

if $(echo ${IP} | grep -qE '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)')
then echo "Private IP"
else echo "Public IP"
fi;

Open in new window

Avatar of projects
projects

ASKER

@Gary; Because in some cases, I do know that everything internal is private IPs so when I reach a public IP, I don't want to keep testing.

The Internet: The Public or external portion of the network where private IPs cannot be routed
While public IPs can also be used internally, in most cases, they are not and even if they are, so be it. I just want to know which is which in the output.

@Gerwin; How can this be incorporated into my code above so that the output has a new row called 'type' for example.

The current output looks like this in the DB;

ip 	                        level 	traceroute_id 	datetime
192.168.1.23 	1 		3 	                        2014-09-05 03:01:49

Open in new window


I want to add a row called 'type' which shows 'public' or 'private'
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The curl command is what is sending the output to php so I need to add something on the php/mysql side as well to make this work.
I don't know curl, but you have the IPtype now.