Solved

bash script - how many hops to the internet?

Posted on 2014-09-04
6
406 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
(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
Comment Utility
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
Comment Utility
I don't know curl, but you have the IPtype now.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

9 Experts available now in Live!

Get 1:1 Help Now