如何设置“IP地址”、“DNS”、“主机名”、“MAC地址”在Python中?
我想编写一个用于网络安全IP扫描目的的脚本,这样的工具可能需要欺骗其主机NIC状态以进行测试目的,例如, 设置NIC的IP地址, 设置 DNS 地址, 在设置主机名时, MAC 地址和 启用/禁用 NIC 适配器。
我用谷歌搜索,发现大多数灵魂是使用“popen”调用系统现有工具,例如
>>> import os
>>> p=os.popen("/sbin/ifconfig eth0")
>>> t=p.read()
>>> p.close()
从系统获取返回。还有一些模块可以读取网卡的状态,例如netifaces,但似乎它们都是“READ ONLY”但不能直接写入。
由于我发现没有模块可以直接设置网卡状态,所以我在这里寻求帮助,看看是否有人可以帮忙或展示更好的方法。
任何提示都会被应用。 谢谢!
参考值 肯尼迪
I want to write a script for network security IP scan porpose, such a tool may need spoofing it's host NIC status for testing purpose, for example,
to setup NIC's ip address,
to setup DNS address,
while to setup hostname,
MAC address and
enable/disable the NICs adapter.
I googled and found most soultion is using "popen" invoking system existing tools such as
>>> import os
>>> p=os.popen("/sbin/ifconfig eth0")
>>> t=p.read()
>>> p.close()
to get return from system. Also there's modules can reading the status of the NIC, such as netifaces, but seems all of them are "READ ONLY" but can not wirte directly.
Since I found no modules can setup the NIC status directly, then I asks help here to see if someone can give a hand or show a more better way.
Any hints will be appricated.
Thanks!
Rgs
KC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前这样做过,没有明确的 Python 操作系统接口来连接低级操作系统特定的东西。您只需为 UNIX(/Windows/Mac) 命令编写一个(特定于操作系统的)包装器。这并不难,只是乏味。
os.popen*() 已非常弃用(自 2.4 起),请使用 subprocess 模块。
subprocess.Popen() 的接口确实令人恼火 ,您几乎肯定最终会为了自己的理智而编写一个包装器,以提供合理的默认参数值并整理您的代码。显然要密切关注命令退出状态、路径前缀等。不确定您有多关心可移植性。
I did this previously, there's no explicit Python os interface to low-level OS-specific stuff like that. You just have to write an (OS-specific) wrapper to UNIX(/Windows/Mac) commands. It's not hard, just tedious.
os.popen*() is very deprecated (since 2.4), use subprocess module.
The interface to subprocess.Popen() is admittedly irritating, you'll almost surely end up writing a wrapper for your own sanity to supply sane default arg values and unclutter your code. Obviously pay close attention to command exit statuses, path prefixes etc. Not sure how much you care about portability.