Python Code to Find Countries Your Packet Is Routing Through

Published on:
Tags:

It is a simple program in Python which makes use of the traceroute utility along with the GeoIP database to find out the names of the countries through which a packet is routing.


First of all, download the GeoIP database file from here. Extract the csv file from the zip folder and rename the file as country.csv. Renaming is necessary as the file, in the code below, is referred by this name.


Now make a file traceroute.py(you can give any name to the file, this is just an example) and copy the following code into it.

import socket
import sys
import csv

    def main(dest_name,countries):
        dest_addr = socket.gethostbyname(dest_name)
        port = 33434
        max_hops = 30
        icmp = socket.getprotobyname('icmp')
        udp = socket.getprotobyname('udp')
        ttl = 1
        f=csv.reader(open('country.csv','rU'))
        data = []
        for row in f:
            data.append(row)
        while True:
            recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
            send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
            send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
            recv_socket.bind(("", port))
            send_socket.sendto("", (dest_name, port))
            curr_addr = None
            curr_name = None
            try:
                _, curr_addr = recv_socket.recvfrom(512)
                curr_addr = curr_addr[0]
                try:
                    curr_name = socket.gethostbyaddr(curr_addr)[0]
                except socket.error:
                    curr_name = curr_addr
            except socket.error:
                pass
            finally:
                send_socket.close()
                recv_socket.close()

            if curr_addr is not None:
                curr_host = "%s (%s)" % (curr_name, curr_addr)
            else:
                curr_host = "*"
            print "%d\t%s" % (ttl, curr_host)
            a=[]
            a=coun(curr_addr,data) ###
            countries.append(a)
            ttl += 1
            if curr_addr == dest_addr or ttl > max_hops:
                break
        return countries

def coun(curr_addr,data):
    countries=[]
    add=curr_addr.split('.')
    #print add
    var=0
    #while data[var] is not None:
    for row in data:
        #print "fdgjdfgdfkg"
        uplimit=row[1]

        lwlimit=row[0]
        uplimit=uplimit.split('.')
        lwlimit=lwlimit.split('.')
        #print uplimit
        #print lwlimit


        if add[0] == uplimit[0]:
            #print "gnvkdfhgkfd"
            if add[1] == lwlimit[1] or add[1] == uplimit[1]:
                if add[2] == uplimit[2] or add[2] == lwlimit[2]:
                    if add[3] == uplimit[3] or add[3] == lwlimit[3]:
                        return row[5]
                elif add[2] < uplimit[2] and add[2] > lwlimit[2]:
                    return row[5]

            elif add[1] < uplimit[1] and add[1] > lwlimit[1]:
                return row[5]
        #else:

    return "country NA"


if __name__ == "__main__":
    countries=[]
    countries = main(sys.argv[1],countries)
    print "The packets are routing through the following countries:"
    for value in countries:
        print value

Now put both the files- traceroute.py and country.csv in the same folder and run the following command in the terminal

sudo python traceroute.py <www.domainname.com>

Provide any domainname without <> and you will see the required output

LAMP on Fedora

Published on:
Tags:

To install LAMP on fedora just follow the following steps and within few minutes you will be ready with the LAMP stack on your system.

Step.1: We must install the updates first:

sudo yum update

Step.2: Installing Apache:

sudo yum install httpd

The Apache service can be started by running

sudo service httpd start

Step.3: Installing MySQL:

sudo yum install mysql mysql-server

To Start MySQL

sudo service mysqld start

Step.4: Installing PHP

sudo yum install php php-mysql

To check whether LAMP is correctly installed and working make a file info.php inside the /var/www/html directory

sudo vi /var/www/html/info.php

and paste the following lines into it and save it

<?php
phpinfo();
?>

Now restart the Apache

sudo service httpd restart

Now in the browser enter the URL of your info.php file i.e

http://localhost/info.php

You should see the info page on the browser.

The LAMP stack has, now, been installed on your system.


If you want to configure the root password for MySQL run the following command

sudo /usr/bin/mysql_secure_installation

This prompts you to enter the current root password, just leave it blank by pressing enter as you have just installed MySQL and no root password has been set yet. Next the prompt asks you whether you want to set a root password, enter y and follow the instructions. In the next few steps it will ask you the following questions

Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]

Just enter Y for all the questions. If all done, you have successfully setup new root password and your MySQL installation is now secure.

Now restart the Apache and Mysql services

sudo service httpd restart
sudo service mysqld restart

To run the services on the startup run the following commands(PHP automatically starts when tha Apache starts):

sudo chkconfig httpd on
sudo chkconfig mysqld on