1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import socket
import requests
import struct
import os
from sets import Set
def geolocalisation(ip): # Split du site et retourne IP, latitude, longitude
header = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; hu-HU; rv:1.7.8) Gecko/20050511 Firefox/1.0.4'}
url = 'http://fr.geoipview.com/?q='+ ip
r = requests.get(url, headers=header)
gps = r.content.split("GLatLng(")[1].split("),")[0]
gps = gps.split(", ")
Latitude = round(float(gps[0]),6)
Longitude = round(float(gps[1]),6)
Localisation = r.content.split("localiseur, ")[1].split('''"''')[0]
Localisation = Localisation.split(", ")
Ip = Localisation[0]
result = [Ip, Latitude, Longitude]
return(Ip, Latitude, Longitude)
def ip_sans_doublon(buffer_ip): # multiples IP
ip = sorted(set(buffer_ip)) # IP sans doublons
return(ip)
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
buffer_ip = []
geo_ip = []
# recuperation de toutes les ip transitant sur le reseau
while True:
packet = rawSocket.recvfrom(2048)
ethernet_header = packet[0][0:14]
ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)
ethertype = ethernet_detailed[2]
#if ethertype != '\x08\x00':
# continue
src = socket.inet_ntoa(packet[0][26:30]) # ip destination
dst = socket.inet_ntoa(packet[0][30:34]) # ip source
buffer_ip.append(src) # sauvegarde vers buffer
buffer_ip.append(dst) # sauvegarde vers buffer
ip = ip_sans_doublon(buffer_ip) # liste des nouvelles adresses ip trouvees sans doublon
if len(geo_ip) < len(ip): # Si nouvelle adresse
os.system("clear") # Rafraichissement
geo_ip = ip # MAJ liste ip a geolocaliser
for i in geo_ip: # Pour chaque ip on essaye une geoloc
try: # Sinon ip classique
print geolocalisation(i)
except:
print i |
Partager