2011-09-02 5 views
0

파이썬에는 주어진 네트워크 인터페이스의 IP 주소를 가져 오는 함수가 있습니까? 이렇게하는 명령 줄 프로그램이 있습니까?주어진 네트워크 인터페이스의 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 
+0

http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib –

+0

[인터페이스에서 실제 인터페이스 IP 주소를 얻는 방법] (http :// /stackoverflow.com/questions/6243276/how-to-get-the-physical-interface-ip-address-from-an-interface) – zeekay

답변

2

$를 sudo를 핍 netifaces

에게

$ 파이썬

파이썬 2.6.1 (R261 : 67,515 2010 년 6 월 24 일 21시 47분 49초)를 설치 [GCC 4.2.1 (애플 Inc의를 . build 5646)] darwin 자세한 내용은 "help", "copyright", "credits"또는 "license"를 입력하십시오.

수입 netifaces

netifaces.ifaddresses ('EN0')

{18 : [{ 'ADDR''10 : 9A : DD : 'netmask': '255.255.254.0', 'addr': '10 .188.55.130 '}], 30 : [{' ' netmask ':'ffff : ffff : ffff : ffff :: ','addr ':'fe80 :: 129a : ddff : fe52 : 89dc % en0 '}]

관련 문제