如何获取格式为“接口:IP 地址”的输出从 Mac 上的 ifconfig

发布于 2024-12-01 12:32:08 字数 267 浏览 0 评论 0原文

我试图从 ifconfig 中获取以下格式化输出:

en0: 10.52.30.105
en1: 10.52.164.63

我至少能够弄清楚如何使用以下命令获取 IP 地址(淘汰 localhost),但这不足以满足我的要求:

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

谢谢!

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

en0: 10.52.30.105
en1: 10.52.164.63

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}'

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-12-08 12:32:08

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

#!/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

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
北方的巷 2024-12-08 12:32:08

在 Debian/RHEL 系统上,您可以执行以下操作 ---

#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "Interface: IP : MASK : BROADCAST : HWADDR"

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

for i in $(ifconfig -a| grep -v ^$| grep ^[a-z*] | awk '{print $1}')

do

     case $i in 

           (lo)
                   ;;

           (*)
         ip=`(/sbin/ifconfig $i | awk /'inet addr/ {print $2}' | cut -f2 -d":" )`
         bcast=`(/sbin/ifconfig $i | awk /'Bcast/ {print $3}' | cut -f2 -d":" )`
         mask=`(/sbin/ifconfig $i | awk /'inet addr/ {print $4}' | cut -f2 -d":" )`
         hwaddr=`(/sbin/ifconfig $i | awk /'HWaddr/ {print $4,$5}' | cut -f2 -d" " )`

         if [ -z $ip ]; then
            ip="NA"
         fi

         if [ -z $bcast ]; then
           bcast="NA"
         fi

         if [ -z $mask ]; then
           mask="NA"
         fi

         if [ -z $hwaddr ]; then
           hwaddr="NA"
         fi

            echo $i: $ip : $mask : $bcast : $hwaddr
            ;;

    esac
done

On Debian/RHEL systems you can do the following ---

#!/bin/sh
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "Interface: IP : MASK : BROADCAST : HWADDR"

echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

for i in $(ifconfig -a| grep -v ^$| grep ^[a-z*] | awk '{print $1}')

do

     case $i in 

           (lo)
                   ;;

           (*)
         ip=`(/sbin/ifconfig $i | awk /'inet addr/ {print $2}' | cut -f2 -d":" )`
         bcast=`(/sbin/ifconfig $i | awk /'Bcast/ {print $3}' | cut -f2 -d":" )`
         mask=`(/sbin/ifconfig $i | awk /'inet addr/ {print $4}' | cut -f2 -d":" )`
         hwaddr=`(/sbin/ifconfig $i | awk /'HWaddr/ {print $4,$5}' | cut -f2 -d" " )`

         if [ -z $ip ]; then
            ip="NA"
         fi

         if [ -z $bcast ]; then
           bcast="NA"
         fi

         if [ -z $mask ]; then
           mask="NA"
         fi

         if [ -z $hwaddr ]; then
           hwaddr="NA"
         fi

            echo $i: $ip : $mask : $bcast : $hwaddr
            ;;

    esac
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文