linux-dash icon indicating copy to clipboard operation
linux-dash copied to clipboard

ip_addresses.sh : malformed output due to localization and other problems

Open zeus86 opened this issue 9 years ago • 8 comments

while wondering why the "ip-address"-widget produced only a "loading" content for infinite time, i tried to debug the script, which was responsible for that.

machine is ubuntu 16.04, x86_64, german localization, ran with included node-webserver.

manually running "server/modules/shell_files/ip_addresses.sh" produced no output, partially caused by the localized output of "ifconfig":

$: ifconfig eth0
eth0      Link encap:Ethernet  Hardware Adresse 00:12:34:56:78:90 
          inet Adresse:10.0.0.100  Bcast:10.0.0.255  Maske:255.255.252.0
          inet6-Adresse: fe80::cafe:1234/64 Gültigkeitsbereich:Verbindung
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX-Pakete:7031885 Fehler:0 Verloren:7 Überläufe:0 Fenster:0
          TX-Pakete:3151855 Fehler:0 Verloren:0 Überläufe:0 Träger:0
          Kollisionen:0 Sendewarteschlangenlänge:1000 
          RX-Bytes:7292861134 (7.2 GB)  TX-Bytes:525524429 (525.5 MB)
          Interrupt:20 Speicher:f2400000-f2420000 

in general I think all commands, which are later parsed by regex-interpreters shall be ecexuted with a leading LANG=c . Also the "END"-statement of awk produced broken output. Removing it fixed this. The Perl-Style regex for grep failed because of the (?=:)replacing it with (?=\ ) helped here.

While comparing the output then to the output of the "ping"-script offered, that the output-format (e.G. on which positions are quotemarks or spaces) slightly differs on some spots, so i adjusted the script here and there to produce exactly the same kind of output.

Comparison:

./server/modules/shell_files/ip_addresses.sh 
[{"interface": "eth0", "ip": 10.0.0.100 },{"interface": "lo", "ip": 127.0.0.1 },{"interface": "external", "ip": 1.2.3.4 }]
./server/modules/shell_files/ping.sh 
[{"host": "google.com", "ping": 13.868 },{"host": "yahoo.com", "ping": 121.863 },{"host": "twitter.com", "ping": 108.083 }]

The script now looks like this:

#!/bin/bash
awkCmd=`which awk`
grepCmd=`which grep`
sedCmd=`which sed`
ifconfigCmd=`which ifconfig`
trCmd=`which tr`
digCmd=`which dig`

externalIp=`$digCmd +short myip.opendns.com @resolver1.opendns.com`

echo -n "["

for item in $($ifconfigCmd | $grepCmd -oP "^[a-zA-Z0-9:]*(?=\ )")

do 
    echo -n "{\"interface\": \""$item"\", \"ip\": $( LANG=c $ifconfigCmd $item | $grepCmd "inet" | $awkCmd '{match($0,"inet (addr:)?([0-9.]*)",a)}{ if (NR != 0){print a[2]; exit}{print "none"}}') },"
done

echo "{\"interface\": \"external\", \"ip\": $externalIp }]"

Now the Output of the script seems to be correct, but the widget still shows only a loading screen. Some advice on this would be very appreciated. Thanks ^^

zeus86 avatar Aug 18 '16 12:08 zeus86

PS: even when only echoing the external ip and the leading bracket the widget does not load any content.

zeus86 avatar Aug 18 '16 12:08 zeus86

I will investigate further this weekend. Meanwhile, have you tried checking the browser dev console's network tab to see if the response comes through? It would help to identify if the layer that is causing the loading screen.

tariqbuilds avatar Aug 18 '16 22:08 tariqbuilds

in chrome (linux, 51.0.2704.106 64 bit) dev-console the regarding frame looks correct so far, and is loaded at the same time as other scripts are:

{moduleName: "ip_addresses",…}
moduleName: "ip_addresses"
output: "[,{"interface": "eth0", "ip": 10.0.0.100 },,{"interface": "lo", "ip": 127.0.0.1 },{"interface": "external", "ip": 1.2.3.4 }]↵"

interesting side note: the output seen from the browser differs from the output of the script (a comma before the first { , two commas separating the eth0 and lo-brackets). don't know if this is intended, but i assume not, because e.g. the ping-script has the same format of output in the browser as from the script.

a possible reason for a part of this problem i can think of is an interface without an ip address, which produces an entry and a comma but not any "content" (in this case the wlan0-interface is turned off by a radio-kill-switch and has therefore no ip, but still persists as interface...).

zeus86 avatar Aug 19 '16 13:08 zeus86

@zeus86 just one note, with gawk (to the shell script) the result is better.

gklipel avatar Nov 02 '16 17:11 gklipel

I got this problem too. I'm inexperience in linux's shell scripts. After a couple day try and edit, finally fix it with this:

#!/bin/bash
awkCmd=`which awk`
grepCmd=`which grep`
sedCmd=`which sed`
ifconfigCmd=`which ifconfig`
trCmd=`which tr`
digCmd=`which dig`

externalIp=`$digCmd +short myip.opendns.com @resolver1.opendns.com`

echo -n "["

for item in $($ifconfigCmd | $grepCmd -oP "^[a-zA-Z0-9:]*(?=\ )")
do 
    echo -n "{\"interface\": \""$item"\", \"ip\": \"$( $ifconfigCmd $item | $grepCmd -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | $grepCmd -Eo '([0-9]*\.){3}[0-9]*' | $grepCmd -v '0.0.0.0')\" }, "
done

echo "{\"interface\": \"external\", \"ip\": \"$externalIp\" }]"

vanbwodonk avatar Nov 18 '16 09:11 vanbwodonk

Hi, using php and chrome i got an issue. Chrome indeed raise an error due to the backslashes presents in the json. I had to workaround this problem in the following way.

echo stripslashes(shell_exec( $shell_file . " " . $module ));

ghost avatar May 19 '17 02:05 ghost

I have the OP problem (Raspi 4, Raspian buster), and the proposed solution does not work for me either; plus, all of this looks quite complicated (my awk/regex skills are weak). So I came up with this which is a lot simpler, works on the Raspi, and puts out v4 and v6 adresses:

ip_addresses() {

local ifconfigCmd=$(type -P ifconfig) local digCmd=$(type -P dig) externalIp=$($digCmd +short myip.opendns.com @resolver1.opendns.com)

for item in $($ifconfigCmd | $GREP -oP "^[a-zA-Z0-9:]*(?=:)") do result=$result"{"interface" : ""$item"", "ip" : "$($ifconfigCmd $item | $GREP -E "inet" | $AWK -v OFS="\n" '{ print $2 }')" }, " done

result=$result"{ "interface": "external", "ip": "$externalIp"} "

$ECHO [ ${result%?} ] | _parseAndPrint }

AnotherDaniel avatar Mar 27 '20 13:03 AnotherDaniel

@AnotherDaniel Hey guy, I got the same issue on my Raspi 3 , Raspbian buster. but I followed your code , and now I got the module "Ip Addresse" into endless waiting .....

could you check the code ?

bao3 avatar May 01 '20 21:05 bao3