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;
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.
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}"
}
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.
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;
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;
I want to add a row called 'type' which shows 'public' or 'private'
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
I want to add a row called 'type' which shows 'public' or 'private'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Maybe you could elaborate on what you hope to gain by the hop-count information?