如何设置“IP地址”、“DNS”、“主机名”、“MAC地址”在Python中?

发布于 2024-09-11 16:56:23 字数 476 浏览 3 评论 0原文

我想编写一个用于网络安全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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-09-18 16:56:23
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 
# Copyright 2014 by mimvp.com


def get_valid_ip(cls, ip_str):
    ip_str_new = ''
    ip_re = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
    ipList = ip_re.findall(ip_str)
    if len(ipList) >= 1:
        ip_str_new = ipList[0]
        ip_str_new = ip_str_new.lstrip("0")     # '05.9.87.163'  ==>  '5.9.87.163'
    return ip_str_new


if __name__ == '__main__':
    result = os.popen("/sbin/ifconfig en0 | grep broadcast")
    ip_inet = result.read()
    for ip_str in ip_inet.split(" "):
        if self.get_valid_ip(ip_str) : 
            print ip_str
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 
# Copyright 2014 by mimvp.com


def get_valid_ip(cls, ip_str):
    ip_str_new = ''
    ip_re = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
    ipList = ip_re.findall(ip_str)
    if len(ipList) >= 1:
        ip_str_new = ipList[0]
        ip_str_new = ip_str_new.lstrip("0")     # '05.9.87.163'  ==>  '5.9.87.163'
    return ip_str_new


if __name__ == '__main__':
    result = os.popen("/sbin/ifconfig en0 | grep broadcast")
    ip_inet = result.read()
    for ip_str in ip_inet.split(" "):
        if self.get_valid_ip(ip_str) : 
            print ip_str
偏爱你一生 2024-09-18 16:56:23

我之前这样做过,没有明确的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文