2 Pluspunkte 0 Minuspunkte

Ich habe dieses Linux Script zum Abfragen einer API mit Curl.

path="$1"
curl -k -L "https://<fw-ip>:4444/api/$path" \
-H "Authorization: Basic xxxxxxxxxx"

Das Ergebnis der Abfrage ist immer ein JSON Objekt wie z.B

[
  {
    "_locked": "",
    "_ref": "REF_IntEthPresentati",
    "_type": "interface/ethernet",
    "additional_addresses": [],
    "bandwidth": 0,
    "comment": "",
    "inbandwidth": 0,
    "itfhw": "REF_ItfEthEth6IntelCorpo",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "presentation",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPri1722130124",
    "proxyarp": false,
    "proxyndp": false,
    "status": false
  },
  {
    "_locked": "",
    "_ref": "REF_IntEthLogeaudio",
    "_type": "interface/ethernet",
    "additional_addresses": [],
    "bandwidth": 0,
    "comment": "Sonos Boxen Loge",
    "inbandwidth": 0,
    "itfhw": "REF_ItfAweLogeaudio",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "Loge_Audio",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPri1721629124",
    "proxyarp": false,
    "proxyndp": false,
    "status": true
  },
  {
    "_locked": "",
    "_ref": "REF_IntEthScantest",
    "_type": "interface/ethernet",
    "additional_addresses": [],
    "bandwidth": 0,
    "comment": "ScanTest",
    "inbandwidth": 0,
    "itfhw": "",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "ScanTest",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPri109998124",
    "proxyarp": false,
    "proxyndp": false,
    "status": false
  },
  {
    "_locked": "",
    "_ref": "REF_IntEthResponsive",
    "_type": "interface/ethernet",
    "additional_addresses": [],
    "bandwidth": 0,
    "comment": "",
    "inbandwidth": 0,
    "itfhw": "",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "ResponsiveTest",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPri109999124",
    "proxyarp": false,
    "proxyndp": false,
    "status": false
  },
  {
    "_locked": "",
    "_ref": "REF_IntEthTest",
    "_type": "interface/ethernet",
    "additional_addresses": [
      "REF_ItfSecOldinterna"
    ],
    "bandwidth": 0,
    "comment": "",
    "inbandwidth": 0,
    "itfhw": "REF_ItfEthEth5IntelCorpo",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "Fortinet",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPri1722302512",
    "proxyarp": false,
    "proxyndp": false,
    "status": true
  },
  {
    "_locked": "",
    "_ref": "REF_IntEthWirelGuestNetwo",
    "_type": "interface/ethernet",
    "additional_addresses": [],
    "bandwidth": 0,
    "comment": "Auto-created by Wireless Protection",
    "inbandwidth": 0,
    "itfhw": "REF_ItfAweWlan0WirelNetwo",
    "link": true,
    "mtu": 1500,
    "mtu_auto_discovery": true,
    "name": "Wireless Guest Network",
    "outbandwidth": 0,
    "primary_address": "REF_ItfPriWirelGuestNetwo",
    "proxyarp": false,
    "proxyndp": false,
    "status": true
  }
]   

Gibt es eine Möglichkeit in Linux bestimmte Werte davon anzuzeigen? Ich möchte z.B von jedem Objekt im Array (jedes Interface) den Namen (name) und die Referenz auf die IP (primary_address) anzeigen. So wie in diesem Pseudo Code?

sh api.sh objects/interface/ethernet | foreach | select name, primary_address
von  

2 Antworten

0 Pluspunkte 0 Minuspunkte

Du kannst das Tool jq installieren.

sudo apt install jq

Danach kannst du die Ausgabe damit parsen.

sh api.sh objects/interface/ethernet | jq -r '.[] | "\(.name) - \(.primary_address)"'

Erklärung

  • .[] -> Durchläuft jedes Interface-Objekt.
  • "\(.name) - \(.primary_address)" -> Gibt den Namen und die primäre Adresse in einer formatieren Zeichenkette aus.
von (1.0k Punkte)  
0 Pluspunkte 0 Minuspunkte

Du kannst die Ausgabe mit grep filtern.

sh api.sh objects/interface/ethernet | grep -Po '"(name|primary_address)":.*?[^\\]",'
von (728 Punkte)