Skip to content

Instantly share code, notes, and snippets.

@aojea
Last active September 8, 2022 07:53
Show Gist options
  • Save aojea/62c8324f19377af44f9f02b0fccfab45 to your computer and use it in GitHub Desktop.
Save aojea/62c8324f19377af44f9f02b0fccfab45 to your computer and use it in GitHub Desktop.
find ipv6 addresses
package main
import (
"bufio"
"flag"
"fmt"
"go/scanner"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
exitCode = 0
ipv6_regex = regexp.MustCompile(`(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`)
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: find-ips [path ...]\n")
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 {
if err := processFile("standard input", true); err != nil {
report(err)
}
os.Exit(exitCode)
}
for i := 0; i < flag.NArg(); i++ {
path := flag.Arg(i)
switch dir, err := os.Stat(path); {
case err != nil:
report(err)
case dir.IsDir():
if err := walkDir(path); err != nil {
report(err)
}
default:
if err := processFile(path, false); err != nil {
report(err)
}
}
}
os.Exit(exitCode)
}
func processFile(filename string, useStdin bool) error {
var f *os.File
var err error
if useStdin {
f = os.Stdin
} else {
f, err = os.Open(filename)
if err != nil {
return err
}
defer f.Close()
}
counter := 0
r := bufio.NewReader(f)
for {
line, err := r.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
counter++
match := ipv6_regex.FindAllString(line, -1)
if len(match) > 0 {
fmt.Printf("%s:%d %v\n", filename, counter, match)
}
}
return nil
}
func report(err error) {
scanner.PrintError(os.Stderr, err)
exitCode = 2
if err != nil {
panic(err)
}
}
func walkDir(path string) error {
return filepath.WalkDir(path, visitFile)
}
func visitFile(path string, f fs.DirEntry, err error) error {
if err == nil && isGoFile(f) {
err = processFile(path, false)
}
if err != nil {
report(err)
}
return nil
}
func isGoFile(f fs.DirEntry) bool {
// ignore non-Go files
name := f.Name()
return !f.IsDir() && !strings.HasPrefix(name, ".") && !strings.HasSuffix(name, "test.go") && (strings.HasSuffix(name, ".md") || strings.HasSuffix(name, ".go"))
}
@aojea
Copy link
Author

aojea commented Sep 7, 2022

 go run main.go  staging/
filename:  staging/src/k8s.io/api/extensions/v1beta1/types.go
[2001:db9:: 2001:db9:: 2001:db9::]
filename:  staging/src/k8s.io/api/extensions/v1beta1/types_swagger_doc_generated.go
[2001:db9:: 2001:db9:: 2001:db9::]
filename:  staging/src/k8s.io/api/networking/v1/types.go
[2001:db9:: 2001:db9:: 2001:db9::]
filename:  staging/src/k8s.io/api/networking/v1/types_swagger_doc_generated.go
[2001:db9:: 2001:db9:: 2001:db9::]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment