Created
April 26, 2023 06:54
-
-
Save STRRL/6d3fcd7bfc3647e3767e36ba2f007cd6 to your computer and use it in GitHub Desktop.
keep-lookup
This file contains hidden or 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 | |
import ( | |
"context" | |
"fmt" | |
"net" | |
"os" | |
"os/signal" | |
"time" | |
) | |
func main() { | |
// listen signal interrupt | |
ctx, cancel := context.WithCancel(context.Background()) | |
go func() { | |
// wait for signal interrupt | |
sigChan := make(chan os.Signal, 1) | |
signal.Notify(sigChan, os.Interrupt) | |
<-sigChan | |
cancel() | |
}() | |
// domain is the first command line argument | |
if len(os.Args) < 2 { | |
fmt.Fprintf(os.Stderr, "Usage: %s <domain>", os.Args[0]) | |
} | |
domain := os.Args[1] | |
lookupLoop(ctx, domain) | |
os.Exit(0) | |
} | |
func lookupLoop(ctx context.Context, doamin string) { | |
canceled := false | |
go func() { | |
for !canceled { | |
// lookup | |
ips, err := net.LookupIP(doamin) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "lookup %s failed: %v\n", doamin, err) | |
} | |
fmt.Printf("lookup %s: %v\n", doamin, ips) | |
// sleep 5s | |
time.Sleep(3 * time.Second) | |
} | |
}() | |
<-ctx.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment