This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Utility to get IP address of the machine


Andrew DeFaria wrote:
> Bernhard Ege wrote:
> --
> It's lonely at the top, but you eat better.
> 
>> I use my getip.sh script (windows xp):
>> 
>> #!/bin/bash
>> # find default gateways
>> # select the one with the lowest metric
>> ip=`route print | egrep "^ +0.0.0.0 +0.0.0.0 +" | gawk 'BEGIN {
>> metric=255; ip="0.0.0.0"; } { if ( $5 < metric ) { ip=$4; metric=$5; }
>> } END { printf("%s\n",ip); }'`
>> echo Current ip is $ip 1>&2
>> echo $ip
>> 
>> Seems to do the job fine on my setup :-)
> 
> Why not the more simple:
> 
> ipconfig | grep "IP Address" | awk '{print $NF}'

  Because that may return more than one result.  And it doesn't choose the one
with the lowest metric, which is most likely to be the primary gateway to the
world-at-large.


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]