Utility to get IP address of the machine

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Utility to get IP address of the machine

Alex Vinokur
What is Cygwin utility to get IP address of the machine?

--
 Alex Vinokur
     email: alex DOT vinokur AT gmail DOT com
     http://mathforum.org/library/view/10978.html
     http://sourceforge.net/users/alexvn





--
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/

Reply | Threaded
Open this post in threaded view
|

RE: Utility to get IP address of the machine

Dave Korn
Alex Vinokur wrote:
> What is Cygwin utility to get IP address of the machine?

  A machine can have many IP addresses, one for each network interface.

  If you don't care /which/ one, you can just hard-code "127.0.0.1" ;-) but I
think that's probably not the answer you're looking for.

  You may need to parse the output from the windows "ipconfig /all".

    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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Bernhard Ege
In reply to this post by Alex Vinokur
Alex Vinokur wrote:
> What is Cygwin utility to get IP address of the machine?
>

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 :-)

Bernhard


--
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/

Reply | Threaded
Open this post in threaded view
|

RE: Utility to get IP address of the machine

Herb Martin
In reply to this post by Dave Korn
>   If you don't care /which/ one, you can just hard-code
> "127.0.0.1" ;-) but I
> think that's probably not the answer you're looking for.
>
>   You may need to parse the output from the windows "ipconfig /all".
>
>     cheers,
>       DaveK

While it is almost always more useful to use
"Ipconfig /all" when working interactively,
when one just wants the IP addresses just using
plain "ipconfig" gets them without so much noise
to parse through.

--
Herb Martin




--
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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Andrew DeFaria-2
In reply to this post by Bernhard Ege
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}'



--
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/

Reply | Threaded
Open this post in threaded view
|

RE: Utility to get IP address of the machine

Dave Korn
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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Peter J. Acklam
In reply to this post by Herb Martin
Herb Martin wrote:

> While it is almost always more useful to use "Ipconfig
> /all" when working interactively, when one just wants the
> IP addresses just using plain "ipconfig" gets them without
> so much noise to parse through.

Or

   perl -MSocket -MSys::Hostname -wle \
      'print inet_ntoa(scalar gethostbyname(hostname || "localhost"))'

Peter


--
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/
Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Jon A. Lambert
In reply to this post by Alex Vinokur
Alex Vinokur wrote:
> What is Cygwin utility to get IP address of the machine?
>

Here's another way to get the IP of your machine, from outside your network.

ruby -ropen-uri -e "URI.parse('http://www.whatismyip.com').read =~
/((\d+\.?){4})/; puts $&"

--
J Lambert



--
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/

Reply | Threaded
Open this post in threaded view
|

RE: Utility to get IP address of the machine

Frier, David
In reply to this post by Alex Vinokur
Probably useless if you're behind any kind of NAT device.  

--David

-----Original Message-----
From: Jon A. Lambert [mailto:[hidden email]]
Sent: Wednesday, January 04, 2006 8:17
To: [hidden email]
Subject: Re: Utility to get IP address of the machine

Alex Vinokur wrote:
> What is Cygwin utility to get IP address of the machine?
>

Here's another way to get the IP of your machine, from outside your
network.

ruby -ropen-uri -e "URI.parse('http://www.whatismyip.com').read =~
/((\d+\.?){4})/; puts $&"

--
J Lambert




--
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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Joe Smith-10
David said:
>Probably useless if you're behind any kind of NAT device.
Unless you need the external ip address. Sometimes you need the internal
address, but other times you need the external address.







--
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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Andrew DeFaria-2
In reply to this post by Dave Korn
Dave Korn wrote:

>> 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.

By and large most people have but 1 IP address but if you must tack on a
"| head -1". YMMV
--
I had a dream that all the victims of The Pill came back...boy, were
they mad!!



--
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/

Reply | Threaded
Open this post in threaded view
|

Re: Utility to get IP address of the machine

Bernhard Ege
Andrew DeFaria wrote:

> Dave Korn wrote:
>
>>> 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.
>
>
> By and large most people have but 1 IP address but if you must tack on a
> "| head -1". YMMV

Perhaps, but if you ever use VPN, you have more than one network
interface (one physical and one virtual). This was why I created my
little script.

Also, many new motherboard have 2 NICs and also a firewire port that
also functions as a NIC. All in all, more than 1 network interface is
not that uncommon (I have 4 on my pc but up to 3 on my laptop (NIC,
wireless, VPN).

Anyway, I gave my script to aid, not to start a discussion.

Have fun ;-)

Bernhard


--
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/