如何获取只有一个接口的IP

发布于 2024-09-28 18:42:59 字数 339 浏览 6 评论 0原文

当我尝试 ifconfig 时,它为我提供了有关网络适配器的全部信息。

我尝试过:

system( "ifconfig -a | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;

输出两个 Ips:

  inet  addr:17.24.17.229
  inet  addr:127.0.0.1

但我只需要第一个,我怎样才能将其过滤掉。

When I tried ifconfig it gives me the whole all the information regarding the Network Adapter.

I tried :

system( "ifconfig -a | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;

which output two Ips :

  inet  addr:17.24.17.229
  inet  addr:127.0.0.1

But I need just the 1st one , How can I filter this out.

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

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

发布评论

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

评论(8

年华零落成诗 2024-10-05 18:42:59

要在 Linux 中获取单个接口 (eth0) 的 IP 地址:

ip address show dev eth0

您可以使用 IP 正则表达式通过 grep 进行管道传输,并仅选择第一个找到地址:

ip a show dev eth0 | grep -oP '(?:\b\.?(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){4} ' | head -1

该设备可能有多个与其关联的 IP 地址,因此您可能需要调整 | head -1 位取决于您要选择的地址。您还必须过滤掉广播地址。

To get the IP address of a single interface (eth0) in Linux:

ip address show dev eth0

You can pipe this through grep using a IP regular expression and select just the first address found:

ip a show dev eth0 | grep -oP '(?:\b\.?(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){4}' | head -1

This device might have more than one IP address associated with it so you could have to adjust the | head -1 bit depending on which address you would like to pick. You will also have to filter out broadcast addresses.

江城子 2024-10-05 18:42:59

我会使用 iproute2ip:(

ip -o addr show dev eth0 | while read IFNUM IFNAME ADDRTYPE ADDR REST; do [ "$ADDRTYPE" == "inet" ] && echo $ADDR; done
9.87.65.43/21

不仅因为它更容易解析,而且它还会显示辅助 IP,例如 ifconfig 不能。)

I'd use iproute2's ip:

ip -o addr show dev eth0 | while read IFNUM IFNAME ADDRTYPE ADDR REST; do [ "$ADDRTYPE" == "inet" ] && echo $ADDR; done
9.87.65.43/21

(Not only because it's easier to parse, but it'll also show e.g. secondary IPs, which ifconfig can't.)

寄意 2024-10-05 18:42:59

您可能会使用 head 但是...

我当然可能会错,但我的猜测是您并不真正需要 first

您可能正在寻找连接到网关(或互联网)的设备。

据我所知,IP 地址或接口的顺序未指定

您到底想实现什么目标?

如果你想知道哪个接口“连接到互联网”,更可靠的方法是找到具有默认路由(使用路由)的接口,然后使用ifconfig 直接获取正确的IP地址。

You might use head but...

I might be mistaking of course, but my guess is that you don't really need the first one.

You're probably looking for the one that is connected to the gateway (or the Internet).

As far as I know, the order of the IP addresses or interfaces is unspecified.

What do you want to achieve exactly ?

If you want to know what interface is "connected to the internet", a more reliable approach is to find the interface which has the default route (using route) then to use ifconfig <interface> to directly get the correct IP address.

弥繁 2024-10-05 18:42:59

您可以减少 grephead 的使用

ifconfig -a | sed -nr -e '/inet\b/{s|^.*inet\s+addr:(.[^ \t]*).*|\1|;h}' -e '${x;p}'

you can reduce the use of grep and head

ifconfig -a | sed -nr -e '/inet\b/{s|^.*inet\s+addr:(.[^ \t]*).*|\1|;h}' -e '${x;p}'
夏至、离别 2024-10-05 18:42:59

不要查看所有适配器,只查看您想要的适配器。

system( "ifconfig -a eth0 | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;

Don't look at all of the adapters, just the one you want.

system( "ifconfig -a eth0 | grep inet | "
          "sed 's/\\([ ]*[^ ]*\\)\\([ ]*[^ ]*\\).*$/\\1 \\2/' "
          " > address.txt" ) ;
终遇你 2024-10-05 18:42:59

如果 ifconfigip 的输出发生变化,您的程序将会崩溃。

为什么不直接使用 SIOCGIFCONF ioctl() 并直接从源代码获取它呢?

ETA:假设任何给定系统只有一个环回接口和一个具有单一地址的以太网也是不安全的。

If the output of ifconfig or ip ever changes, your program will break.

Why not not just use the SIOCGIFCONF ioctl() and get it directly from the source?

ETA: It's also unsafe to assume that any given system will have just one loopback interface and just one Ethernet with a single address.

美人骨 2024-10-05 18:42:59

“ifconfig eth0”怎么样?

How about 'ifconfig eth0'?

水溶 2024-10-05 18:42:59

主机名-I | awk '{print $1}'

hostname -I | awk '{print $1}'

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