somestuff-dhcpstuff 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- somestuff_dhcpstuff-0.1.1/.gitignore +180 -0
- somestuff_dhcpstuff-0.1.1/LICENSE.md +163 -0
- somestuff_dhcpstuff-0.1.1/PKG-INFO +109 -0
- somestuff_dhcpstuff-0.1.1/README.md +86 -0
- somestuff_dhcpstuff-0.1.1/pyproject.toml +49 -0
- somestuff_dhcpstuff-0.1.1/somestuff/dhcpstuff/README.md +86 -0
- somestuff_dhcpstuff-0.1.1/somestuff/dhcpstuff/__init__.py +39 -0
- somestuff_dhcpstuff-0.1.1/somestuff/dhcpstuff/__main__.py +5 -0
- somestuff_dhcpstuff-0.1.1/somestuff/dhcpstuff/dhcp_discover.py +229 -0
- somestuff_dhcpstuff-0.1.1/somestuff/dhcpstuff/py.typed +0 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**/.gtfs_cache
|
|
2
|
+
/*.local
|
|
3
|
+
.claude
|
|
4
|
+
|
|
5
|
+
/*gguf*
|
|
6
|
+
|
|
7
|
+
/docker-config
|
|
8
|
+
/docker_build_local.log
|
|
9
|
+
|
|
10
|
+
/ms?hit
|
|
11
|
+
*mstest*py
|
|
12
|
+
*_test.py
|
|
13
|
+
/umqtt
|
|
14
|
+
.idea/
|
|
15
|
+
/upydev_.config
|
|
16
|
+
*.local.*
|
|
17
|
+
/scripts/logdir/
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Byte-compiled / optimized / DLL files
|
|
21
|
+
__pycache__/
|
|
22
|
+
*.py[cod]
|
|
23
|
+
*$py.class
|
|
24
|
+
|
|
25
|
+
# C extensions
|
|
26
|
+
*.so
|
|
27
|
+
|
|
28
|
+
# Distribution / packaging
|
|
29
|
+
.Python
|
|
30
|
+
build/
|
|
31
|
+
develop-eggs/
|
|
32
|
+
dist/
|
|
33
|
+
downloads/
|
|
34
|
+
eggs/
|
|
35
|
+
.eggs/
|
|
36
|
+
lib/
|
|
37
|
+
lib64/
|
|
38
|
+
parts/
|
|
39
|
+
sdist/
|
|
40
|
+
var/
|
|
41
|
+
wheels/
|
|
42
|
+
share/python-wheels/
|
|
43
|
+
*.egg-info/
|
|
44
|
+
.installed.cfg
|
|
45
|
+
*.egg
|
|
46
|
+
MANIFEST
|
|
47
|
+
|
|
48
|
+
# PyInstaller
|
|
49
|
+
# Usually these files are written by a python script from a template
|
|
50
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
51
|
+
*.manifest
|
|
52
|
+
*.spec
|
|
53
|
+
|
|
54
|
+
# Installer logs
|
|
55
|
+
pip-log.txt
|
|
56
|
+
pip-delete-this-directory.txt
|
|
57
|
+
|
|
58
|
+
# Unit test / coverage reports
|
|
59
|
+
htmlcov/
|
|
60
|
+
.tox/
|
|
61
|
+
.nox/
|
|
62
|
+
.coverage
|
|
63
|
+
.coverage.*
|
|
64
|
+
.cache
|
|
65
|
+
nosetests.xml
|
|
66
|
+
coverage.xml
|
|
67
|
+
*.cover
|
|
68
|
+
*.py,cover
|
|
69
|
+
.hypothesis/
|
|
70
|
+
.pytest_cache/
|
|
71
|
+
cover/
|
|
72
|
+
|
|
73
|
+
# Translations
|
|
74
|
+
*.mo
|
|
75
|
+
*.pot
|
|
76
|
+
|
|
77
|
+
# Django stuff:
|
|
78
|
+
*.log
|
|
79
|
+
local_settings.py
|
|
80
|
+
db.sqlite3
|
|
81
|
+
db.sqlite3-journal
|
|
82
|
+
|
|
83
|
+
# Flask stuff:
|
|
84
|
+
instance/
|
|
85
|
+
.webassets-cache
|
|
86
|
+
|
|
87
|
+
# Scrapy stuff:
|
|
88
|
+
.scrapy
|
|
89
|
+
|
|
90
|
+
# Sphinx documentation
|
|
91
|
+
docs/_build/
|
|
92
|
+
|
|
93
|
+
# PyBuilder
|
|
94
|
+
.pybuilder/
|
|
95
|
+
target/
|
|
96
|
+
|
|
97
|
+
# Jupyter Notebook
|
|
98
|
+
.ipynb_checkpoints
|
|
99
|
+
|
|
100
|
+
# IPython
|
|
101
|
+
profile_default/
|
|
102
|
+
ipython_config.py
|
|
103
|
+
|
|
104
|
+
# pyenv
|
|
105
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
106
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
107
|
+
# .python-version
|
|
108
|
+
|
|
109
|
+
# pipenv
|
|
110
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
111
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
112
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
113
|
+
# install all needed dependencies.
|
|
114
|
+
#Pipfile.lock
|
|
115
|
+
|
|
116
|
+
# poetry
|
|
117
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
118
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
119
|
+
# commonly ignored for libraries.
|
|
120
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
121
|
+
#poetry.lock
|
|
122
|
+
|
|
123
|
+
# pdm
|
|
124
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
125
|
+
#pdm.lock
|
|
126
|
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
|
127
|
+
# in version control.
|
|
128
|
+
# https://pdm.fming.dev/#use-with-ide
|
|
129
|
+
.pdm.toml
|
|
130
|
+
|
|
131
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
132
|
+
__pypackages__/
|
|
133
|
+
|
|
134
|
+
# Celery stuff
|
|
135
|
+
celerybeat-schedule
|
|
136
|
+
celerybeat.pid
|
|
137
|
+
|
|
138
|
+
# SageMath parsed files
|
|
139
|
+
*.sage.py
|
|
140
|
+
|
|
141
|
+
# Environments
|
|
142
|
+
.env
|
|
143
|
+
.venv
|
|
144
|
+
env/
|
|
145
|
+
venv/
|
|
146
|
+
ENV/
|
|
147
|
+
env.bak/
|
|
148
|
+
venv.bak/
|
|
149
|
+
|
|
150
|
+
# Spyder project settings
|
|
151
|
+
.spyderproject
|
|
152
|
+
.spyproject
|
|
153
|
+
|
|
154
|
+
# Rope project settings
|
|
155
|
+
.ropeproject
|
|
156
|
+
|
|
157
|
+
# mkdocs documentation
|
|
158
|
+
/site
|
|
159
|
+
|
|
160
|
+
# mypy
|
|
161
|
+
.mypy_cache/
|
|
162
|
+
.dmypy.json
|
|
163
|
+
dmypy.json
|
|
164
|
+
|
|
165
|
+
# Pyre type checker
|
|
166
|
+
.pyre/
|
|
167
|
+
|
|
168
|
+
# pytype static type analyzer
|
|
169
|
+
.pytype/
|
|
170
|
+
|
|
171
|
+
# Cython debug symbols
|
|
172
|
+
cython_debug/
|
|
173
|
+
|
|
174
|
+
# PyCharm
|
|
175
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
176
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
177
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
178
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
179
|
+
#.idea/
|
|
180
|
+
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
GNU Lesser General Public License
|
|
2
|
+
=================================
|
|
3
|
+
|
|
4
|
+
_Version 3, 29 June 2007_
|
|
5
|
+
_Copyright © 2007 Free Software Foundation, Inc. <<http://fsf.org/>>_
|
|
6
|
+
|
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
8
|
+
of this license document, but changing it is not allowed.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates
|
|
12
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
13
|
+
License, supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
### 0. Additional Definitions
|
|
16
|
+
|
|
17
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the “GNU GPL” refers to version 3 of the GNU
|
|
19
|
+
General Public License.
|
|
20
|
+
|
|
21
|
+
“The Library” refers to a covered work governed by this License,
|
|
22
|
+
other than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An “Application” is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A “Combined Work” is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the “Linked
|
|
32
|
+
Version”.
|
|
33
|
+
|
|
34
|
+
The “Minimal Corresponding Source” for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The “Corresponding Application Code” for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
### 1. Exception to Section 3 of the GNU GPL
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
### 2. Conveying Modified Versions
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
* **a)** under this License, provided that you make a good faith effort to
|
|
58
|
+
ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
|
|
62
|
+
* **b)** under the GNU GPL, with none of the additional permissions of
|
|
63
|
+
this License applicable to that copy.
|
|
64
|
+
|
|
65
|
+
### 3. Object Code Incorporating Material from Library Header Files
|
|
66
|
+
|
|
67
|
+
The object code form of an Application may incorporate material from
|
|
68
|
+
a header file that is part of the Library. You may convey such object
|
|
69
|
+
code under terms of your choice, provided that, if the incorporated
|
|
70
|
+
material is not limited to numerical parameters, data structure
|
|
71
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
72
|
+
(ten or fewer lines in length), you do both of the following:
|
|
73
|
+
|
|
74
|
+
* **a)** Give prominent notice with each copy of the object code that the
|
|
75
|
+
Library is used in it and that the Library and its use are
|
|
76
|
+
covered by this License.
|
|
77
|
+
* **b)** Accompany the object code with a copy of the GNU GPL and this license
|
|
78
|
+
document.
|
|
79
|
+
|
|
80
|
+
### 4. Combined Works
|
|
81
|
+
|
|
82
|
+
You may convey a Combined Work under terms of your choice that,
|
|
83
|
+
taken together, effectively do not restrict modification of the
|
|
84
|
+
portions of the Library contained in the Combined Work and reverse
|
|
85
|
+
engineering for debugging such modifications, if you also do each of
|
|
86
|
+
the following:
|
|
87
|
+
|
|
88
|
+
* **a)** Give prominent notice with each copy of the Combined Work that
|
|
89
|
+
the Library is used in it and that the Library and its use are
|
|
90
|
+
covered by this License.
|
|
91
|
+
|
|
92
|
+
* **b)** Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
93
|
+
document.
|
|
94
|
+
|
|
95
|
+
* **c)** For a Combined Work that displays copyright notices during
|
|
96
|
+
execution, include the copyright notice for the Library among
|
|
97
|
+
these notices, as well as a reference directing the user to the
|
|
98
|
+
copies of the GNU GPL and this license document.
|
|
99
|
+
|
|
100
|
+
* **d)** Do one of the following:
|
|
101
|
+
- **0)** Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
- **1)** Use a suitable shared library mechanism for linking with the
|
|
109
|
+
Library. A suitable mechanism is one that **(a)** uses at run time
|
|
110
|
+
a copy of the Library already present on the user's computer
|
|
111
|
+
system, and **(b)** will operate properly with a modified version
|
|
112
|
+
of the Library that is interface-compatible with the Linked
|
|
113
|
+
Version.
|
|
114
|
+
|
|
115
|
+
* **e)** Provide Installation Information, but only if you would otherwise
|
|
116
|
+
be required to provide such information under section 6 of the
|
|
117
|
+
GNU GPL, and only to the extent that such information is
|
|
118
|
+
necessary to install and execute a modified version of the
|
|
119
|
+
Combined Work produced by recombining or relinking the
|
|
120
|
+
Application with a modified version of the Linked Version. (If
|
|
121
|
+
you use option **4d0**, the Installation Information must accompany
|
|
122
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
123
|
+
Code. If you use option **4d1**, you must provide the Installation
|
|
124
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
125
|
+
for conveying Corresponding Source.)
|
|
126
|
+
|
|
127
|
+
### 5. Combined Libraries
|
|
128
|
+
|
|
129
|
+
You may place library facilities that are a work based on the
|
|
130
|
+
Library side by side in a single library together with other library
|
|
131
|
+
facilities that are not Applications and are not covered by this
|
|
132
|
+
License, and convey such a combined library under terms of your
|
|
133
|
+
choice, if you do both of the following:
|
|
134
|
+
|
|
135
|
+
* **a)** Accompany the combined library with a copy of the same work based
|
|
136
|
+
on the Library, uncombined with any other library facilities,
|
|
137
|
+
conveyed under the terms of this License.
|
|
138
|
+
* **b)** Give prominent notice with the combined library that part of it
|
|
139
|
+
is a work based on the Library, and explaining where to find the
|
|
140
|
+
accompanying uncombined form of the same work.
|
|
141
|
+
|
|
142
|
+
### 6. Revised Versions of the GNU Lesser General Public License
|
|
143
|
+
|
|
144
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
145
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
146
|
+
versions will be similar in spirit to the present version, but may
|
|
147
|
+
differ in detail to address new problems or concerns.
|
|
148
|
+
|
|
149
|
+
Each version is given a distinguishing version number. If the
|
|
150
|
+
Library as you received it specifies that a certain numbered version
|
|
151
|
+
of the GNU Lesser General Public License “or any later version”
|
|
152
|
+
applies to it, you have the option of following the terms and
|
|
153
|
+
conditions either of that published version or of any later version
|
|
154
|
+
published by the Free Software Foundation. If the Library as you
|
|
155
|
+
received it does not specify a version number of the GNU Lesser
|
|
156
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
157
|
+
General Public License ever published by the Free Software Foundation.
|
|
158
|
+
|
|
159
|
+
If the Library as you received it specifies that a proxy can decide
|
|
160
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
161
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
162
|
+
permanent authorization for you to choose that version for the
|
|
163
|
+
Library.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: somestuff-dhcpstuff
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: DHCP Discover sender and response analyzer, including Proxy DHCP / PXE boot server responses (stdlib only)
|
|
5
|
+
Project-URL: Homepage, https://github.com/vroomfondel/somestuff
|
|
6
|
+
Project-URL: Repository, https://github.com/vroomfondel/somestuff
|
|
7
|
+
Project-URL: Issues, https://github.com/vroomfondel/somestuff/issues
|
|
8
|
+
Author-email: Henning Thieß <ht@xomox.cc>
|
|
9
|
+
License-File: LICENSE.md
|
|
10
|
+
Keywords: dhcp,discover,network-diagnostics,proxy-dhcp,pxe
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: System Administrators
|
|
13
|
+
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
|
|
14
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: System :: Boot
|
|
19
|
+
Classifier: Topic :: System :: Networking
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.13
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# dhcpstuff
|
|
25
|
+
|
|
26
|
+
DHCP Discover sender and response analyzer for network diagnostics: broadcasts a
|
|
27
|
+
DHCPDISCOVER and prints every answer, including regular DHCP offers and Proxy DHCP
|
|
28
|
+
responses from PXE boot servers. Standard library only.
|
|
29
|
+
|
|
30
|
+
## Modules
|
|
31
|
+
|
|
32
|
+
- **`dhcp_discover.py`** — builds a DHCPDISCOVER packet, broadcasts it to
|
|
33
|
+
`255.255.255.255:67` from UDP port 68 and collects replies for the given timeout.
|
|
34
|
+
Only replies matching the transaction ID are shown. Each response is labelled
|
|
35
|
+
`DHCP <type>` or, if the offered address (`yiaddr`) is `0.0.0.0`, `Proxy DHCP <type>`,
|
|
36
|
+
followed by offered IP, next server, server name, boot file and all parsed options
|
|
37
|
+
with human-readable names. With `--arch` the Discover carries PXE client
|
|
38
|
+
architecture (option 93) and a matching `PXEClient` vendor class (option 60),
|
|
39
|
+
which Proxy DHCP / PXE boot servers require before they answer.
|
|
40
|
+
- **`__main__.py`** — makes `python3 -m somestuff.dhcpstuff` run the same CLI.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install somestuff-dhcpstuff
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`somestuff` is a PEP 420 namespace; the package is imported as
|
|
49
|
+
`somestuff.dhcpstuff` and shares the namespace with the other `somestuff-*`
|
|
50
|
+
distributions.
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
Console script and module invocation are equivalent:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
sudo dhcp-discover
|
|
58
|
+
sudo python3 -m somestuff.dhcpstuff.dhcp_discover
|
|
59
|
+
sudo python3 -m somestuff.dhcpstuff
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| Flag | Description |
|
|
63
|
+
|---------------------|------------------------------------------------------------------------|
|
|
64
|
+
| `-i`, `--interface` | Bind to a specific interface (e.g. `eth0`) |
|
|
65
|
+
| `-t`, `--timeout` | Seconds to wait for responses (default: 5) |
|
|
66
|
+
| `-m`, `--mac` | MAC address to use (default: random locally-administered MAC) |
|
|
67
|
+
| `-a`, `--arch` | PXE client architecture for option 93: `bios`, `efi64` or `efi64-http` |
|
|
68
|
+
|
|
69
|
+
Examples:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# Basic discover on all interfaces
|
|
73
|
+
sudo dhcp-discover
|
|
74
|
+
|
|
75
|
+
# Specific interface, PXE EFI64 architecture
|
|
76
|
+
sudo dhcp-discover -i eth0 -a efi64
|
|
77
|
+
|
|
78
|
+
# Custom MAC and longer timeout
|
|
79
|
+
sudo dhcp-discover -m 02:aa:bb:cc:dd:ee -t 10
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Library
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from somestuff.dhcpstuff import build_discover, mac_bytes, parse_response
|
|
86
|
+
|
|
87
|
+
packet = build_discover(mac_bytes("02:aa:bb:cc:dd:ee"), xid=0x12345678)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The package also exports `parse_options`, `print_response`, `format_option`,
|
|
91
|
+
`fmt_ip`, `fmt_mac`, `main` and the constants `DHCP_SERVER_PORT`,
|
|
92
|
+
`DHCP_CLIENT_PORT`, `MAGIC_COOKIE`, `DHCP_MSG_TYPES`, `DHCP_OPTIONS` and
|
|
93
|
+
`PXE_ARCH_TYPES`.
|
|
94
|
+
|
|
95
|
+
## Privileges
|
|
96
|
+
|
|
97
|
+
`dhcp-discover` binds UDP port 68 and needs root (or `CAP_NET_BIND_SERVICE`). It
|
|
98
|
+
will not work while another DHCP client holds the port exclusively.
|
|
99
|
+
|
|
100
|
+
## Notes
|
|
101
|
+
|
|
102
|
+
- The repository additionally contains `diagnose-dhcp.sh`, a root-only (German)
|
|
103
|
+
shell script for finding unwanted DHCP on physical interfaces of Ubuntu Server
|
|
104
|
+
(cloud-init, Netplan, systemd-networkd, NetworkManager, DHCP client processes,
|
|
105
|
+
lease files, `ip=dhcp` kernel cmdline, udev/dispatcher hooks). It is not part
|
|
106
|
+
of the installed package:
|
|
107
|
+
https://github.com/vroomfondel/somestuff/tree/main/somestuff/dhcpstuff/
|
|
108
|
+
- Requires: no third-party dependencies. Linux only (`SO_BINDTODEVICE`).
|
|
109
|
+
- Licensed under the LGPL, see `LICENSE.md` in the repository root.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# dhcpstuff
|
|
2
|
+
|
|
3
|
+
DHCP Discover sender and response analyzer for network diagnostics: broadcasts a
|
|
4
|
+
DHCPDISCOVER and prints every answer, including regular DHCP offers and Proxy DHCP
|
|
5
|
+
responses from PXE boot servers. Standard library only.
|
|
6
|
+
|
|
7
|
+
## Modules
|
|
8
|
+
|
|
9
|
+
- **`dhcp_discover.py`** — builds a DHCPDISCOVER packet, broadcasts it to
|
|
10
|
+
`255.255.255.255:67` from UDP port 68 and collects replies for the given timeout.
|
|
11
|
+
Only replies matching the transaction ID are shown. Each response is labelled
|
|
12
|
+
`DHCP <type>` or, if the offered address (`yiaddr`) is `0.0.0.0`, `Proxy DHCP <type>`,
|
|
13
|
+
followed by offered IP, next server, server name, boot file and all parsed options
|
|
14
|
+
with human-readable names. With `--arch` the Discover carries PXE client
|
|
15
|
+
architecture (option 93) and a matching `PXEClient` vendor class (option 60),
|
|
16
|
+
which Proxy DHCP / PXE boot servers require before they answer.
|
|
17
|
+
- **`__main__.py`** — makes `python3 -m somestuff.dhcpstuff` run the same CLI.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install somestuff-dhcpstuff
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`somestuff` is a PEP 420 namespace; the package is imported as
|
|
26
|
+
`somestuff.dhcpstuff` and shares the namespace with the other `somestuff-*`
|
|
27
|
+
distributions.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
Console script and module invocation are equivalent:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
sudo dhcp-discover
|
|
35
|
+
sudo python3 -m somestuff.dhcpstuff.dhcp_discover
|
|
36
|
+
sudo python3 -m somestuff.dhcpstuff
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| Flag | Description |
|
|
40
|
+
|---------------------|------------------------------------------------------------------------|
|
|
41
|
+
| `-i`, `--interface` | Bind to a specific interface (e.g. `eth0`) |
|
|
42
|
+
| `-t`, `--timeout` | Seconds to wait for responses (default: 5) |
|
|
43
|
+
| `-m`, `--mac` | MAC address to use (default: random locally-administered MAC) |
|
|
44
|
+
| `-a`, `--arch` | PXE client architecture for option 93: `bios`, `efi64` or `efi64-http` |
|
|
45
|
+
|
|
46
|
+
Examples:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# Basic discover on all interfaces
|
|
50
|
+
sudo dhcp-discover
|
|
51
|
+
|
|
52
|
+
# Specific interface, PXE EFI64 architecture
|
|
53
|
+
sudo dhcp-discover -i eth0 -a efi64
|
|
54
|
+
|
|
55
|
+
# Custom MAC and longer timeout
|
|
56
|
+
sudo dhcp-discover -m 02:aa:bb:cc:dd:ee -t 10
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Library
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from somestuff.dhcpstuff import build_discover, mac_bytes, parse_response
|
|
63
|
+
|
|
64
|
+
packet = build_discover(mac_bytes("02:aa:bb:cc:dd:ee"), xid=0x12345678)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The package also exports `parse_options`, `print_response`, `format_option`,
|
|
68
|
+
`fmt_ip`, `fmt_mac`, `main` and the constants `DHCP_SERVER_PORT`,
|
|
69
|
+
`DHCP_CLIENT_PORT`, `MAGIC_COOKIE`, `DHCP_MSG_TYPES`, `DHCP_OPTIONS` and
|
|
70
|
+
`PXE_ARCH_TYPES`.
|
|
71
|
+
|
|
72
|
+
## Privileges
|
|
73
|
+
|
|
74
|
+
`dhcp-discover` binds UDP port 68 and needs root (or `CAP_NET_BIND_SERVICE`). It
|
|
75
|
+
will not work while another DHCP client holds the port exclusively.
|
|
76
|
+
|
|
77
|
+
## Notes
|
|
78
|
+
|
|
79
|
+
- The repository additionally contains `diagnose-dhcp.sh`, a root-only (German)
|
|
80
|
+
shell script for finding unwanted DHCP on physical interfaces of Ubuntu Server
|
|
81
|
+
(cloud-init, Netplan, systemd-networkd, NetworkManager, DHCP client processes,
|
|
82
|
+
lease files, `ip=dhcp` kernel cmdline, udev/dispatcher hooks). It is not part
|
|
83
|
+
of the installed package:
|
|
84
|
+
https://github.com/vroomfondel/somestuff/tree/main/somestuff/dhcpstuff/
|
|
85
|
+
- Requires: no third-party dependencies. Linux only (`SO_BINDTODEVICE`).
|
|
86
|
+
- Licensed under the LGPL, see `LICENSE.md` in the repository root.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "somestuff-dhcpstuff"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "DHCP Discover sender and response analyzer, including Proxy DHCP / PXE boot server responses (stdlib only)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.13"
|
|
11
|
+
authors = [{ name = "Henning Thieß", email = "ht@xomox.cc" }]
|
|
12
|
+
license-files = ["LICENSE.md"]
|
|
13
|
+
keywords = ["dhcp", "pxe", "proxy-dhcp", "network-diagnostics", "discover"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: System Administrators",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: System :: Networking",
|
|
21
|
+
"Topic :: System :: Boot",
|
|
22
|
+
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
|
|
23
|
+
"Operating System :: POSIX :: Linux",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = []
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
dhcp-discover = "somestuff.dhcpstuff.dhcp_discover:main"
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://github.com/vroomfondel/somestuff"
|
|
33
|
+
Repository = "https://github.com/vroomfondel/somestuff"
|
|
34
|
+
Issues = "https://github.com/vroomfondel/somestuff/issues"
|
|
35
|
+
|
|
36
|
+
[tool.hatch.version]
|
|
37
|
+
path = "somestuff/dhcpstuff/__init__.py"
|
|
38
|
+
|
|
39
|
+
# Build-level selection governs sdist AND wheel; a wheel-only `packages` leaves the sdist unscoped.
|
|
40
|
+
# A target-level `exclude` replaces the build-level list instead of extending it, so each target carries the full set.
|
|
41
|
+
[tool.hatch.build]
|
|
42
|
+
packages = ["somestuff"]
|
|
43
|
+
exclude = ["**/__pycache__", "**/*.pyc", "**/*.local.*", "**/*_deployment.yml", "**/*_cronjob.yml", "**/*.example", "**/*.example.yaml", "**/*.sh"]
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.sdist]
|
|
46
|
+
exclude = ["**/__pycache__", "**/*.pyc", "**/*.local.*", "**/*_deployment.yml", "**/*_cronjob.yml", "**/*.example", "**/*.example.yaml", "**/*.sh"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
exclude = ["**/__pycache__", "**/*.pyc", "**/*.local.*", "**/*_deployment.yml", "**/*_cronjob.yml", "**/*.example", "**/*.example.yaml", "**/*.sh", "**/README.md"]
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# dhcpstuff
|
|
2
|
+
|
|
3
|
+
DHCP Discover sender and response analyzer for network diagnostics: broadcasts a
|
|
4
|
+
DHCPDISCOVER and prints every answer, including regular DHCP offers and Proxy DHCP
|
|
5
|
+
responses from PXE boot servers. Standard library only.
|
|
6
|
+
|
|
7
|
+
## Modules
|
|
8
|
+
|
|
9
|
+
- **`dhcp_discover.py`** — builds a DHCPDISCOVER packet, broadcasts it to
|
|
10
|
+
`255.255.255.255:67` from UDP port 68 and collects replies for the given timeout.
|
|
11
|
+
Only replies matching the transaction ID are shown. Each response is labelled
|
|
12
|
+
`DHCP <type>` or, if the offered address (`yiaddr`) is `0.0.0.0`, `Proxy DHCP <type>`,
|
|
13
|
+
followed by offered IP, next server, server name, boot file and all parsed options
|
|
14
|
+
with human-readable names. With `--arch` the Discover carries PXE client
|
|
15
|
+
architecture (option 93) and a matching `PXEClient` vendor class (option 60),
|
|
16
|
+
which Proxy DHCP / PXE boot servers require before they answer.
|
|
17
|
+
- **`__main__.py`** — makes `python3 -m somestuff.dhcpstuff` run the same CLI.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install somestuff-dhcpstuff
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`somestuff` is a PEP 420 namespace; the package is imported as
|
|
26
|
+
`somestuff.dhcpstuff` and shares the namespace with the other `somestuff-*`
|
|
27
|
+
distributions.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
Console script and module invocation are equivalent:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
sudo dhcp-discover
|
|
35
|
+
sudo python3 -m somestuff.dhcpstuff.dhcp_discover
|
|
36
|
+
sudo python3 -m somestuff.dhcpstuff
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| Flag | Description |
|
|
40
|
+
|---------------------|------------------------------------------------------------------------|
|
|
41
|
+
| `-i`, `--interface` | Bind to a specific interface (e.g. `eth0`) |
|
|
42
|
+
| `-t`, `--timeout` | Seconds to wait for responses (default: 5) |
|
|
43
|
+
| `-m`, `--mac` | MAC address to use (default: random locally-administered MAC) |
|
|
44
|
+
| `-a`, `--arch` | PXE client architecture for option 93: `bios`, `efi64` or `efi64-http` |
|
|
45
|
+
|
|
46
|
+
Examples:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# Basic discover on all interfaces
|
|
50
|
+
sudo dhcp-discover
|
|
51
|
+
|
|
52
|
+
# Specific interface, PXE EFI64 architecture
|
|
53
|
+
sudo dhcp-discover -i eth0 -a efi64
|
|
54
|
+
|
|
55
|
+
# Custom MAC and longer timeout
|
|
56
|
+
sudo dhcp-discover -m 02:aa:bb:cc:dd:ee -t 10
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Library
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from somestuff.dhcpstuff import build_discover, mac_bytes, parse_response
|
|
63
|
+
|
|
64
|
+
packet = build_discover(mac_bytes("02:aa:bb:cc:dd:ee"), xid=0x12345678)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The package also exports `parse_options`, `print_response`, `format_option`,
|
|
68
|
+
`fmt_ip`, `fmt_mac`, `main` and the constants `DHCP_SERVER_PORT`,
|
|
69
|
+
`DHCP_CLIENT_PORT`, `MAGIC_COOKIE`, `DHCP_MSG_TYPES`, `DHCP_OPTIONS` and
|
|
70
|
+
`PXE_ARCH_TYPES`.
|
|
71
|
+
|
|
72
|
+
## Privileges
|
|
73
|
+
|
|
74
|
+
`dhcp-discover` binds UDP port 68 and needs root (or `CAP_NET_BIND_SERVICE`). It
|
|
75
|
+
will not work while another DHCP client holds the port exclusively.
|
|
76
|
+
|
|
77
|
+
## Notes
|
|
78
|
+
|
|
79
|
+
- The repository additionally contains `diagnose-dhcp.sh`, a root-only (German)
|
|
80
|
+
shell script for finding unwanted DHCP on physical interfaces of Ubuntu Server
|
|
81
|
+
(cloud-init, Netplan, systemd-networkd, NetworkManager, DHCP client processes,
|
|
82
|
+
lease files, `ip=dhcp` kernel cmdline, udev/dispatcher hooks). It is not part
|
|
83
|
+
of the installed package:
|
|
84
|
+
https://github.com/vroomfondel/somestuff/tree/main/somestuff/dhcpstuff/
|
|
85
|
+
- Requires: no third-party dependencies. Linux only (`SO_BINDTODEVICE`).
|
|
86
|
+
- Licensed under the LGPL, see `LICENSE.md` in the repository root.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""DHCP discovery and diagnostic tools."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.1"
|
|
4
|
+
|
|
5
|
+
from somestuff.dhcpstuff.dhcp_discover import (
|
|
6
|
+
DHCP_CLIENT_PORT,
|
|
7
|
+
DHCP_MSG_TYPES,
|
|
8
|
+
DHCP_OPTIONS,
|
|
9
|
+
DHCP_SERVER_PORT,
|
|
10
|
+
MAGIC_COOKIE,
|
|
11
|
+
PXE_ARCH_TYPES,
|
|
12
|
+
build_discover,
|
|
13
|
+
fmt_ip,
|
|
14
|
+
fmt_mac,
|
|
15
|
+
format_option,
|
|
16
|
+
mac_bytes,
|
|
17
|
+
main,
|
|
18
|
+
parse_options,
|
|
19
|
+
parse_response,
|
|
20
|
+
print_response,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"DHCP_CLIENT_PORT",
|
|
25
|
+
"DHCP_MSG_TYPES",
|
|
26
|
+
"DHCP_OPTIONS",
|
|
27
|
+
"DHCP_SERVER_PORT",
|
|
28
|
+
"MAGIC_COOKIE",
|
|
29
|
+
"PXE_ARCH_TYPES",
|
|
30
|
+
"build_discover",
|
|
31
|
+
"fmt_ip",
|
|
32
|
+
"fmt_mac",
|
|
33
|
+
"format_option",
|
|
34
|
+
"mac_bytes",
|
|
35
|
+
"main",
|
|
36
|
+
"parse_options",
|
|
37
|
+
"parse_response",
|
|
38
|
+
"print_response",
|
|
39
|
+
]
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Send DHCP Discover and display all responses (DHCP + Proxy DHCP)."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import random
|
|
6
|
+
import socket
|
|
7
|
+
import struct
|
|
8
|
+
import time
|
|
9
|
+
|
|
10
|
+
DHCP_SERVER_PORT = 67
|
|
11
|
+
DHCP_CLIENT_PORT = 68
|
|
12
|
+
MAGIC_COOKIE = b"\x63\x82\x53\x63"
|
|
13
|
+
|
|
14
|
+
DHCP_MSG_TYPES = {
|
|
15
|
+
1: "DISCOVER",
|
|
16
|
+
2: "OFFER",
|
|
17
|
+
3: "REQUEST",
|
|
18
|
+
4: "DECLINE",
|
|
19
|
+
5: "ACK",
|
|
20
|
+
6: "NAK",
|
|
21
|
+
7: "RELEASE",
|
|
22
|
+
8: "INFORM",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
DHCP_OPTIONS = {
|
|
26
|
+
1: "Subnet Mask",
|
|
27
|
+
3: "Router",
|
|
28
|
+
6: "DNS Server",
|
|
29
|
+
12: "Hostname",
|
|
30
|
+
15: "Domain Name",
|
|
31
|
+
28: "Broadcast Address",
|
|
32
|
+
43: "Vendor Specific",
|
|
33
|
+
50: "Requested IP",
|
|
34
|
+
51: "Lease Time",
|
|
35
|
+
53: "DHCP Message Type",
|
|
36
|
+
54: "DHCP Server ID",
|
|
37
|
+
58: "Renewal Time",
|
|
38
|
+
59: "Rebinding Time",
|
|
39
|
+
60: "Vendor Class ID",
|
|
40
|
+
61: "Client ID",
|
|
41
|
+
66: "TFTP Server Name",
|
|
42
|
+
67: "Bootfile Name",
|
|
43
|
+
93: "Client System Architecture",
|
|
44
|
+
97: "UUID/GUID",
|
|
45
|
+
175: "iPXE Encapsulated",
|
|
46
|
+
255: "End",
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def mac_bytes(mac_str: str) -> bytes:
|
|
51
|
+
return bytes(int(b, 16) for b in mac_str.split(":"))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def fmt_ip(raw: bytes) -> str:
|
|
55
|
+
return ".".join(str(b) for b in raw)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def fmt_mac(raw: bytes) -> str:
|
|
59
|
+
return ":".join(f"{b:02x}" for b in raw)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
PXE_ARCH_TYPES = {
|
|
63
|
+
"bios": (0, "PXEClient:Arch:00000:UNDI:002001"),
|
|
64
|
+
"efi64": (7, "PXEClient:Arch:00007:UNDI:003016"),
|
|
65
|
+
"efi64-http": (9, "PXEClient:Arch:00009:UNDI:003016"),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def build_discover(mac: bytes, xid: int, arch: int | None = None, vendor_class: str | None = None) -> bytes:
|
|
70
|
+
"""Build a DHCPDISCOVER packet."""
|
|
71
|
+
pkt = struct.pack("!BBBB", 1, 1, 6, 0) # op, htype, hlen, hops
|
|
72
|
+
pkt += struct.pack("!I", xid) # xid
|
|
73
|
+
pkt += struct.pack("!HH", 0, 0x8000) # secs, flags (broadcast)
|
|
74
|
+
pkt += b"\x00" * 4 * 4 # ciaddr, yiaddr, siaddr, giaddr
|
|
75
|
+
pkt += mac + b"\x00" * 10 # chaddr (16 bytes)
|
|
76
|
+
pkt += b"\x00" * 64 # sname
|
|
77
|
+
pkt += b"\x00" * 128 # file
|
|
78
|
+
pkt += MAGIC_COOKIE
|
|
79
|
+
# Options
|
|
80
|
+
pkt += b"\x35\x01\x01" # 53: DHCPDISCOVER
|
|
81
|
+
pkt += b"\x37\x08\x01\x03\x06\x0f\x1c\x42\x43\xaf" # 55: param request list
|
|
82
|
+
if arch is not None:
|
|
83
|
+
pkt += struct.pack("!BBH", 93, 2, arch) # 93: Client System Architecture
|
|
84
|
+
if vendor_class is not None:
|
|
85
|
+
vc = vendor_class.encode("ascii")
|
|
86
|
+
pkt += struct.pack("!BB", 60, len(vc)) + vc # 60: Vendor Class Identifier
|
|
87
|
+
pkt += b"\xff" # end
|
|
88
|
+
return pkt
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def parse_options(data: bytes) -> dict:
|
|
92
|
+
"""Parse DHCP options from raw bytes."""
|
|
93
|
+
opts = {}
|
|
94
|
+
i = 0
|
|
95
|
+
while i < len(data):
|
|
96
|
+
code = data[i]
|
|
97
|
+
if code == 255:
|
|
98
|
+
break
|
|
99
|
+
if code == 0:
|
|
100
|
+
i += 1
|
|
101
|
+
continue
|
|
102
|
+
length = data[i + 1]
|
|
103
|
+
value = data[i + 2 : i + 2 + length]
|
|
104
|
+
opts[code] = value
|
|
105
|
+
i += 2 + length
|
|
106
|
+
return opts
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def format_option(code: int, value: bytes) -> str:
|
|
110
|
+
"""Format a DHCP option value for display."""
|
|
111
|
+
if code == 53:
|
|
112
|
+
return DHCP_MSG_TYPES.get(value[0], f"Unknown({value[0]})")
|
|
113
|
+
if code in (1, 3, 6, 28, 50, 54):
|
|
114
|
+
ips = [fmt_ip(value[j : j + 4]) for j in range(0, len(value), 4)]
|
|
115
|
+
return ", ".join(ips)
|
|
116
|
+
if code in (51, 58, 59):
|
|
117
|
+
return f"{struct.unpack('!I', value)[0]}s"
|
|
118
|
+
if code in (12, 15, 60, 66, 67):
|
|
119
|
+
return value.decode("ascii", errors="replace")
|
|
120
|
+
return value.hex()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def parse_response(data: bytes) -> dict | None:
|
|
124
|
+
"""Parse a DHCP response packet."""
|
|
125
|
+
if len(data) < 240:
|
|
126
|
+
return None
|
|
127
|
+
if data[236:240] != MAGIC_COOKIE:
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
info = {
|
|
131
|
+
"op": data[0],
|
|
132
|
+
"yiaddr": fmt_ip(data[16:20]),
|
|
133
|
+
"siaddr": fmt_ip(data[20:24]),
|
|
134
|
+
"giaddr": fmt_ip(data[24:28]),
|
|
135
|
+
"chaddr": fmt_mac(data[28:34]),
|
|
136
|
+
"sname": data[44:108].rstrip(b"\x00").decode("ascii", errors="replace"),
|
|
137
|
+
"file": data[108:236].rstrip(b"\x00").decode("ascii", errors="replace"),
|
|
138
|
+
"options": parse_options(data[240:]),
|
|
139
|
+
}
|
|
140
|
+
return info
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def print_response(info: dict, idx: int) -> None:
|
|
144
|
+
"""Print a parsed DHCP response."""
|
|
145
|
+
msg_type_raw = info["options"].get(53, b"\x00")[0]
|
|
146
|
+
msg_type = DHCP_MSG_TYPES.get(msg_type_raw, f"Unknown({msg_type_raw})")
|
|
147
|
+
server_id = fmt_ip(info["options"][54]) if 54 in info["options"] else "?"
|
|
148
|
+
is_proxy = info["yiaddr"] == "0.0.0.0"
|
|
149
|
+
|
|
150
|
+
label = f"Proxy DHCP {msg_type}" if is_proxy else f"DHCP {msg_type}"
|
|
151
|
+
print(f"\n{'='*60}")
|
|
152
|
+
print(f"Response #{idx}: {label} from {server_id}")
|
|
153
|
+
print(f"{'='*60}")
|
|
154
|
+
|
|
155
|
+
if not is_proxy:
|
|
156
|
+
print(f" Offered IP: {info['yiaddr']}")
|
|
157
|
+
if info["siaddr"] != "0.0.0.0":
|
|
158
|
+
print(f" Next Server: {info['siaddr']}")
|
|
159
|
+
if info["sname"]:
|
|
160
|
+
print(f" Server Name: {info['sname']}")
|
|
161
|
+
if info["file"]:
|
|
162
|
+
print(f" Boot File: {info['file']}")
|
|
163
|
+
|
|
164
|
+
print(f" Options:")
|
|
165
|
+
for code, value in sorted(info["options"].items()):
|
|
166
|
+
name = DHCP_OPTIONS.get(code, f"Option {code}")
|
|
167
|
+
print(f" {name} ({code}): {format_option(code, value)}")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def main() -> None:
|
|
171
|
+
parser = argparse.ArgumentParser(description="DHCP Discover - show DHCP and Proxy DHCP responses")
|
|
172
|
+
parser.add_argument("-i", "--interface", help="Bind to specific interface (e.g. eth0)")
|
|
173
|
+
parser.add_argument("-t", "--timeout", type=float, default=5.0, help="Seconds to wait for responses (default: 5)")
|
|
174
|
+
parser.add_argument("-m", "--mac", default=None, help="MAC address to use (default: random)")
|
|
175
|
+
parser.add_argument(
|
|
176
|
+
"-a",
|
|
177
|
+
"--arch",
|
|
178
|
+
choices=list(PXE_ARCH_TYPES.keys()),
|
|
179
|
+
help="PXE client architecture for option 93 (required for Proxy DHCP/PXE boot servers)",
|
|
180
|
+
)
|
|
181
|
+
args = parser.parse_args()
|
|
182
|
+
|
|
183
|
+
if args.mac:
|
|
184
|
+
mac = mac_bytes(args.mac)
|
|
185
|
+
else:
|
|
186
|
+
mac = bytes([0x02] + [random.randint(0, 255) for _ in range(5)])
|
|
187
|
+
print(f"Using random MAC: {fmt_mac(mac)}")
|
|
188
|
+
|
|
189
|
+
xid = random.randint(1, 0xFFFFFFFF)
|
|
190
|
+
|
|
191
|
+
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
192
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
193
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
194
|
+
if args.interface:
|
|
195
|
+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, args.interface.encode() + b"\0")
|
|
196
|
+
sock.bind(("", DHCP_CLIENT_PORT))
|
|
197
|
+
sock.settimeout(1.0)
|
|
198
|
+
|
|
199
|
+
if args.arch:
|
|
200
|
+
arch_code, vendor_class = PXE_ARCH_TYPES[args.arch]
|
|
201
|
+
else:
|
|
202
|
+
arch_code, vendor_class = None, None
|
|
203
|
+
pkt = build_discover(mac, xid, arch=arch_code, vendor_class=vendor_class)
|
|
204
|
+
sock.sendto(pkt, ("255.255.255.255", DHCP_SERVER_PORT))
|
|
205
|
+
arch_info = f", arch={args.arch}({arch_code}), vendor={vendor_class}" if arch_code is not None else ""
|
|
206
|
+
print(f"Sent DHCPDISCOVER (xid=0x{xid:08x}{arch_info}), waiting {args.timeout}s for responses...")
|
|
207
|
+
|
|
208
|
+
responses = []
|
|
209
|
+
deadline = time.monotonic() + args.timeout
|
|
210
|
+
while time.monotonic() < deadline:
|
|
211
|
+
try:
|
|
212
|
+
data, addr = sock.recvfrom(4096)
|
|
213
|
+
except socket.timeout:
|
|
214
|
+
continue
|
|
215
|
+
info = parse_response(data)
|
|
216
|
+
if info and struct.unpack("!I", struct.pack("!I", xid))[0] == struct.unpack("!I", data[4:8])[0]:
|
|
217
|
+
responses.append((addr, info))
|
|
218
|
+
print_response(info, len(responses))
|
|
219
|
+
|
|
220
|
+
sock.close()
|
|
221
|
+
|
|
222
|
+
if not responses:
|
|
223
|
+
print("\nNo responses received.")
|
|
224
|
+
else:
|
|
225
|
+
print(f"\n{len(responses)} response(s) received.")
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
if __name__ == "__main__":
|
|
229
|
+
main()
|
|
File without changes
|