Created
March 26, 2014 17:26
-
-
Save jniltinho/9788660 to your computer and use it in GitHub Desktop.
Get My IP External/Internal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
Usage of ./getmyip --get_ip=(external|internal) | |
URL: http://myexternalip.com/#golang | |
URL: http://changsijay.com/2013/07/28/golang-get-ip-address/ | |
*/ | |
import ( | |
"flag" | |
"io" | |
"net" | |
"net/http" | |
"os" | |
) | |
var get_ip = flag.String("get_ip", "", "external|internal") | |
func main() { | |
flag.Parse() | |
if *get_ip == "external" { | |
get_external() | |
} | |
if *get_ip == "internal" { | |
get_internal() | |
} | |
} | |
func get_external() { | |
resp, err := http.Get("http://myexternalip.com/raw") | |
if err != nil { | |
os.Stderr.WriteString(err.Error()) | |
os.Stderr.WriteString("\n") | |
os.Exit(1) | |
} | |
defer resp.Body.Close() | |
io.Copy(os.Stdout, resp.Body) | |
os.Exit(0) | |
} | |
func get_internal() { | |
addrs, err := net.InterfaceAddrs() | |
if err != nil { | |
os.Stderr.WriteString("Oops: " + err.Error() + "\n") | |
os.Exit(1) | |
} | |
for _, a := range addrs { | |
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | |
if ipnet.IP.To4() != nil { | |
os.Stdout.WriteString(ipnet.IP.String() + "\n") | |
} | |
} | |
} | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment