Last active
January 9, 2018 03:03
-
-
Save vvoody/62af3a7bc73b6b07b1a978cb569b55c6 to your computer and use it in GitHub Desktop.
portping - connect to massive ip:port addresses and find out what are not connectable
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
/* | |
Connect to massive ip:port addresses and find out what are not connectable. | |
Port variant of PingInfoView. | |
It only accepts input from a CSV file with following format: | |
10.12.15.121:8080,xxxxx,zzzzzz | |
10.12.15.4:8080,aaaaa,bbbbbb | |
10.12.15.35:8080,ddddd,eeeeee | |
... | |
*/ | |
package main | |
import ( | |
"bufio" | |
"encoding/csv" | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"os" | |
"time" | |
) | |
type VipInfo struct { | |
Addr string | |
VipName string | |
Lb string | |
} | |
func IsConnectable(addr string) bool { | |
conn, err := net.DialTimeout("tcp", addr, 300*time.Millisecond) | |
if err != nil { | |
return false | |
} | |
defer conn.Close() | |
return true | |
} | |
func GetUnconnectableVips(vips []VipInfo) []string { | |
rv := []string{} | |
sem := make(chan bool, 200) | |
for i := 0; i < len(vips); i++ { | |
sem <- true | |
vip := vips[i] | |
addr := vip.Addr | |
vipName := vip.VipName | |
lb := vip.Lb | |
go func(addr string) { | |
if ! IsConnectable(addr) { | |
msg := fmt.Sprintf("%20s, %30s, %25s", addr, vipName, lb) | |
rv = append(rv, msg) | |
} | |
<- sem | |
}(addr) | |
} | |
for i := 0; i < cap(sem); i++ { | |
sem <- true | |
} | |
return rv | |
} | |
func usage() { | |
fmt.Fprintf(os.Stderr, "usage: %s csv_file\n", os.Args[0]) | |
fmt.Println("csv_file must be in format like '10.12.15.121:8080,xxxxx,zzzzzz'.") | |
os.Exit(2) | |
} | |
func main(){ | |
if len(os.Args) != 2 { | |
usage() | |
} | |
csvFile, _ := os.Open(os.Args[1]) | |
reader := csv.NewReader(bufio.NewReader(csvFile)) | |
var vips []VipInfo | |
for { | |
line, err := reader.Read() | |
if err == io.EOF { | |
break | |
} else if err != nil { | |
log.Fatal(err) | |
} | |
vips = append(vips, VipInfo{ | |
Addr: line[0], | |
VipName: line[1], | |
Lb: line[2], | |
}) | |
} | |
fmt.Println("running portping...") | |
unconnectableVips := GetUnconnectableVips(vips) | |
for i := 0; i < len(unconnectableVips); i++ { | |
vipLb := unconnectableVips[i] | |
fmt.Printf("%s, not able to connect\n", vipLb) | |
} | |
fmt.Println("(all other VIPs are good)") | |
} |
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
10.12.15.121:8080 | xxxxx | zzzzzz | |
---|---|---|---|
10.12.15.4:8080 | aaaaa | bbbbbb | |
10.12.15.35:8080 | ddddd | eeeeee | |
... | ... | ... |
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
$ wc -l x_example_input.csv | |
3353 example_input.csv | |
$ | |
$ time ./portping lvslb-vips.csv | |
running portping... | |
10.12.15.121:8080, xxxxx, zzzzzz, not able to connect | |
10.12.15.4:8080, aaaaa, bbbbbb, not able to connect | |
10.12.15.35:8080, ddddd, eeeeee, not able to connect | |
(all other VIPs are good) | |
real 0m0.368s | |
user 0m0.071s | |
sys 0m0.172s | |
$ | |
$ while true; do echo -n $(date) '- ' ; ./portping lvslb-vips.csv; echo; sleep 10; done | |
Sun Jan 7 14:42:33 -07 2018 - running portping... | |
10.12.15.121:8080, xxxxx, zzzzzz, not able to connect | |
10.12.15.4:8080, aaaaa, bbbbbb, not able to connect | |
10.12.15.35:8080, ddddd, eeeeee, not able to connect | |
(all other VIPs are good) | |
Sun Jan 7 14:42:39 -07 2018 - running portping... | |
10.12.15.121:8080, xxxxx, zzzzzz, not able to connect | |
10.12.15.4:8080, aaaaa, bbbbbb, not able to connect | |
10.12.15.35:8080, ddddd, eeeeee, not able to connect | |
(all other VIPs are good) | |
^C | |
$ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment