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

Richard Bronosky Richard at Bronosky.com
Wed Jun 8 00:19:59 EDT 2011


Thanks Geoffrey. A little piece of me dies every time I see some one
pipe [almost] any combination of grep | sed | awk | tr | cut

On Mon, Jun 6, 2011 at 1:44 PM, Geoffrey Myers
<lists at serioustechnology.com> 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....
>
>
>>
>> -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
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
.!# RichardBronosky #!.



More information about the Ale mailing list