PROJET AUTOBLOG


Korben

source: Korben

⇐ retour index

Être alerté quand vos parents ou votre copine / copain arrive à la maison

mercredi 2 juillet 2014 à 09:30

Si vous souhaitez savoir quelques secondes en avance si votre mec, votre nana, vos parents ou vos colocataires débarquent chez vous à l'improviste, j'ai ce qu'il vous faut !

Il s'agit d'un petit bout de code en python qui scanne votre réseau WiFi à la recherche du nombre de machines qui y sont connectées. Vous le lancez quand vous êtes seul et lorsque ce nombre change, cela signifie qu'un téléphone ou une tablette ou un ordi s'est connecté au réseau.

Concrètement, si les personnes avec qui vous vivez ou vous travaillez ont un téléphone qui switche directement de la 4G au WiFi lorsqu'ils sont à proximité de chez vous, vous serez alerté par un message et des biiiiip biiiip biiiiiip.

Installez nmap puis lancez le script Python suivant (attention aux dépendances nmap) :

#!/usr/bin/env python
 
"""
Author : pescimoro.mattia@gmail.com
Licence : GPL v3 or any later version
 
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
 
import sys
import os
import nmap # import nmap.py
import time
 
try:
    nm = nmap.PortScanner() # creates an'instance of nmap.PortScanner
except nmap.PortScannerError:
    print('Nmap not found', sys.exc_info()[0])
    sys.exit(0)
except:
    print("Unexpected error:", sys.exc_info()[0])
    sys.exit(0)
 
def seek(): # defines a function to analize the network
    count = 0
    nm.scan(hosts='192.168.1.0/24', arguments='-n -sP -T4')
    # runs a quick ping sweep
 
    hosts_list = [(x) for x in nm.all_hosts()]
    # saves the host list
 
    localtime = time.asctime(time.localtime(time.time()))
    print('Local current time :', localtime)
    # print out system time
 
    for host in hosts_list: # count and print active IPs
        count = count + 1
        print('IP: {0}'.format(host))
    print('-----------------')
    return count # returns the number of addresses
 
def beep(): # avoids OS dependency with a system beep
    print('\a')
 
if __name__ == '__main__':
    count = new_count = seek()
 
    # check if the number of addresses is still the same
    while (new_count <= count):
        new_count = seek()
 
    # DANGER!!!
    print('OHSHITOHSHITOHSHITOHSHITOHSHIT!')
    beep()

Ce script et ses dépendances sont téléchargeables ici.

Sommaire mais pratique !

Source

Cet article merveilleux et sans aucun égal intitulé : Être alerté quand vos parents ou votre copine / copain arrive à la maison ; a été publié sur Korben, le seul site qui t'aime plus fort que tes parents.