Web lists-archives.com

Re: get my ip address

On 16/02/2019 05:28, David Wright wrote:
> On Sat 16 Feb 2019 at 11:10:32 (+0900), John Crawley wrote:
>> On 16/02/2019 08.54, David Wright wrote:
>>> On Fri 15 Feb 2019 at 22:04:42 (+0000), Darac Marjal wrote:
>>>> If you're going to recommend parsing `ip`, the -j option may be more
>>>> amenable to scripting. (JSON output)
>>>> On 15/02/2019 15:52, David Wright wrote:
>>>>> On Fri 15 Feb 2019 at 12:02:20 (+0100), Markus Schönhaber wrote:
>>>>>> Tony, 15.2.2019, 11:11:29 +0100:
>>>>>>> Debian 9. I need to read my IPv6 address into a python script.
>>>                ↑
>>> Some of us here are still running stable (stretch) and older. So
>>> perhaps only for buster onwards and, I assume, stretch-backports.
>>> But …
>>>>> [That's probably best if your destination is a Python program. ←snipped]
>>>>> Otherwise, for scripting, it's easy to overlook   ip -o a
>>>>> which makes parsing much easier.
>>> (Sorry if I was expected to explicitly write "shell scripting".)
>>> … I don't think that JSON would be any help: rather, the opposite.
>> Though a call to jq makes parsing json very easy for shell scripts.
> I'm not sure I understand why you'd ask ip to write JSON, and then
> post-process it with jq to filter it to different JSON, and then
> parse it in a shell, rather than just parsing something as simple as
> the oneline format using the tools that every system has installed
> (like grep, sed).

Because JSON gives a standard way to markup data. Grep and sed are
great, no quibbles there, but they work with text streams. Structured
data is more reliable. With JSON, you don't need to know that the  ip
address is the fourth (or is it fifth?) space-separated token in the
twelfth line of the stream. You can actually ask jq "If the interface
name matches /eth\d/, what are the IP addresses for this interface?"
because it's all key:value based.

I don't want to say "don't use grep et al.", because they definitely
have their place. I'm just saying that the world moving toward
structured data brings a lot of robustness.

> That is, unless you're already involving jq for something else,
> like the OP is with their Python program. And as we've seen, Python
> has a module or function for just about everything, and may avoid
> having to call ip in the first place.
> Cheers,
> David.