La herramienta ip permite volcar la información relacionada a las direcciones IP de las interfaces de red de un sistema GNU/Linux en formato JSON, lo cual puede ser útil para enviar a un Web Service o cargar en una base de datos.

Para obtener la salida de la herramienta IP en formato JSON, simplemente se debe agregar la opción -j:

root@debian9:~# ip -j a
[{"ifindex":1,"ifname":"lo","flags":["LOOPBACK","UP","LOWER_UP"],"mtu":65536,"qdisc":"noqueue","operstate":"UNKNOWN","group":"default","txqlen":1000,"link_type":"loopback","address":"00:00:00:00:00:00","broadcast":"00:00:00:00:00:00","addr_info":[{"family":"inet","local":"127.0.0.1","prefixlen":8,"scope":"host","label":"lo","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":2,"ifname":"eth0","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"mq","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"00:11:dd:55:7a:02","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet","local":"192.168.94.45","prefixlen":22,"broadcast":"192.168.97.255","scope":"global","label":"eth0","valid_life_time":4294967295,"preferred_life_time":4294967295}]},{"ifindex":3,"ifname":"eth1","flags":["BROADCAST","MULTICAST","UP","LOWER_UP"],"mtu":1500,"qdisc":"mq","operstate":"UP","group":"default","txqlen":1000,"link_type":"ether","address":"00:a2:22:de:54:77","broadcast":"ff:ff:ff:ff:ff:ff","addr_info":[{"family":"inet","local":"10.54.136.164","prefixlen":24,"broadcast":"10.54.136.255","scope":"global","label":"eth1","valid_life_time":4294967295,"preferred_life_time":4294967295}]}]

Si se desea que tenga un formato más legible para el humano, es posible agregar la opción pretty (-p), la cual incorpora indentación en el código JSON resultante:

root@debian9:~# ip -j -p a
[ {
        "ifindex": 1,
        "ifname": "lo",
        "flags": [ "LOOPBACK","UP","LOWER_UP" ],
        "mtu": 65536,
        "qdisc": "noqueue",
        "operstate": "UNKNOWN",
        "group": "default",
        "txqlen": 1000,
        "link_type": "loopback",
        "address": "00:00:00:00:00:00",
        "broadcast": "00:00:00:00:00:00",
        "addr_info": [ {
                "family": "inet",
                "local": "127.0.0.1",
                "prefixlen": 8,
                "scope": "host",
                "label": "lo",
                "valid_life_time": 4294967295,
                "preferred_life_time": 4294967295
            } ]
    },{
        "ifindex": 2,
        "ifname": "eth0",
        "flags": [ "BROADCAST","MULTICAST","UP","LOWER_UP" ],
        "mtu": 1500,
        "qdisc": "mq",
        "operstate": "UP",
        "group": "default",
        "txqlen": 1000,
        "link_type": "ether",
        "address": "00:11:dd:55:7a:02",
        "broadcast": "ff:ff:ff:ff:ff:ff",
        "addr_info": [ {
                "family": "inet",
                "local": "192.168.94.45",
                "prefixlen": 22,
                "broadcast": "192.168.97.255",
                "scope": "global",
                "label": "eth0",
                "valid_life_time": 4294967295,
                "preferred_life_time": 4294967295
            } ]
    },{
        "ifindex": 3,
        "ifname": "eth1",
        "flags": [ "BROADCAST","MULTICAST","UP","LOWER_UP" ],
        "mtu": 1500,
        "qdisc": "mq",
        "operstate": "UP",
        "group": "default",
        "txqlen": 1000,
        "link_type": "ether",
        "address": "00:a2:22:de:54:77",
        "broadcast": "ff:ff:ff:ff:ff:ff",
        "addr_info": [ {
                "family": "inet",
                "local": "10.54.136.164",
                "prefixlen": 24,
                "broadcast": "10.54.136.255",
                "scope": "global",
                "label": "eth1",
                "valid_life_time": 4294967295,
                "preferred_life_time": 4294967295
            } ]
    } ]

Por supuesto, ambas opciones (-j y -p) pueden ser utilizadas para cualquier subcomando de la herramienta ip (como "address", "link", "route", etc.)

Para más información:

man ip


Tal vez pueda interesarte


Compartí este artículo