如何获取格式为&“接口:IP地址&"的输出从Mac上的ifconfig [英] How to get an output formatted as "Interface: IP Address" from ifconfig on Mac

查看:110
本文介绍了如何获取格式为&“接口:IP地址&"的输出从Mac上的ifconfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从ifconfig中获取以下格式化输出:

I am trying to get the following formatted output out of ifconfig:

en0: 10.52.30.105
en1: 10.52.164.63

我至少可以通过以下命令弄清楚如何仅获取IP地址(淘汰本地主机),但这不足以满足我的要求:

I've been able to at least figure out how to get just the IP addresses (weeding out localhost) with the following command, but it's not sufficient for my requirements:

ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'

谢谢!

推荐答案

这适用于FreeBSD,它是苹果的核心:-)

This works on FreeBSD, which is at the heart of an apple :-)

#!/bin/sh
for i in $(ifconfig -l); do
   case $i in
   (lo0)
      ;;
   (*)
      set -- $(ifconfig $i | grep "inet [1-9]")
      if test $# -gt 1; then
         echo $i: $2
      fi
   esac
done

这篇关于如何获取格式为&“接口:IP地址&"的输出从Mac上的ifconfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆