ruebenramirez / blog

My blog
http://blog.ruebenramirez.com/
7 stars 0 forks source link

parse json on the command line with jq #349

Open ruebenramirez opened 7 years ago

ruebenramirez commented 7 years ago

I wanted to quickly grab the default gateway from the docker bridge device in order to reference the docker host. The docker command provides enough info to give us this back!

docker network inspect bridge

which gives us back a whole bunch of json:

[
    {
        "Name": "bridge",
        "Id": "3ad6f1a9f798529ca7e13ceac232abc44abc77e3bf9d3c0e793dea7b27334e68",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Containers": {
            "1a79cdc32162a143cf04673628f47348bc85e6b99fa9cc7982da366dd55f9638": {
                "Name": "dockerelk_kibana_1",
                "EndpointID": "ab89f5b46f499b551d9e1d573b811700356b4403e81f6e2dbc1cd8b769b3b830",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "4357f82254a49e283e641c932532123ce140e61d769487106ad33767c2832e03": {
                "Name": "dockerelk_elasticsearch_1",
                "EndpointID": "9def354b04485dfb7246456ddc7669c76699c39b55523e78d04dd34f743e5326",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "5d87af82ff2915500ef421091230c948f0f2e62d9d397474ab5add28e36e777e": {
                "Name": "snsv2_db_1",
                "EndpointID": "6d8d53678f50f67bed5597ea30ff1d888b1240a8cf4dd85766f85cf613082574",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "5fc230d1e7ecab1ed2064c7195e94f0939a554fabbe2ca4ed788719573e78146": {
                "Name": "snsv2_web_1",
                "EndpointID": "6417a29080081bdf8cccc899a0978892acf9d77b0cd8cf4c275ca31f171670d5",
                "MacAddress": "02:42:ac:11:00:05",
                "IPv4Address": "172.17.0.5/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

I found an excellent utility to parse json on the command line: https://stedolan.github.io/jq/. They even have a web shell to learn the jq syntax to grab elements from your own json: https://jqplay.org/

to dynamically source the gateway address from the docker bridge device we can use this jq query:

.[0]["IPAM"]["Config"][0]["Gateway"]

results in providing just the gateway IP value:

"172.17.0.1"
ruebenramirez commented 7 years ago

I suppose I could have also just grabbed the IP assigned to the docker0 interface via ifconfig + grep + awk ^2

/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

source: http://www.if-not-true-then-false.com/2010/linux-get-ip-address/

ruebenramirez commented 7 years ago

or using python:

curl -s http://www.telize.com/geoip/46.19.37.108 | python -c 'import sys, json; print json.load(sys.stdin)["country"]'

source: http://www.cambus.net/parsing-json-from-command-line-using-python/

definitely liking jq's portable c binary over importing packages to filter json via cli though.