|
|
Subscribe / Log in / New account

Just outout an IP address of an interface?

Just outout an IP address of an interface?

Posted Jan 13, 2017 22:54 UTC (Fri) by Cyberax (✭ supporter ✭, #52523)
In reply to: Just outout an IP address of an interface? by zlynx
Parent article: Moving on from net-tools

Seriously? How do I do it in bash?


to post comments

Just output an IP address of an interface?

Posted Jan 13, 2017 23:17 UTC (Fri) by zlynx (guest, #2285) [Link] (4 responses)

I hadn't seen that the question linked was specifically about bash, until later. I added a new answer to that question because of it. Asking a question about "programmatically" and then it's really about bash is an oxymoron. To do anything in bash requires stitching together command-line programs, searching and formatting their output.

And seriously, someone wanted JSON output? JSON is almost impossible to deal with in bash. The only tool I've seen that makes it possible is "jq", and I had to build that manually since Fedora didn't ship it. Although I see it is in the repos for F25 so they finally got it.

So processing the text output of ifconfig or ip is really the bash way to get it done. That's how everything gets done in bash.

Just output an IP address of an interface?

Posted Jan 13, 2017 23:20 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link] (3 responses)

How about Python or Ruby? Or maybe Java and C# where IOCTLs are a pain?

JSON is extremely easy to parse and tools to do it are now built-in into various languages. On the other hand, calling the kernel and then parsing complicated structures is often fraught with problems.

Just output an IP address of an interface?

Posted Jan 13, 2017 23:59 UTC (Fri) by zlynx (guest, #2285) [Link] (2 responses)

Found this for Python:
http://stackoverflow.com/questions/166506/finding-local-i...

And this, which is more modern:
https://pypi.python.org/pypi/pyroute2/
https://github.com/svinota/pyroute2

I'm not going to look up Ruby, but I know Perl has similar modules, because I used one in the past (2005'ish)

Just output an IP address of an interface?

Posted Jan 14, 2017 0:06 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link] (1 responses)

You might notice that the built-in library doesn't have a good way to get the information about all the IP addresses. Other libraries depend on libraries that need a working C compiler to install. And you might need more info from the interface, like MAC info or the routing database.

And anyway, why would you make a Linux tool that is user-hostile to scripting users? I understand that people might not have been thinking about it 10 years ago, but defending this like it's a good thing - that's just stupidity.

Just output an IP address of an interface?

Posted Jan 14, 2017 21:09 UTC (Sat) by zlynx (guest, #2285) [Link]

I'm not defending anything. It's just the way it is. Unless you have a time machine you can't make the commonly installed ifconfig and ip tools do things the way you want them done.

Just outout an IP address of an interface?

Posted Jan 14, 2017 4:54 UTC (Sat) by lsl (subscriber, #86508) [Link]

> Seriously? How do I do it in bash?

You use ifdata(1). Moreutils are quite useful.

https://joeyh.name/code/moreutils/


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds