[ale] Get host's IP address as an unprivileged user?

Derek Atkins warlord at MIT.EDU
Sat Jun 4 10:05:18 EDT 2011


JD <jdp at algoloma.com> writes:

> On 06/03/2011 09:25 AM, James Sumners wrote:
>> I need to get the IP address of the local machine on which a script is
>> being executed. The script is going to be run by an unprivileged user
>> (so /sbin is not in the PATH). Does anyone know of a better way than
>> this:
>> 
>> IP=$(host $(hostname) | awk '{print $4}')
>> 
>
> I never trust the PATH in scripts.  Anyone can call /sbin/ifconfig.
> If you must, just add :/sbin to your PATH (ew).

Like JD said, you do not need to be root to run ifconfig:.

[warlord at dev webdav]$ whoami
warlord
[warlord at dev webdav]$ /sbin/ifconfig wlan0 
wlan0     Link encap:Ethernet  HWaddr 00:21:6A:90:98:58  
          inet addr:192.168.248.100  Bcast:192.168.248.255  Mask:255.255.255.0
...
[warlord at dev webdav]$ /sbin/ifconfig wlan0 | grep 'inet addr' | tr : ' ' | awk '{print $3}'
192.168.248.100

-derek
-- 
       Derek Atkins, SB '93 MIT EE, SM '95 MIT Media Laboratory
       Member, MIT Student Information Processing Board  (SIPB)
       URL: http://web.mit.edu/warlord/    PP-ASEL-IA     N1NWH
       warlord at MIT.EDU                        PGP key available


More information about the Ale mailing list