从“inxi -ni”中提取信息作为键值对

从“inxi -ni”中提取信息作为键值对

团队,

我需要从“inxi -ni”中提取输出作为键值对。例如,如果我只需要“IP v4”

操作系统:Ubuntu V-20

在此处输入图片描述

答案1

您可以使用:

address=`inxi -ni | grep "IP v4" | cut -d' ' -f3`
echo "IPv4=$address"
  • 用于grep正则表达式匹配“IP v4”
  • 然后用来cut解析不受空间限制的字段 3。
  • 保存到$address变量
  • echo然后使用命令显示它

答案2

假设您有一个当前的 inxi,或者您更新了旧的 inxi,这个 -y1 输出选项是最近引入的(我认为是 3.1.03 或 04):

inxi -nizy1
Network:
  Device-1: Intel I211 Gigabit Network
    driver: igb
    IF: enp6s0
      state: up
      speed: 1000 Mbps
      duplex: full
      mac: <filter>
      IP v4: <filter>
        scope: global
      IP v6: <filter>
        scope: link
  IF-ID-1: vboxnet0
    state: down
    mac: <filter>
  WAN IP: <filter>

对于所有 3.x inxi:

inxi -niz --output json --output-file print
{"000#1#0#Network":[{"001#1#1#Device":"Intel I211 Gigabit Network","002#1#2#driver":"igb"},{"002#0#3#speed":"1000 Mbps","001#0#3#state":"up","003#0#3#duplex":"full","004#0#3#mac":"<filter>","000#1#2#IF":"enp6s0"},{"002#0#4#scope":"global","001#1#3#IP v4":"<filter>"},{"001#1#3#IP v6":"<filter>","002#0#4#scope":"link"},{"001#0#2#state":"down","002#0#2#mac":"<filter>","000#1#1#IF-ID":"vboxnet0"},{"000#0#1#WAN IP":"<filter>"}]}

答案3

您可以使用以下命令获取所选键的值:

inxi -ni --output json --output-file print | python3 -m json.tool | jq '.[] | . []' | sed 's/".*#/"/' | jq 'select(has("IP v4"))'

{
  "scope": "global",
  "IP v4": "192.168.1.91/24"
}
{
  "scope": "global",
  "IP v4": "172.17.0.1/16"
}
{
  "scope": "global",
  "IP v4": "192.168.192.119/24"
}

相关内容