Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

bash script - how many hops to the internet?

Posted on 2014-09-04
6
Medium Priority
?
502 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 18

Expert Comment

by:Garry Glendown
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 38

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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 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 38

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

916 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