如何获取只有一个接口的IP
当我尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要在 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.我会使用
iproute2
的ip
:(不仅因为它更容易解析,而且它还会显示辅助 IP,例如
ifconfig
不能。)I'd use
iproute2
'sip
:(Not only because it's easier to parse, but it'll also show e.g. secondary IPs, which
ifconfig
can't.)您可能会使用
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 useifconfig <interface>
to directly get the correct IP address.您可以减少
grep
和head
的使用you can reduce the use of
grep
andhead
不要查看所有适配器,只查看您想要的适配器。
Don't look at all of the adapters, just the one you want.
如果
ifconfig
或ip
的输出发生变化,您的程序将会崩溃。为什么不直接使用
SIOCGIFCONF
ioctl()
并直接从源代码获取它呢?ETA:假设任何给定系统只有一个环回接口和一个具有单一地址的以太网也是不安全的。
If the output of
ifconfig
orip
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.
“ifconfig eth0”怎么样?
How about 'ifconfig eth0'?
主机名-I | awk '{print $1}'
hostname -I | awk '{print $1}'