Last active
September 8, 2022 07:53
-
-
Save aojea/62c8324f19377af44f9f02b0fccfab45 to your computer and use it in GitHub Desktop.
find ipv6 addresses
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 ( | |
"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")) | |
} |
Author
aojea
commented
Sep 7, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment