python分别获取虚拟网卡和真实网卡ip
Posted navysummer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python分别获取虚拟网卡和真实网卡ip相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python # -*- coding: utf-8 -*- import commands import socket import fcntl import struct CMD_VIR_NETCARD = "ls /sys/devices/virtual/net/" CMD_ALL_NETCARD = "cat /proc/net/dev | awk ‘{i++; if(i>2){print $1}}‘ | sed ‘s/^[\t]*//g‘ | sed ‘s/[:]*$//g‘" def get_ip_address(netcard_name): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, struct.pack(‘256s‘, netcard_name[:15]) )[20:24]) def get_netcard(CMD): return_code, output = commands.getstatusoutput(CMD) vnic_list = output.split(‘\n‘) return set(vnic_list) def main(): vir_netcard_name = get_netcard(CMD_VIR_NETCARD) all_netcard_name = get_netcard(CMD_ALL_NETCARD) rel_netcard = [{item:get_ip_address(item)} for item in all_netcard_name if item not in vir_netcard_name] vir_netcard = [{item:get_ip_address(item)} for item in all_netcard_name if item in vir_netcard_name] print(rel_netcard) print(vir_netcard) if __name__ == ‘__main__‘: main()
以上是关于python分别获取虚拟网卡和真实网卡ip的主要内容,如果未能解决你的问题,请参考以下文章