Linux One Liner返回eth名称,ip,mac和子网

时间:2017-06-05 17:42:11

标签: linux bash awk

我一直在尝试将两个查询合并为一个。我正在尝试获取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

Top提供大部分信息,bottom提供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

2 个答案:

答案 0 :(得分:0)

尝试使用egrep而不是grep

some message

答案 1 :(得分:0)

这不是原始的100%准确,但它适用于您的输入:

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

如果你想在一行输出, 那么你可以像{i}这样在echo $(...)包装上面的命令:

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