kellyjonbrazil / jc

CLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
MIT License
7.9k stars 210 forks source link

Proposal for ''ip route'' Parser #424

Closed Julian5555 closed 1 year ago

Julian5555 commented 1 year ago

I built a parser for the Ip route command in Linux. Headers were gathered from the Ip route man pages. The schema I'm using is as follows:

`[ {

    "ip":        string,
    "via":       string,
    "dev":       string,
    "metric":    string,
    "proto":     string,
    "scope":     string,
    "src":       string,
    "via":       string,
    "status":    string
  }
]

` So, if the input file looked like this:

default via 10.0.2.2 dev enp0s3 proto dhcp metric 100 10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 metric 100 169.254.0.0/16 dev enp0s3 scope link metric 1000 172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown

The parser would output: [ {

"ip": "default",
"via": "10.0.2.2",
"dev": "enp0s3",
"proto": "dhcp",
"metric": "100"

}, {

"ip": "10.0.2.0/24",
"dev": "enp0s3",
"proto": "kernel",
"scope": "link",
"src": "10.0.2.15",
"metric": "100"

}, {

"ip": "169.254.0.0/16",
"dev": "enp0s3",
"scope": "link",
"metric": "1000"

}, {

"ip": "172.17.0.0/16",
"dev": "docker0",
"proto": "kernel",
"scope": "link",
"src": "172.17.0.1",
"status": "linkdown"

} ]

The status header was used to indicate the "linkdown" status and can be changed if needed.

kellyjonbrazil commented 1 year ago

Nice! One of the reasons I hadn't implemented a parser for ip route is because it looks like the newer versions support JSON output for that command. Have you been able to try that out or are you using an older version?

Julian5555 commented 1 year ago

I hadn't tried that out because the feature I added was intended to aid me in dealing with older files/systems so I don't have access to the newer JSON output.

kellyjonbrazil commented 1 year ago

Sounds good. You can contribute it with a Pull Request from the dev branch if you'd like. Then I'll review it an merge it in. Thanks!

Julian5555 commented 1 year ago

Hey, I noticed that the find, proc/net/tcp, and iproute parsers were all marked as ready to ship but hadn't been merged yet. I was just wondering if there was anything on our end that we needed to do before they could be merged?

kellyjonbrazil commented 1 year ago

Nope, all good. I may be able to get a new release out this weekend. I usually batch up a few updates into a release.

kellyjonbrazil commented 1 year ago

Added in v1.23.4