Created
April 28, 2022 16:20
-
-
Save crhntr/64e17a1fe85fe0548579cdc93a7f4bcc to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"bufio" | |
"context" | |
"fmt" | |
"os" | |
"os/exec" | |
"os/signal" | |
"regexp" | |
) | |
func main() { | |
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) | |
defer cancel() | |
cmd := exec.CommandContext(ctx, "lsof", "-i", ":443") | |
stdout, err := cmd.StdoutPipe() | |
if err != nil { | |
panic(err) | |
} | |
err = cmd.Start() | |
if err != nil { | |
panic(err) | |
} | |
ex := regexp.MustCompile("->(.*):") | |
b := bufio.NewReader(stdout) | |
for { | |
buf, err := b.ReadBytes('\n') | |
if err != nil { | |
break | |
} | |
matches := ex.FindAllSubmatch(buf, -1) | |
for _, match := range matches { | |
if len(match) == 0 { | |
continue | |
} | |
_, _ = fmt.Fprintf(os.Stdout, "%s\n", match[1]) | |
} | |
} | |
err = cmd.Wait() | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment