python 获取linux 本机IP地址
#!/usr/bin/env python#coding: utf-8import psutilimport socketimport structimport fcntldef getip(ethname):s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)return socket.inet_ntoa(fcn
·
#!/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'}
更多推荐
已为社区贡献1条内容
所有评论(0)