Python dhcp 客户端 [英] Python dhcp client

查看:44
本文介绍了Python dhcp 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在多宿主机器(多个接口)上直接(无库)从 python 发送 DHCP 请求.我已经看过 pydhcplib,但仍然不明白.

I'm trying to learn how to directly (no libraries) send DHCP request from python on multi-homed machine (multiple interfaces). I've looked at pydhcplib, but still do not get it.

此代码在特定接口上发送 DHCP 数据包(在我的情况下为 eth3 - 未分配 IP),但它使用 eth0 IP 地址发送.如何将我的 src IP 更改为 0.0.0.0?本例中 dhcp-message 被截断

This code send DHCP packet on specific interface (eth3 in my case - no IP assigned), but it sends with eth0 IP address. How to change my src IP to 0.0.0.0? dhcp-message is truncated in this example

LOCAL_PORT=68
SERVER_PORT=67
LOCAL_IP="0.0.0.0"
BCAST_IP="255.255.255.255"
LISTEN_DEV="eth3"
MSG_SIZE=2048
Conn=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Conn.settimeout(5)
Conn.setsockopt(socket.SOL_SOCKET,IN.SO_BINDTODEVICE,LISTEN_DEV+'\0')
Conn.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)
Conn.bind((LOCAL_IP, LOCAL_PORT))
# Create DHCP-Discovery
msg="010106003f7d1664......"
Conn.sendto(msg.decode("hex"),(BCAST_IP,SERVER_PORT))
received = Conn.recv(MSG_SIZE)
Conn.close()

推荐答案

我假设您已经了解 高级进程间通信教程.

剧透警报:如果您想直接跳到底线,请查看 DHCP 查询配方.

Spoiler Alert: If you want to jump straight to the bottom line, have a look at the DHCP Query recipe.

特殊值 INADDR_ANY(0.0.0.0,或 Python 套接字中的空字符串 '')不是 IP 地址.

The special value INADDR_ANY (0.0.0.0, or the empty string '' in a python socket) is not an IP address.

"当一个地址被指定为 INADDR_ANY(一个清单常量定义在 <netinet/in.h >),系统将地址解释为'any有效地址'."

"When an address is specified as INADDR_ANY (a manifest constant defined in < netinet/in.h >), the system interprets the address as 'any valid address'."

来自 RFC 2131:

From RFC 2131:

如果客户端使用 DHCP 进行初始配置(之前客户端的 TCP/IP 软件已经完全配置),DHCP 需要创造性地使用客户端的 TCP/IPRFC 1122 的软件和自由解释. TCP/IP软件应该接受并转发到 IP 层任何 IP 数据包在 IP 地址被发送到客户端的硬件地址之前已配置;DHCP 服务器和 BOOTP 中继代理可能无法将 DHCP 消息传递给不能接受硬件的客户端配置 TCP/IP 软件之前的单播数据报.

In the case of a client using DHCP for initial configuration (before the client's TCP/IP software has been completely configured), DHCP requires creative use of the client's TCP/IP software and liberal interpretation of RFC 1122. The TCP/IP software SHOULD accept and forward to the IP layer any IP packets delivered to the client's hardware address before the IP address is configured; DHCP servers and BOOTP relay agents may not be able to deliver DHCP messages to clients that cannot accept hardware unicast datagrams before the TCP/IP software is configured.

据推测,您正在一个以太网接口已经配置并具有有效 IP 地址的系统上运行此程序.我不知道为什么您希望源 IP 地址为 0.0.0.0,但也许您可以使用 ifconfig 将接口 IP 设置为 0.0.0.0 以获得您想要的效果.

Presumably you're running this program on a system where the ethernet interfaces have already been configured and have valid IP addresses. I'm not sure why you'd want the source IP address to be 0.0.0.0, but perhaps you could set the interface IP to 0.0.0.0 with ifconfig to get the effect you want.

或者您可以使用 RAW 套接字并自己构建 IP 和 UDP 标头以包含任何内容.

Or you could use a RAW socket and build the IP and UDP headers yourself to contain anything.

这篇关于Python dhcp 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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