#!/usr/bin/env python
#coding: utf-8
import psutil
import socket
import struct
import fcntl
def getip(ethname):
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0X8915, struct.pack('256s', ethname[:15]))[20:24])
ipList = {}
ditNet = psutil.network_io_counters(pernic=True)
for strNet in ditNet.keys():
ipList[strNet] = getip(strNet)
print(ipList)
需要先安装psutil模块
输出结果:{'lo': '127.0.0.1', 'eth0': '192.168.1.205'}