Mac Based presence script

Python and python framework

Moderator: leecollings

Post Reply
goshi0
Posts: 6
Joined: Sunday 31 December 2017 17:43
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Mac Based presence script

Post by goshi0 »

This is a variation of a few scripts wich mostly are depenents of the ip address this one search your device with your mac and make a ping to double check the presence in the net

you will need to install arp-scan from repositories

Code: Select all

sudo apt-get install arp-scan
tested from 2 weeks in a raspberry but must work in every linux distro

Code: Select all

#!/usr/bin/python

import http.client
import requests
from subprocess import check_output
#################config ######################

mac = "Your Mac address"
idx = domoticz idx

###############################################
# ARP checking
#

out = check_output(["sudo","/usr/bin/arp-scan", "-l"]).decode('utf-8')

#
#look for mac
#

if mac in out:
#
#look for mac's ip
#
  for elemento in out.split("\n"):
  #mac ruben 'ac:c1:ee:2e:92:e6'
     if mac in elemento:
       linea = elemento.strip()
       ip = linea[linea.find("192"):linea.find(" ")]
       
 
 
  ping = check_output(["/bin/ping","-c 2" ,ip]).decode('utf-8')
  #
  #2 pings to the ip f 
  #
  if '2 packets transmitted, 2 received' in ping:
   #
   # if the ip answers to ping turn the switch on 
   
   r = requests.get('http://192.168.1.101:8080/json.htm?type=command&param=switchlight&idx=60&switchcmd=On&username=Y29tZWRvcg==&password=ZmFuaHVudGVy')
  
  else:
  #
  #  If not turn the switch off
  #
    r = requests.get('http://192.168.1.101:8080/json.htm?type=command&param=switchlight&idx=60&switchcmd=Off&username=Y29tZWRvcg==&password=ZmFuaHVudGVy')
   
else:
   #
   #The mac isnt in the arp tables turn switch off
   #
   r = requests.get('http://192.168.1.101:8080/json.htm?type=command&param=switchlight&idx=60&switchcmd=Off&username=Y29tZWRvcg==&password=ZmFuaHVudGVy')
   print ('nadie en casa')
   print (r.text)

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests