如果地址更改,如何将设备的IP地址合并到Python脚本中 [英] How to incorporate the IP address of a device into a Python script if the address changes

查看:105
本文介绍了如果地址更改,如何将设备的IP地址合并到Python脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,可以从智能插头中检索测量的数据,以便可以在Rasbperry Pi上对其进行可视化.

I have a Python script which retrieves the measured data from a smart plug so that I can visualize it on my Rasbperry Pi.

此命令获取数据

send_hs_command("192.168.1.26", 9999, b'{"emeter":{"get_realtime":{}}}')

这就是定义

def send_hs_command(address, port, cmd):
    data = b""

    tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        tcp_sock.connect((address, port))
        tcp_sock.send(encrypt(cmd))
        data = tcp_sock.recv(2048)
    except socket.error:        
        print(time.asctime( time.localtime(time.time()) ), "Socket closed.", file=sys.stderr)
    finally:
        tcp_sock.close()
    return data

我的问题是,如果我将智能插头放在其他地方,它将具有 一个新的IP地址,这意味着我必须继续在我的Python脚本上重写它.这不是我的选择.最简单的解决方案是什么?谢谢

My problem is that if I take the Smart Plug somewhere else, it will have a new IP-Address, which means I have to keep rewriting it on my Python script. This is not an option for me. What would be the simplest solution? Thanks

推荐答案

我没有可以在上面运行的Pi.

I don't have a Pi to run this on.

如果目标(智能插头)的IP地址是可变的,您不能使用预定的主机名(位于'/etc/hostname'中)吗?

If the IP address of the target(Smart Plug) is variable, can you not use a pre-determined host-name(located in '/etc/hostname') instead?

socket库提供了一些方便的功能;

the socket library provides a few handy functions;

您可以先使用 gethostbyaddr 来获取主机名已经有主机名信息. 从那时起,您可以使用已知的主机名并使用 create_connection 来建立连接.

You can first use gethostbyaddr to get the host-name if you don't have the host-name information already. Then from that point onward you can use the known host-name and use create_connection to establish connections.

但是,如果您想使用更具动态性的内容,请执行以下操作:我建议使用MAC地址作为密钥. 请注意,在Raspberry Pi上运行可能依赖于tcpdumpscapy可能会占用大量CPU资源. 请查看以下代码段:

However, if you want to use something more dynamic; I'd suggest using the MAC address as the key. Please be advised that running scapy which perhaps depends on tcpdump on Raspberry Pi might be CPU exhaustive. Please take a look at the following snippet:

import socket
import time
import sys
from scapy.all import *


def send_hs_command(address, port, cmd):
    data = b""
    tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        tcp_sock.connect((address, port))
        tcp_sock.send(encrypt(cmd))
        data = tcp_sock.recv(2048)
    except socket.error:
        print(time.asctime( time.localtime(time.time()) ), "Socket closed.", file=sys.stderr)
    finally:
        tcp_sock.close()
    print(data)
    return data


def get_ip_from_mac():
    # Match ARP requests
    packet_list = sniff(filter="arp", count=10) # increase number of arp counts
    for i in packet_list:
        # Show all ARP requests
        # print(i[Ether].src, "is broadcasting IP", i[ARP].psrc)
        if (i[ARP].hwsrc == '00:0c:29:b6:f4:be'): # target MAC address
            return (True, i[ARP].psrc)
    return (False, '')


def main():
    result = get_ip_from_mac()
    if result[0] == True:
        print("Succeeded to reach server")
        send_hs_command(result[1], 22, b'{"emeter":{"get_realtime":{}}}')
    else:
        # logic to retry or graciously fail
        print("Failed to reach server")


if __name__== "__main__":
    main()

这篇关于如果地址更改,如何将设备的IP地址合并到Python脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆