首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >特定IP连接到wifi时的ip记录器

特定IP连接到wifi时的ip记录器
EN

Stack Overflow用户
提问于 2020-07-24 18:37:35
回答 3查看 352关注 0票数 2

我看了一段视频,视频中有人编写了一个python脚本,用于检测特定的ip是否加入了网络。如果ip连接,将发送一条消息。

代码语言:javascript
复制
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"])

但是我得到了这个错误:

代码语言:javascript
复制
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.

我做错了什么,或者这能行得通吗?我找到剧本的视频来自Kalle Hallden。耽误您时间,实在对不起!

EN

回答 3

Stack Overflow用户

发布于 2020-10-27 22:09:22

我尝试了另一种使用pyttsx3模块的方法,它工作得很好。

代码语言:javascript
复制
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")

我增加了计数,以防止一次又一次地发送相同的tts

票数 1
EN

Stack Overflow用户

发布于 2020-10-27 16:01:41

您需要实际输入IP,而不仅仅是纯文本。

代码语言:javascript
复制
IP_NETWORK = config('xxx.xx.xx.x')
IP_DEVICE = config('xxx.xx.xx.x')

我也看过那段视频,但他不会泄露他的IP地址

编辑:我自己试过了,但它对一些模块问题从来没有起作用,现在正在尝试一个新的方法,所以如果这个代码对你有效,请让我知道。

票数 0
EN

Stack Overflow用户

发布于 2020-10-27 17:48:17

如果你知道ip,你想做什么,你可以这样做:

代码语言:javascript
复制
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() =等待ping进程的完成/4个ping stdout=subprocess.PIPE =隐藏进程的输出ping.poll()可以是0或1,如果是0,则有响应;如果有1,则没有响应。请注意,在windows上,即使是在目标主机上,也会报告不可达!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63071877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档