繁体   English   中英

使用IP地址和AWK Bash检索网络接口的名称

[英]Retrieve the name of a Network Interface using an IP Address and AWK Bash

我试图在CentOS 6.4上使用Bash来检索使用AWK附加到IP地址的网络接口名称。 我从Solaris盒子里得到了一些命令,但我不确定如何将它转换为Linux输出。

该命令如下所示:

ifconfig -a | awk '
    $1 ~ /:/    {split($1,nic,":"); 
                     lastif=sprintf("%s:%s",nic[1],nic[2]);}
    $2 == "'$1'"    { print lastif ; exit; }

    '

它是脚本的一部分,所以它需要像monitor.sh xxxx yyyy这样的命令行参数,它使用第一个xxxx获取接口名称,然后使$ 1 == $ 2,这样它就可以在以后ping yyyy。 我猜测在Solaris中, ifconfig -a输出与CentOS不同。 如果IP和接口在同一行,我可以得到接口名称,但在linux中,它们在两个不同的行上。 有任何想法吗。

我没有CentOS,但在RHEL中,IP地址被列为inet地址。 我相信他们应该是一样的。

以下命令应该为您提供具有IP地址的接口名称。

export iface=$(ifconfig | grep -B1 "inet addr:x.x.x.x" | awk '$1!="inet" && $1!="--" {print $1}')

echo "$iface" # To get the interface name for x.x.x.x ip

这个应该显示IP包括localhost:

ifconfig | grep "inet addr:" | sed -e 's/addr:/addr: /g' | awk '{print $3}'

为127.0.0.1(或任何其他IP)创建ifname

ifconfig | awk '/127.0.0.1/ {print $1}' RS="\n\n"
lo

得到ip:

ifconfig | awk -F"[ :]+" '/inet addr:/ {print $4}'

发布ifconfig的输出,我可以帮助您微调您的操作系统

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM