[ale] Get host's IP address as an unprivileged user?
Geoffrey Myers
lists at serioustechnology.com
Mon Jun 6 14:00:37 EDT 2011
Geoffrey Myers wrote:
> Derek Atkins wrote:
>> 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
>
> You probably can't guarantee that the device will be wlan0. Further,
> you can combine your grep, awk, and tr into a single awk script:
>
> ifconfig| awk -F'[" ":]' '/Bcast/ {print $16, NF}'
>
> Note, I've looking for Bcast, should match any configured interfaces.
> Problem is, if you have more then one interface configured....\
that should have been:
ifconfig| awk -F'[" ":]' '/Bcast/ {print $16}'
I had the NF in the original output to see if I was counting the fields
properly. :(
>
>
>> -derek
>
>
--
Until later, Geoffrey
"I predict future happiness for America if they can prevent
the government from wasting the labors of the people under
the pretense of taking care of them."
- Thomas Jefferson
More information about the Ale
mailing list