ifconfig equivalent with json or xml display

We are doing some regression testing using selenium and part of our tests involves many interfaces tests.
Working with the standard ifconfig turns out to be quite painful because the output is not properly formatted.
Of course we could manage to pipe this into sed / awk -- but this is like an oldish approach and would force us to add more coding where simple parsing could easily be used.

So long story short: is there any tool which could allow us to parse interfaces status in a format like json or xml?

On linux one could use somethings like :
Code:
# ip --json address show
 
🤣

OP wants Windows or Mac.

Within Linux, there's usually some kind of desktop-specific utility to nicely display information on a network interface. KDE, GNOME, XFCE, and the like - they do offer a utility that monitors the status of a network interface.

But if you need to catch a specific status of a network interface - Sorry, I don't see a way around sed/awk or other ways of using UNIX utilities to play with basic text.

If you're willing to study it for just a bit, you'll discover that it only takes maybe a line of code, like:
Code:
ifconfig | grep netmask | cut -d/ -f4-5 > status_1.txt

if you run cat status_1.txt, you'll discover the value of the netmask. Depending on where status_1.txt lives, you can use JSON to pick it up later. Frankly, to me, JSON is a bit of a black box.

There's probably better ways of extracting specific strings into files that can be read by JSON and entered into a database later, but my point is, don't be afraid to RTFM... 😩
 
The following works on gentoo linux,
Code:
ifconfig | grep netmask | grep -v 127.0.0.1 | awk '{print $2}'
For freebsd it will be something similar.
 
To get the IP address for an interface (lo0 in demo below) in what should be a reliable (won't break if formatting of some other tool changes) way using xmllint(1) from textproc/libxml2:
Code:
$ netstat -4n -I lo0 --libxo xml | xmllint --xpath '//address/text()' -
127.0.0.1
 
Hey, let's turn this thread into a Perl Golf - style competition, and see which one-liner is:
  • POSIX compliant - can run on Linux or BSD
  • Does not require root to run
  • Makes use of stuff already in base of FreeBSD - sed, awk, cut, pipe/redirect output/input, etc.
  • Requires minimal tweaking to catch ANY string from ifconfig's output
  • Requires minimal tweaking to feed that string into a text file that OP's Selenium testing suite can read
  • uses textproc/py-smartypants or www/py-beautifulsoup
  • Comes with a good description of why it works correctly
:p
 
Hey, let's turn this thread into a Perl Golf - style competition, and see which one-liner is:
  • POSIX compliant - can run on Linux or BSD
  • Does not require root to run
  • Makes use of stuff already in base of FreeBSD - sed, awk, cut, pipe/redirect output/input, etc.
  • Requires minimal tweaking to catch ANY string from ifconfig's output
  • Requires minimal tweaking to feed that string into a text file that OP's Selenium testing suite can read
  • uses textproc/py-smartypants or www/py-beautifulsoup
  • Comes with a good description of why it works correctly
:p

Unfortunately both Linux and FreeBSD have moved away from ifconfig in different directions.
 
What I'm saying is that machine parseable output has been moved to netstat
If it's text that you can see on the console/terminal, it's machine parsable :p

And if it's hex-encoded, FreeBSD has a utility for reading that, as well.

Heck, I even saw an MOTD that had a tip on how to remove unprintable characters from a DOS file, it was a one-liner, as well :p
 
Back
Top