gpt4 book ai didi

Linux One Liner 返回 eth 名称、ip、ma​​c 和子网

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:47 24 4
gpt4 key购买 nike

我一直在尝试将两个查询合并为一个。我正在尝试获取 IP 信息的输出,该信息将与针对主机文件的另一个查询进行比较,并最终转储到数据库中。由于它们的部署方式,我的命令必须是简单的一行。我可以用这两个得到我需要的东西,但是有没有办法将它们结合起来。 ip show 输出了多行输出,但不确定如何组合这些行。

ip -o addr show | awk '/inet/ {print $9, $4}' | grep -v '127.0.0.1'
ip -o addr show | awk '/link/ {print $2, $13}' | grep -v lo

顶部提供大部分信息,底部提供 mac。这同时给出了两者,但它是多行的,所以不确定如何计算出我需要的值。有什么好的想法或技巧吗?在 linux/bash 上仍然是新的。

 ip -o addr show | grep -i 'ether\|inet'

ip -o addr show 命令示例

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN \            link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
1: lo inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
2: eth0 inet x.x.x.x/24 brd x.x.x.x scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: eth1 inet x.x.x.x/24 brd x.x.x.x scope global eth1
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
4: eth2 inet x.x.x.x/27 brd x.x.x.x scope global eth2
5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
6: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
6: eth4 inet x.x.x.x/24 brd x.x.x.x scope global eth4
7: eth5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
7: eth5 inet x.x.x.x/24 brd x.x.x.x scope global eth5
8: eth6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
8: eth6 inet x.x.x.x/28 brd x.x.x.x scope global eth6
9: eth7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000\ link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

最佳答案

这不是 100% 和原来一样准确,但它适用于您的输入:

ip -o addr show | awk '/inet/ && !/127.0.0.1/ {print $9, $4} /link/ && ! /lo/ {print $2, $13}'

如果你想在一行上输出,然后你可以将上面的命令包装在 echo $(...) 中,如下所示:

echo $(ip -o addr show | awk '/inet/ && !/127.0.0.1/ {print $9, $4} /link/ && ! /lo/ {print $2, $13}')

关于Linux One Liner 返回 eth 名称、ip、ma​​c 和子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com