Find the local IP address

4,474 views
Skip to first unread message

Kowshik Prakasam

unread,
Sep 24, 2012, 5:10:20 AM9/24/12
to golan...@googlegroups.com
The function below returns the local IP address if you give it a server
that it should connect to. For example, you can pass "google.com:80"
to the function.

Is there a simpler way to find the local IP address? For example, without
specifying the port number in the server? In the code below, absence
of port number in the server raises an error.

====================================================
func getLocalIp(server string) (*string, error) {
        conn, err := net.Dial("udp", server)
        if err != nil {
                return nil, err
        }

        // conn.LocalAddr().String() returns ip_address:port                                                                    
        return &strings.Split(conn.LocalAddr().String(), ":")[0], nil
}
====================================================



-Kowshik

Daniël Bos (远洋)

unread,
Sep 24, 2012, 5:15:20 AM9/24/12
to Kowshik Prakasam, golan...@googlegroups.com
I believe you can use net.LookupHost: http://golang.org/pkg/net/#LookupHost
> --
>
>



--
远洋 / Daniël Bos

email : cor...@gmail.com
phone : +31-318-711063 (Dutch) / +86-18-701330735 (Chinese)
weblog : http://blog.loadingdata.nl/
ostatus: cor...@status.loadingdata.nl

spete...@gmail.com

unread,
Sep 24, 2012, 5:22:37 AM9/24/12
to Kowshik Prakasam, golan...@googlegroups.com
What do you want to do with the local address?

There are usually multiple addresses assigned to each host (one or two local host addresses, LAN addresses, VPN addresses etc.)
Which one you need and how to obtain it generally depends on what you want to use it for.

Peter

--
 
 

RoboTamer

unread,
Sep 24, 2012, 5:25:07 AM9/24/12
to golan...@googlegroups.com
package main

import (
    "fmt"
    "net"
)

func main(){
    ip, _ := net.LookupIP("google.net")
    fmt.Println(ip)

spete...@gmail.com

unread,
Sep 24, 2012, 5:53:01 AM9/24/12
to spete...@gmail.com, Kowshik Prakasam, golan...@googlegroups.com
By "local host address" I meant "loopback address."

You can get all network addresses using net.InterfaceAddrs() and eliminate those that are not IP networks to get all local IP addresses.

Peter

RoboTamer

unread,
Sep 24, 2012, 6:12:19 AM9/24/12
to golan...@googlegroups.com

I don't think your code works right.
The ip address you are getting is not google.com:80 servers local address.
It may be your local address.
I know because I tried it on my own server the result is incorrect.
It always returns the same result.


On Monday, September 24, 2012 2:10:27 AM UTC-7, Kowshik Prakasam wrote:

Steve McCoy

unread,
Sep 24, 2012, 8:03:07 AM9/24/12
to golan...@googlegroups.com
I wrote a small program to print the host's external IPs: https://github.com/mccoyst/myip/blob/master/myip.go

It's been a while and I don't recall why I didn't use net.InterfaceAddrs(), though it actually seems like less programming effort than manually filtering out the loopbacks. However, I could be convinced that it'd be worth changing.

Peter S

unread,
Sep 24, 2012, 9:03:26 AM9/24/12
to Steve McCoy, golan...@googlegroups.com
On Mon, Sep 24, 2012 at 9:03 PM, Steve McCoy <mcc...@gmail.com> wrote:
I wrote a small program to print the host's external IPs: https://github.com/mccoyst/myip/blob/master/myip.go

It's been a while and I don't recall why I didn't use net.InterfaceAddrs(), though it actually seems like less programming effort than manually filtering out the loopbacks. However, I could be convinced that it'd be worth changing.


According to your program, my laptop's "external IP" (whatever that means) is 127.0.1.1.
It seems that your program obtains the hostname and attempts to resolve it. In general, the hostname may or may not resolve to an IP address, and even when it does, it may resolve to a loopback or a non-loopback IP address.

Here is another one which should list all IP addresses of a host (10 addresses on my laptop):
http://play.golang.org/p/Apqck0ovcr
Of course, it doesn't run on the playground, you have to copy it and run it locally.

Peter



On Monday, September 24, 2012 5:10:27 AM UTC-4, Kowshik Prakasam wrote:
The function below returns the local IP address if you give it a server
that it should connect to. For example, you can pass "google.com:80"
to the function.

Is there a simpler way to find the local IP address? For example, without
specifying the port number in the server? In the code below, absence
of port number in the server raises an error.

====================================================
func getLocalIp(server string) (*string, error) {
        conn, err := net.Dial("udp", server)
        if err != nil {
                return nil, err
        }

        // conn.LocalAddr().String() returns ip_address:port                                                                    
        return &strings.Split(conn.LocalAddr().String(), ":")[0], nil
}
====================================================



-Kowshik

--
 
 

Russ Cox

unread,
Sep 24, 2012, 10:11:00 AM9/24/12
to golang-nuts
func localIP() (net.IP, error) {
tt, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, t := range tt {
aa, err := t.Addrs()
if err != nil {
return nil, err
}
for _, a := range aa {
ipnet, ok := a.(*net.IPNet)
if !ok {
continue
}
v4 := ipnet.IP.To4()
if v4 == nil || v4[0] == 127 { // loopback address
continue
}
return v4, nil
}
}
return nil, errors.New("cannot find local IP address")
}

Steve McCoy

unread,
Sep 24, 2012, 11:09:19 AM9/24/12
to golan...@googlegroups.com, Steve McCoy
On Monday, September 24, 2012 9:03:40 AM UTC-4, speter wrote:


On Mon, Sep 24, 2012 at 9:03 PM, Steve McCoy <mcc...@gmail.com> wrote:
I wrote a small program to print the host's external IPs: https://github.com/mccoyst/myip/blob/master/myip.go

It's been a while and I don't recall why I didn't use net.InterfaceAddrs(), though it actually seems like less programming effort than manually filtering out the loopbacks. However, I could be convinced that it'd be worth changing.


According to your program, my laptop's "external IP" (whatever that means) is 127.0.1.1.
It seems that your program obtains the hostname and attempts to resolve it. In general, the hostname may or may not resolve to an IP address, and even when it does, it may resolve to a loopback or a non-loopback IP address.

Here is another one which should list all IP addresses of a host (10 addresses on my laptop):
http://play.golang.org/p/Apqck0ovcr
Of course, it doesn't run on the playground, you have to copy it and run it locally.

Peter


Thanks for the counterexample. I've changed it to use net.InterfaceAddrs and IP.IsLoopback to filter those out (I don't know about the OP, but I'm only interested in the IPs that other hosts can use to connect to me — which is what I meant by "external IPs").
Reply all
Reply to author
Forward
0 new messages