0

For a bash script I want to determine whether my device has Wi-Fi. The easiest way for me would be ifconfig and some regex. So I want to check if my interface en0 has a valid IP or not.

ifconfig en0 | grep inet | cut -d: -f2 | awk '{ print $2}'

gives me the IP. If I dont have one it doesn't return anyhting since the grep command returns nothing. How do I check for that in an if for example?

tzippy
  • 6,458
  • 30
  • 82
  • 151
  • How does this help with checking for the presence of WiFi? `en0` could be a cable connection. –  Jul 25 '12 at 12:07
  • Well that interface is the WiFi Interface. So if it has an IP, I definately am on WiFi. Plus, I dont have an Ethernet Interface (iPhone). – tzippy Jul 25 '12 at 12:09
  • Both of you are correct. The `en0` interface on the iPhone **is** WI-Fi, but on an actual computer it could be Wi-Fi **or** ethernet. – pasawaya Jul 25 '12 at 12:24
  • Ah now I seem to understand the difference. on a PC let's say eth0 would be the physical, maybe ethernet, interface and eth1 another physical (maybe wifi) interface. Whereas en0 is an interface for both? – tzippy Jul 25 '12 at 12:30
  • @tzippy I am not able to find ip for en0 in my mac, Is this normal? But I see I can find ip for en1. – Anurag Sharma May 06 '17 at 11:32
  • Actually I was referring to iPhone not mac. On mac this can actually be different. – tzippy May 06 '17 at 11:39

3 Answers3

0

I managed to put this together with info I found on other sites.

#!/bin/sh
IP_ADDRESS=$(ifconfig en0 | grep inet | cut -d: -f2 | awk '{ print $2}')
IP_ADDR_VAL=$(echo "$IP_ADDRESS" | grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')

if [ $IP_ADDR_VAL -eq 0 ]; then
   echo not valid
else 
   echo valid
fi
tzippy
  • 6,458
  • 30
  • 82
  • 151
0

Actually I noticed the inet part being only there when I'm connected, so a piped grep inet should do the job just fine!

tzippy
  • 6,458
  • 30
  • 82
  • 151
-1

my ifconfig output is different

inet addr:192.168.210.140  Bcast:192.168.215.255  Mask:255.255.248.0
inet6 addr: fe80::92fb:a6ff:fe66:970/64 Scope:Link

however the difference is not great u will find out what u need to change (I understand u need to check one interface - here eth0; every interface has one inet line)

if [[ `/sbin/ifconfig eth0 | grep -e 'inet ' | cut -d ' ' -f12 | grep -E "^addr:[0-9]{3}.[0-9]{3}.[0-9]{3}.[0-9]{3}$" | wc -l` -eq 1 ]]; then
  echo "correct IP";
else
  echo "no IP";
fi
echo " assigned";
lord.didger
  • 1,357
  • 1
  • 17
  • 31
  • your 'cut -d ' ' -f12' command doesnt seem to return anthing. – tzippy Jul 25 '12 at 12:54
  • i've got this ' (lots of spaces) inet addr:192.168.210.140 Bcast:192.168.215.255 Mask:255.255.248.0' after `/sbin/ifconfig eth0 | grep -e 'inet '`. u need to check what your grep prints and adjust cut option -f. – lord.didger Jul 25 '12 at 13:19