Skip to content

Instantly share code, notes, and snippets.

@STRRL
Created April 26, 2023 06:54
Show Gist options
  • Save STRRL/6d3fcd7bfc3647e3767e36ba2f007cd6 to your computer and use it in GitHub Desktop.
Save STRRL/6d3fcd7bfc3647e3767e36ba2f007cd6 to your computer and use it in GitHub Desktop.
keep-lookup
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