[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: get my ip address



On Sun 17 Feb 2019 at 12:51:34 (+0900), John Crawley wrote:
> On 16/02/2019 14.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)
> ---
> > > > (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
> 
> jq can pull out the exact element of the json that you want, making
> further shell parsing unnecessary.
> Totally unrelated example, (Adobe flash player downloads)
> this query:
> curl -s "https://get.adobe.com/flashplayer/webservices/json/?platform_type=Linux&platform_arch=x86-32&browser_dist=Chrome";
> | jq -r '.[0].download_url'
> returns:
> http://fpdownload.adobe.com/pub/flashplayer/pdc/32.0.0.142/flash_player_ppapi_linux.i386.tar.gz

It's a very different example from what the OP was talking about. But
I am interested to know where the [0] came from in your jq options.

It's also a very different example from what I use   ip -o   for,
which is to pick up the name of my own wired interface and the
ipv6 address of whatever machine is connected to the other end of
a cat5 cable. The results are used for the purpose, much disapproved
of by certain people here, of directly connecting two hosts and using
the ipv6 link to transfer files, thereby leaving both machines' ipv4
configuration completely undisturbed.

My script fragments are:

 Mywiredifname=$(ip -o link show | sed -e '/^[0-9]\+: [^e]/d;s/[0-9]\+: \([^:]\+\): .*/\1/;')

where sed throws away any interface names not starting with "e" and
then prints the rest. (All my machines nowadays have one wired port,
so multiple lines never occur.)

 ping6 -c 1 -W 1 ff02::1%"$Mywiredifname"
 Neighbour=$(ip -6 -o neighbour | sed -e 's/^\([^ ]\+\) .*/\1/;')

where sed throws away everything after the first space.
So now I have the ipv6 address of the interface at the other end,
and I can login or transfer files with a single bash function that
isn't aware of the hostnames at either end.

This ping, for example, checks whether the machine is still connected¹
at the other end (neighbour entries can be stale) before proceeding:

 ping6 -c 1 -W 1 "$Neighbour%$Mywiredifname" # and test $?

You're welcome to rewrite these fragments using json, and I shall
try them out.

¹ I have the odd cat5 connector where the locking tab has snapped off.

Cheers,
David.


Reply to: