Guest User

geolocation.py

a guest
Jun 19th, 2012
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Julien Deudon (initbrain) - 20/03/2012 15h35
  4.  
  5. from commands import getoutput
  6. import sys, re, simplejson, urllib2, webbrowser
  7. #import os
  8.  
  9. if len(sys.argv)<2:
  10.     print "\nUsage :\n\tpython "+sys.argv[0]+" <interface wifi>\n"
  11. #elif os.geteuid()!=0:
  12. #   print "Erreur : nécessite d'être lancé en tant qu'administrateur !"
  13. elif not getoutput("which iw"):
  14.     print "Un outils est nécessaire, veuillez l'installer :\niw - tool for configuring Linux wireless devices"
  15.     if "ubuntu" in getoutput("uname -a").lower(): print "sudo apt-get install iw"
  16. else:
  17.     print "[+] Scan des réseaux WIFI à proximité ..."
  18.  
  19.     iwOut=getoutput("sudo iw dev "+sys.argv[1]+" scan")
  20.  
  21.     if "failed" in iwOut:
  22.         print "[!] Le scan des réseaux wifi à échoué !"
  23.     else:
  24.         result=re.compile("BSS ([\w\d\:]+).*\n.*\n.*\n.*\n.*\n\tsignal: ([-\d]+)", re.MULTILINE).findall(iwOut)
  25.  
  26.         print "[+] Génération de la requête"
  27.  
  28.         loc_req={ "version":"1.1.0",
  29.                 "request_address":False,
  30.                 #"addresults_language":"fr",
  31.                 "wifi_towers":[{"mac_address":x[0].replace(":","-"),"signal_strength":int(x[1])} for x in result]
  32.             }
  33.  
  34.         print '\n'.join([l.rstrip() for l in simplejson.dumps(loc_req, sort_keys=True, indent=4*' ').splitlines()])
  35.  
  36.         print "[+] Envoi de la requête à Google"
  37.  
  38.         data = simplejson.JSONEncoder().encode(loc_req)
  39.         output = simplejson.loads(urllib2.urlopen('https://www.google.com/loc/json', data).read())
  40.  
  41.         print "[+] Résultat"
  42.         print '\n'.join([l.rstrip() for l in simplejson.dumps(output, sort_keys=True, indent=4*' ').splitlines()])
  43.  
  44.         print "[+] Lien Google Map"
  45.         print "http://maps.google.fr/maps?q="+str(output["location"]["latitude"])+","+str(output["location"]["longitude"])
  46.  
  47.         print "[+] Génération d'un aperçu"
  48.         html="""<!DOCTYPE html>
  49.         <html>
  50.             <head>
  51.                 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  52.                 <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  53.  
  54.                 <style type="text/css">
  55.                     html, body {
  56.                         height: 100%;
  57.                         margin: 0;
  58.                         padding: 0;
  59.                     }
  60.  
  61.                     #map_canvas {
  62.                         height: 100%;
  63.                     }
  64.  
  65.                     @media print {
  66.                         html, body {
  67.                             height: auto;
  68.                         }
  69.  
  70.                         #map_canvas {
  71.                             height: 650px;
  72.                         }
  73.                     }
  74.                 </style>
  75.  
  76.                 <title>Geolocation</title>
  77.                 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  78.                 <script type="text/javascript">
  79.                     function initialize() {
  80.                         var mapOptions = {
  81.                             zoom: 17,
  82.                             center: new google.maps.LatLng("""+str(output["location"]["latitude"])+", "+str(output["location"]["longitude"])+"""),
  83.                             //mapTypeId: google.maps.MapTypeId.ROADMAP
  84.                             mapTypeId: google.maps.MapTypeId.HYBRID
  85.  
  86.                         };
  87.  
  88.                         var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  89.  
  90.                         // Construct the accuracy circle.
  91.                         var accuracyOptions = {
  92.                             strokeColor: "#000000",
  93.                             strokeOpacity: 0.8,
  94.                             strokeWeight: 2,
  95.                             fillColor: "#000000",
  96.                             fillOpacity: 0.35,
  97.                             map: map,
  98.                             center: mapOptions.center,
  99.                             radius: """+str(output["location"]["accuracy"])+"""
  100.                         };
  101.                         var accuracyCircle = new google.maps.Circle(accuracyOptions);
  102.  
  103.                         var contentString = '<div>'+
  104.                             '<p><b>YOU ARE HERE !</b><br>'+
  105.                             'Latitude : """+str(output["location"]["latitude"])+"""<br>'+
  106.                             'Longitude : """+str(output["location"]["longitude"])+"""<br>'+
  107.                             'Accuracy : """+str(output["location"]["accuracy"])+"""</p>'+
  108.                             '</div>';
  109.  
  110.                         var infoWindow = new google.maps.InfoWindow({
  111.                             content: contentString
  112.                         });
  113.  
  114.                         var marker = new google.maps.Marker({
  115.                             position: mapOptions.center,
  116.                             map: map
  117.                         });
  118.  
  119.                         google.maps.event.addListener(accuracyCircle, 'click', function() {
  120.                             infoWindow.open(map,marker);
  121.                         });
  122.  
  123.                         google.maps.event.addListener(marker, 'click', function() {
  124.                             infoWindow.open(map,marker);
  125.                         });
  126.                     }
  127.                 </script>
  128.             </head>
  129.             <body onload="initialize()">
  130.                 <div id="map_canvas"></div>
  131.             </body>
  132.         </html>"""
  133.  
  134.         output = open("geolocation.html",'wb')
  135.         output.write(html)
  136.         output.close()
  137.  
  138.         print "[+] Ouverture dans votre navigateur"
  139.         webbrowser.open("geolocation.html")
Advertisement
Add Comment
Please, Sign In to add comment