如何获取给定网络接口的 IP 地址?

发布于 2024-12-02 17:09:05 字数 1286 浏览 1 评论 0原文

Python中是否有一个函数可以获取给定网络接口的IP地址? 有没有一个命令行程序可以做到这一点?

我想获取en1的IP地址。

这是我机器上 ifconfig 的结果。

$  ifconfig 
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 127.0.0.1 netmask 0xff000000 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:23:32:be:4e:90 
    media: autoselect
    status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:23:6c:7e:3e:0c 
    inet6 fe80::223:6cff:fe7e:3e0c%en1 prefixlen 64 scopeid 0x5 
    inet 192.168.1.70 netmask 0xffffff00 broadcast 192.168.1.255
    media: autoselect
    status: active
vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:01 
    inet 172.16.64.1 netmask 0xffffff00 broadcast 172.16.64.255
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:08 
    inet 172.16.174.1 netmask 0xffffff00 broadcast 172.16.174.255
vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00

In Python is there a function to get the IP address of a given network interface?
Is there a command line programme that get do this?

I want to get the IP address of en1.

This is the result of ifconfig on my machine.

$  ifconfig 
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    inet 127.0.0.1 netmask 0xff000000 
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:23:32:be:4e:90 
    media: autoselect
    status: inactive
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:23:6c:7e:3e:0c 
    inet6 fe80::223:6cff:fe7e:3e0c%en1 prefixlen 64 scopeid 0x5 
    inet 192.168.1.70 netmask 0xffffff00 broadcast 192.168.1.255
    media: autoselect
    status: active
vmnet1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:01 
    inet 172.16.64.1 netmask 0xffffff00 broadcast 172.16.64.255
vmnet8: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 00:50:56:c0:00:08 
    inet 172.16.174.1 netmask 0xffffff00 broadcast 172.16.174.255
vboxnet0: flags=8842<BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

各空 2024-12-09 17:09:05

$ sudo pip install netifaces

$ python

Python 2.6.1(r261:67515,2010 年 6 月 24 日,21:47:49)
[GCC 4.2.1(Apple Inc. build 5646)] 达尔文
输入“帮助”、“版权”、“制作人员”或“许可证”以获取更多信息。

<块引用>
<块引用>

导入网络

netifaces.ifaddresses('en0')


{18: [{'addr': '10:9a:dd:52:89:dc'}], 2: [{'广播': '10.188.55.255', '网络掩码':'255.255.254.0','地址': '10.188.55.130'}], 30: [{'网络掩码': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::129a:ddff:fe52:89dc%en0'}]

$ sudo pip install netifaces

$ python

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

import netifaces

netifaces.ifaddresses('en0')

{18: [{'addr': '10:9a:dd:52:89:dc'}], 2: [{'broadcast': '10.188.55.255', 'netmask': '255.255.254.0', 'addr': '10.188.55.130'}], 30: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr': 'fe80::129a:ddff:fe52:89dc%en0'}]

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