Solved

bash script - how many hops to the internet?

Posted on 2014-09-04
6
429 Views
Last Modified: 2014-09-08
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.
0
Comment
Question by:projects
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Garry-G
ID: 40305321
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?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40305539
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

0
 

Author Comment

by:projects
ID: 40306047
@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'
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 37

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40306419
(Added code block in question)

If you change the function to set a variable:

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

and then add it like this:

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))
        if $(echo ${IP} | grep -qE '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)')
        then IPtype="Private IP"
        else IPtype="Public IP"
        fi;
        DATA="$DATA$COUNT=$IP+$IPtype+$HOPTIME&"
        done
        $CURL -F function=add_hop -F data="${DATA:: -1}"
}

Open in new window


Don't know what your CURL command does extactly, can you test the above?
0
 

Author Comment

by:projects
ID: 40310244
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.
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40310342
I don't know curl, but you have the IPtype now.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question