Last active
June 10, 2017 11:10
-
-
Save gretel/1cadb5c626930cab7a0b8687aa710916 to your computer and use it in GitHub Desktop.
tiny golang to gather external ipv4/6 addresses using https://github.com/andreaskoch/myip
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
///usr/bin/true; exec /usr/bin/env go run "$0" "$@" | |
// | |
//https://gist.github.com/gretel/1cadb5c626930cab7a0b8687aa710916 | |
//usage: | |
// $ external_ip.go | |
// $ external_ip.go -4=true -6=true | |
// $ external_ip.go -4=false -6=true | |
package main | |
import ( | |
"fmt" | |
"flag" | |
"github.com/andreaskoch/myip" | |
"os" | |
) | |
// note, that variables are pointers | |
var fourFlag = flag.Bool("4", true, "output for version 6") | |
var sixFlag = flag.Bool("6", false, "output for version 4") | |
func main() { | |
flag.Parse() | |
if(*fourFlag == false && *sixFlag == false) { | |
os.Exit(1) | |
} | |
remoteIPProvider := myip.NewRemoteIPProvider() | |
if(*fourFlag) { | |
remoteIPv4Addresses, remoteIPError := remoteIPProvider.GetIPv4Addresses() | |
if remoteIPError != nil { | |
fmt.Fprintf(os.Stderr, "failed to determine ipv4 addresses: %s\n", remoteIPError.Error()) | |
os.Exit(2) | |
} | |
fmt.Fprintf(os.Stdout, "%s\n", remoteIPv4Addresses[0]) | |
} | |
if(*sixFlag) { | |
remoteIPv6Addresses, remoteIPError := remoteIPProvider.GetIPv6Addresses() | |
if remoteIPError != nil { | |
fmt.Fprintf(os.Stderr, "failed to determine ipv6 addresses: %s\n", remoteIPError.Error()) | |
os.Exit(3) | |
} | |
fmt.Fprintf(os.Stdout, "%s\n", remoteIPv6Addresses[0]) | |
} | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment