Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Scripting problem extracting IP address

Sat Jun 16, 2018 5:29 pm

I have a script that extracts the IP address and MAC of eth0 and wlan0 and sends this to my website.
I am putting this script on every RPi box I build so I will get a report of its IP address so I can connect to it when it is moved between networks.
This has worked very well for several years on a half dozen RPi boxes.

But now I have run into a problem on one of my Pi units that does not extract the IP address at all.
The code in my script to extract say wlan0 address is like this:

Code: Select all

IPAddrWiFi=$(ifconfig wlan0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//')
On that Pi the ifconfig command returns this:

Code: Select all

 ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr b8:27:eb:77:59:16
          inet addr:192.168.117.174  Bcast:192.168.117.255  Mask:255.255.255.0
          inet6 addr: fe80::a1fd:8a00:a333:b6e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8234 errors:0 dropped:0 overruns:0 frame:0
          TX packets:71 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1191636 (1.1 MiB)  TX bytes:10935 (10.6 KiB)
But when I run ifconfig on the box that fails it returns this:

Code: Select all

ifconfig wlan0
wlan0: flags=-28605<UP,BROADCAST,RUNNING,MULTICAST,DYNAMIC>  mtu 1500
        inet 192.168.117.170  netmask 255.255.255.0  broadcast 192.168.117.255
        ether 74:da:38:65:1c:91  txqueuelen 1000  (Ethernet)
        RX packets 14755  bytes 4079739 (3.8 MiB)
        RX errors 0  dropped 7  overruns 0  frame 0
        TX packets 8613  bytes 1520375 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
So what I think fails is the command:

Code: Select all

ifconfig wlan0|grep "inet addr"|sed 's/ *inet addr://;s/ .*//'
I tried changing it to:

Code: Select all

ifconfig wlan0|grep "inet"|sed 's/ *inet//;s/ .*//'
But it did not help at all...

Question
(from one who is a complete Dummy concerning sed usage):
How should the command be modified to actually return the IP address

The RPi box in question runs OSMC but earlier it worked the same as on the ones running Raspbian. Following a recent OSMC update it no longer sends the IP address.
Bo Berglund
Sweden

User avatar
DougieLawson
Posts: 40125
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 5:44 pm

Try

Code: Select all

IPAddrWiFi=$(ip addr show | awk  '/inet .*global/ {print $2})'
That should return "192.168.117.174/24"
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
rpdom
Posts: 17699
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 5:48 pm

This works for me

Code: Select all

IPAddrWiFi=$(ifconfig wlan0|sed -n "s/^ *inet [^0-9.]*\([0-9.]*\) .*$/\1/p")
Last edited by rpdom on Sat Jun 16, 2018 6:35 pm, edited 1 time in total.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 6:25 pm

I get this reply when running on both Raspbian and OSMC:

Code: Select all

./ipreport.sh: line 17: unexpected EOF while looking for matching `)'
The script contains this on that line:

Code: Select all

IPAddrWiFi=$(ifconfig wlan0|sed -n "s/^ *inet [^0-9.]*\([0-9.]*\) .*$/\1/p"
EDIT:
If I add a trailing closing ) at the end of the line there are no errors but the IP address is empty.
Bo Berglund
Sweden

User avatar
rpdom
Posts: 17699
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 6:36 pm

Sorry for missing the closing ), I've edited that post now. Hmm, it should work, unless I've got some typo in it. I'll have another look. All I can think of is that the spaces before the "inet " line are a tab on your OS version. A hex dump of part of the output of ifconfig should give me something to test against, if you could post the output of this command please

Code: Select all

ifconfig wlan0 | grep -m 1 inet | hexdump -C

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 8:41 pm

When I add the trailing ) on the Raspbian version of the script I get this output when echoing the WiFi IP address:

Code: Select all

WiFi addr:   192.168.117.174
Trying the hexdump On OSMC:

Code: Select all

ifconfig wlan0 | grep -m 1 inet | hexdump -C
-bash: hexdump: command not found
I could not install hexdump via apt-get on OSMC:

Code: Select all

E: Unable to locate package hexdump
But on Raspbian:

Code: Select all

ifconfig wlan0 | grep -m 1 inet | hexdump -C
00000000  20 20 20 20 20 20 20 20  20 20 69 6e 65 74 20 61  |          inet a|
00000010  64 64 72 3a 31 39 32 2e  31 36 38 2e 31 31 37 2e  |ddr:192.168.117.|
00000020  31 37 34 20 20 42 63 61  73 74 3a 31 39 32 2e 31  |174  Bcast:192.1|
00000030  36 38 2e 31 31 37 2e 32  35 35 20 20 4d 61 73 6b  |68.117.255  Mask|
00000040  3a 32 35 35 2e 32 35 35  2e 32 35 35 2e 30 0a     |:255.255.255.0.|
0000004f
Seems like this is an OSMC issue, but even so since the information is present using ifconfig it should be possible to extract the IP address in a script also on OSMC. That part should not be OS specific...

Code: Select all

ifconfig wlan0 | grep "inet"
        inet 192.168.117.170  netmask 255.255.255.0  broadcast 192.168.117.255
But as I said I am pretty illiterate in using the stream editors like sed, awk etc...
Bo Berglund
Sweden

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sat Jun 16, 2018 11:02 pm

Avoid sed - using sed is for masochists.

Just do this:

Code: Select all

addr=$(ifconfig wlan0 | awk 'sub(/.*inet addr:/,"") { print $1 }')
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
Greg Erskine
Posts: 162
Joined: Sat Sep 15, 2012 4:20 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 12:28 am

As the output of the ifconfig command is different I would be inclined to follow Dougie Lawson's advice and use a "ip" instead.

But... this might work in both cases?? I can't test it.

Code: Select all

$ ifconfig eth0 | grep inet | sed 's/addr://' | awk '{ print $2 }'
* Raspberry Pi is a trademark of the Raspberry Pi Foundation

User avatar
Greg Erskine
Posts: 162
Joined: Sat Sep 15, 2012 4:20 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 1:05 am

Ooops typo. wlan0 not eth0. Sorry. :oops:
* Raspberry Pi is a trademark of the Raspberry Pi Foundation

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 5:57 am

Thanks, this worked as used in my script:

Code: Select all

IPAddrWiFi=$(ifconfig wlan0 | grep inet | sed 's/addr://' | awk '{ print $2 }')
However this also worked:

Code: Select all

IPAddrWiFi=$(ifconfig wlan0|sed -n "s/^ *inet [^0-9.]*\([0-9.]*\) .*$/\1/p")
What happened when I tested the last line above was that I had a typo in the script echo command that displayed the result so that nothing was shown.

But now I found another problem:
On OSMC:

Code: Select all

ifconfig wlan0 | grep inet | sed 's/addr://' | awk '{ print $2 }'
192.168.117.170
But on Raspbian Stretch:

Code: Select all

ifconfig wlan0 | grep inet | sed 's/addr://' | awk '{ print $2 }'
192.168.117.174
fe80::a1fd:8a00:a333:b6e/64
So there is an extra line output with what appears as IPv6, which I don't want, when using the first command.
I am going to put the second version into my script now since it produces the same result on both systems.
Bo Berglund
Sweden

User avatar
DougieLawson
Posts: 40125
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 8:30 am

The extra line is because your "inet" regex matches both "inet" and "inet6".

You may want to include a space to elimininate the match against "inet6".
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 8:39 am

Thanks, that fixed it!
Bo Berglund
Sweden

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 8:48 am

I still don't understand why you are using grep, sed, and awk, when a single awk woudl suffice.

Please enlighten me.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
rpdom
Posts: 17699
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 10:08 am

n67 wrote:
Sun Jun 17, 2018 8:48 am
I still don't understand why you are using grep, sed, and awk, when a single awk woudl suffice.

Please enlighten me.
Because you didn't read the problem fully and your solution wouldn't work.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 10:40 am

rpdom wrote:
Sun Jun 17, 2018 10:08 am
n67 wrote:
Sun Jun 17, 2018 8:48 am
I still don't understand why you are using grep, sed, and awk, when a single awk woudl suffice.

Please enlighten me.
Because you didn't read the problem fully and your solution wouldn't work.
No.

Even if I did get the details wrong, which is possible, it is still better/easier to do it with a single AWK (which might not be exactly what I posted - I don't really care about the exact details of the problem), then to do it with grep/sed/awk.

That stands regardless of the exact details.

BTW, this is the key problem with the so-called "Unix Way" - the idea of taking the output of utilities and piping it through other utilities. The problem being that the output of most (not all) commands is not standardized and subtle changes in the output can throw things off. What I am getting at with all this is that if you really wanted to do this problem right, you'd find a way to get the information that didn't depend on the vagaries of either the "ifconfig" or "ip" commands (which could - and will - change at any time). I'm pretty sure that there is some way to get it via one or more of the "pseudo-filesystems" (/proc, /sys, etc), although I haven't found it yet. That's gotta be better than parsing the output of "ifconfig" or "ip". There's probably also a way to get it via some Python-callable library, but again, I haven't researched this.

Anyway, I'm not convinced that my solution (or some subtle re-working - again, I don't care about the exact details) doesn't work. You know how these things go - it is far more likely that OP is doing something unexpected than that there actually are multiple versions of the "ifconfig" command floating around out there.

Anyway, I'm done with this. OP is free to use my version or not. No skin off my nose either way.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
rpdom
Posts: 17699
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 10:46 am

n67 wrote:
Sun Jun 17, 2018 10:40 am
Even if I did get the details wrong, which is possible, it is still better/easier to do it with a single AWK (which might not be exactly what I posted - I don't really care about the exact details of the problem), then to do it with grep/sed/awk.
I never suggested grep/sed/awk. Mine was a simple one operation sed. It works. Maybe you can do it with awk, which tbh I've never found very friendly to use. But enough of this. The OP has a solution. That is what is important.

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 10:57 am

rpdom wrote:
Sun Jun 17, 2018 10:46 am
n67 wrote:
Sun Jun 17, 2018 10:40 am
Even if I did get the details wrong, which is possible, it is still better/easier to do it with a single AWK (which might not be exactly what I posted - I don't really care about the exact details of the problem), then to do it with grep/sed/awk.
I never suggested grep/sed/awk. Mine was a simple one operation sed.
No worries. I never said that.

However, OP seemed to be going with a g/s/a solution:
But on Raspbian Stretch:
Code: Select all

ifconfig wlan0 | grep inet | sed 's/addr://' | awk '{ print $2 }'
192.168.117.174
fe80::a1fd:8a00:a333:b6e/64
So there is an extra line output with what appears as IPv6, which I don't want, when using the first command.
I was just trying to help him by pointing out that it can be done more simply with a single AWK.

BTW, sorry to hear about your unpleasant experiences with AWK. Really a shame, since I find it to be far and away the most useful Unix tool for text processing. The right balance of high-level and low-level.

I also use it for systems/tool development.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 2:20 pm

Gosh, stirred up some discussion...
Thanks to all participants!
My problem is solved and I don't need any efficiency improvement, after all it runs once 60 seconds after boot...

Summary:
What I discovered when I fired up the OSMC box at my summer home was that the IP address was not part of the email I waited for.
(The script sends the data via curl to a PHP file on my website, which formats an email to myself with the RPi boot info)
This had worked before so something changed after an OSMC system upgrade changed the output of ifconfig.

Then I screwed up because did not know how to execute the commands on the command prompt so I edited the script and ran it.
Stupidly I made an error right there so the wrong variable was echo-ed (with the blank result).

Now I can confirm that the following works fine in OSMC:

Code: Select all

ifconfig wlan0 | sed -n "s/^ *inet [^0-9.]*\([0-9.]*\) .*$/\1/p"
192.168.117.170

ifconfig wlan0 | grep 'inet ' | sed 's/addr://' | awk '{ print $2 }'
192.168.117.170
(the extra space in grep on command 2 is to block IPv6 from the output)

But this does not return anything useful:

Code: Select all

ifconfig wlan0 | awk 'sub(/.*inet addr:/,"") { print $1 }'

ip addr show | awk  '/inet .*global/ {print $2}
>
Regarding operating system peculiarities, I encountered that as well when I installed Raspbian Stretch some time ago and found that eth0 was non-existing!
Had to google a lot to find out how to restore the normal interface name and then my report script worked again.
Bo Berglund
Sweden

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 2:27 pm

Yeah, yeah, yeah, I know, you solved your problem so you don't get care. Yes, OK.

But. This:
ifconfig wlan0 | awk 'sub(/.*inet addr:/,"") { print $1 }'
absolutely will work. Given your commentary, I have to assume there was some kind of transcription error.

What exactly do you get as output when you run that command?

What exactly do you get if you run:

Code: Select all

ifconfig wlan0 | grep "inet addr:"
What do you get if you run:

Code: Select all

awk -W version
By the way, "Nothing" is probably not a correct answer to any of these questions.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 3:04 pm

This is the exact replies to your test commands as copied from the terminal ("nothing" describes it well):

Code: Select all

osmc@rpi2-osmc:~$ ifconfig wlan0 | awk 'sub(/.*inet addr:/,"") { print $1 }'
osmc@rpi2-osmc:~$ ifconfig wlan0 | grep "inet addr:"
osmc@rpi2-osmc:~$ awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits:
max NF             32767
sprintf buffer      1020
osmc@rpi2-osmc:~$
Regarding ifconfig, this is what I get without piping it anywhere:

Code: Select all

osmc@rpi2-osmc:~$ ifconfig wlan0
wlan0: flags=-28605<UP,BROADCAST,RUNNING,MULTICAST,DYNAMIC>  mtu 1500
        inet 192.168.117.170  netmask 255.255.255.0  broadcast 192.168.117.255
        ether 74:da:38:65:1c:91  txqueuelen 1000  (Ethernet)
        RX packets 106783  bytes 25211111 (24.0 MiB)
        RX errors 0  dropped 18  overruns 0  frame 0
        TX packets 38790  bytes 7238422 (6.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
Please notice (as I stated above) that ifconfig output differs from Raspbian in that on Raspbian Stretch it looks like this:

Code: Select all

pi@rpi3-aspo:~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr b8:27:eb:77:59:16
          inet addr:192.168.117.174  Bcast:192.168.117.255  Mask:255.255.255.0
          inet6 addr: fe80::a1fd:8a00:a333:b6e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:38087 errors:0 dropped:1 overruns:0 frame:0
          TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4304408 (4.1 MiB)  TX bytes:9462 (9.2 KiB)
So the IP address is on a line starting with "inet addr" on Raspbian but with "inet" on OSMC.
By modifying your awk command as follows it seems to also work:

Code: Select all

osmc@rpi2-osmc:~$ ifconfig wlan0 | awk 'sub(/.*inet /,"") { print $1 }'
192.168.117.170
Bo Berglund
Sweden

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 3:43 pm

osmc@rpi2-osmc:~$ ifconfig wlan0 | awk 'sub(/.*inet /,"") { print $1 }'
Yeah, that'll do it. You just need something to distinguish the ipv4 line from any possible ipv6 lines.

Did we ever determine *why* the OSMC version of ifconfig is different from the Raspbian version?

(As I indicated earlier, there is no standard for this, so either/any output is "correct" [from a standards point of view])

I suppose the next exercise would be to devise a reg exp that works in either case (i.e., with either version of ifconfig).

Or you could switch to "ip", which is probably less likely to have inconsistent versions, but has its own set of problems.

Thanks for following up on this!

P.S. This seems to work (on Raspbian):

Code: Select all

ip addr show dev wlan0 | awk -F/ 'sub(/ +inet +/,"") { print $1 }'
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
rpdom
Posts: 17699
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 3:58 pm

n67 wrote:
Sun Jun 17, 2018 2:27 pm
Yeah, yeah, yeah, I know, you solved your problem so you don't get care. Yes, OK.

But. This:
ifconfig wlan0 | awk 'sub(/.*inet addr:/,"") { print $1 }'
absolutely will work. Given your commentary, I have to assume there was some kind of transcription error.
Again, you didn't read the original.

The difference is that ifconfig used to return a line like

Code: Select all

        inet addr:192.168.1.30  Bcast:192.168.1.255  Mask:255.255.255.0
but now it returns

Code: Select all

        inet 192.168.1.30  netmask 255.255.255.0  broadcast 192.168.1.255
Note the lack of the "addr:" bit.

Ernst
Posts: 1350
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 4:02 pm

Bosse_B wrote:
Sun Jun 17, 2018 3:04 pm

Please notice (as I stated above) that ifconfig output differs from Raspbian in that on Raspbian Stretch it looks like this:

Code: Select all

pi@rpi3-aspo:~ $ ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr b8:27:eb:77:59:16
          inet addr:192.168.117.174  Bcast:192.168.117.255  Mask:255.255.255.0
          inet6 addr: fe80::a1fd:8a00:a333:b6e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:38087 errors:0 dropped:1 overruns:0 frame:0
          TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4304408 (4.1 MiB)  TX bytes:9462 (9.2 KiB)
What version of Raspbian are you using ? According your post rpi3-aspo is on Stretch, when I look at my Stretch systems I get a different format, like on rpi2-osmc, meaning without "inet addr:"

Code: Select all

pi@raspi2:~ $ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.249  netmask 255.255.255.0  broadcast 192.168.0.255
        ether b8:27:eb:d1:0c:99  txqueuelen 1000  (Ethernet)
        RX packets 2192570  bytes 1107834624 (1.0 GiB)
        RX errors 0  dropped 2032  overruns 0  frame 0
        TX packets 1412057  bytes 1158297823 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
The road to insanity is paved with static ip addresses

Bosse_B
Posts: 1074
Joined: Thu Jan 30, 2014 9:53 am

Re: Scripting problem extracting IP address

Sun Jun 17, 2018 5:09 pm

My bad!
I thought that I had upgraded all of my RPi3 units to Stretch by now, but:

Code: Select all

cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
Now at my summer home and here I only have two Pi units, one OSMC and the other RPi3 apparently still on Jessie...
Bo Berglund
Sweden

videobuff
Posts: 9
Joined: Sun Feb 10, 2013 4:05 pm
Location: Hengelo - Netherlands
Contact: Website

Re: Scripting problem extracting IP address

Tue Oct 20, 2020 10:45 pm

I agree with the AWK advice. The orginal script however has to be adapted on a Raspberry 4 with Buster because of different ifconfig output.
Just the part where the IP adresses are looked up. the addr: is gone, hence......
Erik

Code: Select all


def get_my_ipwlan():
    val = run_cmd("/sbin/ifconfig wlan0 | grep 'inet ' | cut -d: -f2 | awk '{ print $2}'")[:-1]
    if val == "":
        val = "No connection!"
    return val

def get_my_ipeth():
    val = run_cmd("/sbin/ifconfig eth0 | grep 'inet ' | cut -d: -f2 | awk '{ print $2}'")[:-1]
    if val == "":
        val = "No connection"
    return val
    

Return to “Raspberry Pi OS”