天天看點

根據 裝置名(br0/eth0/em0)稱擷取 目前機器的IP位址與子網路遮罩資訊

#!/usr/bin/env python 

'''

根據 裝置名(br0/eth0/em0)稱擷取 目前機器的IP位址與子網路遮罩資訊

import socket, struct, fcntl

def get_ipaddress(ifname = 'eth0'):

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

    return socket.inet_ntoa(fcntl.ioctl(

        s.fileno(),

        0x8915,  # SIOCGIFADDR

        struct.pack('24s',ifname))[20:24])

def get_netmask(ifname = 'eth0'):

        0x891b,  #SIOCGIFNETMASK

print get_ipaddress('eth0')

print get_netmask('eth0')

本文轉自 swq499809608 51CTO部落格,原文連結:http://blog.51cto.com/swq499809608/1130673

繼續閱讀