python获取linux主机某个网卡的IP

#!/usr/bin/python

# -*- coding: UTF-8 -*-

import socket, fcntl, struct

 

def get_local_ip(ifname):

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))

    ret = socket.inet_ntoa(inet[20:24])

    return ret

print(get_local_ip("eth0"))

print(get_local_ip("lo"))

你可能感兴趣的:(python)