IP logger when a specific ip connects to the wifi

Viewed 532

I watched a video of someone coding a python script which detects if a specific ip joins the network. If the ip connects a message will be sent.

import subprocess
import os
from decouple import config

IP_NETWORK = config('My Network IP')
IP_DEVICE = config('The target device IP')

proc = subprocess.Popen(["ping", IP_NETWORK], stdout=subprocess.PIPE)
while True:
    line = proc.stdout.readline()
    if not line:
        break

# the real code does filtering here
connected_ip = line.decode('utf-8').split()[3]

if connected_ip == IP_DEVICE:
    subprocess.Popen(["say", "Person just connected to the network"])

I get this error though:

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/decouple.py", line 199, in __call__
    return self.config(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/decouple.py", line 83, in __call__
    return self.get(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/decouple.py", line 68, in get
    raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option))
decouple.UndefinedValueError: My Network IP not found. Declare it as envvar or define a default value.

What did I do wrong or can this even work? The video where I found the script was from Kalle Hallden. Thanks for your time!

3 Answers

i have tried another way using the pyttsx3 module and it worked fine,

import subprocess
import pyttsx3
import time

tts = pyttsx3.init()



def sayconnect(x):
    tts.say(x + " just connected to the internet!")
    tts.runAndWait()


def saydisconnect(x):
    tts.say(x + " just disconnected from the internet!")
    tts.runAndWait()


count= 0 


my_ip= "insert you ip here"


while True: 



     pingip = subprocess.Popen(['ping' , my_ip] , stdout=subprocess.PIPE)
     time.sleep(4)
     if(pingip.poll() == 0 ):

         if(count  == 0):
            print("user connected")

     elif(pingip.poll() == 1):

         if (count == 1 ):
            print("user disconnected")

i added the count to prevent spamming the same tts over and over again

you need to actually put in the IPs, not just plain text.

IP_NETWORK = config('xxx.xx.xx.x')
IP_DEVICE = config('xxx.xx.xx.x')

i saw that video too, but he won't just leak his IP address :D

Edit: i tried it myself, but it never worked for some module issues, trying a new method right now, so if this code worked for you let me know.

if you know the ip, what you'd like to monit, you can do it like :

import subprocess

def ipmonit(ip):
    pingIP = subprocess.Popen(['ping', ip], stdout=subprocess.PIPE)
    pingIP.wait()
    if (pingIP.poll() == 0):
        print("Person just connected to the network")

ipmonit("192.168.1.1")

ping.wait() = waits the finish of the ping procces/ 4 pings stdout=subprocess.PIPE = hides the output of the procces ping.poll() can be 0 or 1, if its 0 you have response, if you have 1 its not responding. note that on windows even on Destination host unreachable will report UP!

Related