import os
def get_ip_addresses():
spliter = "$$$"
def parse(info):
info = [i.strip() for i in info.split(spliter)]
eth_name = info[0].split(': ')[0]
v4info = [i.split(' ')[1] for i in info if 'inet' in i and 'inet6' not in i]
v6info = [i.split(' ')[1] for i in info if 'inet6' in i]
return {'eth_name': eth_name, 'v6_ip': v6info, 'v4_ip': v4info}
# get ifconfig result
ifconfig_context = os.popen('ifconfig').read()
# group info by card name
card_info = ifconfig_context.replace("\n ", spliter).split('\n')
# group parse
return [info for info in [parse(i) for i in card_info] if info['eth_name']]
暴力解析ifconfig
最新推荐文章于 2024-08-22 07:51:42 发布