简体   繁体   English

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

[英]How do you get the IP address of a given network interface?

In Python is there a function to get the IP address of a given network interface? 在Python中是否有一个函数来获取给定网络接口的IP地址? Is there a command line programme that get do this? 是否有命令行程序可以做到这一点?

I want to get the IP address of en1. 我想获得en1的IP地址。

This is the result of ifconfig on my machine. 这是我机器上的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

$ sudo pip install netifaces $ sudo pip install netifaces

$ python $ 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. Python 2.6.1(r261:67515,2010年6月24日,21:47:49)[GCC 4.2.1(Apple Inc. build 5646)]关于darwin类型“帮助”,“版权”,“信用”或“许可”欲获得更多信息。

import netifaces 导入netifaces

netifaces.ifaddresses('en0') 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'}] {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'} ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从网络上的IP地址获取mac地址? - How do you get a mac address from an IP address on your network? PyAMF:您如何获得客户的IP地址? - PyAMF: How do you get the client's ip address? 如何从 Python 中的 NIC(网络接口控制器)获取 IP 地址? - How can I get the IP address from a NIC (network interface controller) in Python? 给定Python中的起始网络地址,如何获取下一个网络地址 - How to get the next network address given the starting network address in python 如何从一个接口获取物理接口IP地址 - How to get the physical interface IP address from an interface 如何在 Python 中从此文本文件中获取接口名称和 IP 地址 - How to get interface name and IP address from this text file in Python 如何在python中获取第二局域网中IP地址的列表 - How to get the list of IP address in 2nd LAN network in python 如何使用Fabric更改计算机IP地址而不挂机? - How do you change a computers IP address with Fabric without hanging? 在 Python 中,您如何确定 IP 地址是否是私有的? - How do you determine if an IP address is private, in Python? 如何使用Python boto判断AWS Elastic Network Interface(ENI)是否具有公共IP地址? - How to tell if AWS Elastic Network Interface (ENI) has a public IP address using Python boto?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM