Solved

bash script - how many hops to the internet?

Posted on 2014-09-04
6
418 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
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
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.

 
LVL 37

Accepted Solution

by:
Gerwin Jansen 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
ID: 40310342
I don't know curl, but you have the IPtype now.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Don’t let your business fall victim to the coming apocalypse – use our Survival Guide for the Fax Apocalypse to identify the risks and signs of zombie fax activities at your business.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now