-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (57 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
var (
filepath = flag.String("fpath", "input.txt", "input file path")
)
func main() {
flag.Parse()
file, err := os.Open(*filepath)
fileInfo, _ := os.Stat(*filepath)
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
if err == nil && fileInfo.IsDir() {
log.Fatalf("%s is a directory and cannot be opened", *filepath)
}
defer func() {
if err = file.Close(); err != nil {
log.Fatal(err)
}
}()
// Parse the input file. Each line in the file has IP address
// and port in the format host:port
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var hostPorts []string
for scanner.Scan() {
hostPorts = append(hostPorts, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
// for debugging
// fmt.Printf("Input: %v \n", hostPorts)
// timeout after 5 seconds
client := http.Client{
Timeout: 5 * time.Second,
}
fmt.Println("HTTP GET is successful for")
for _, hostPort := range hostPorts {
resp, err := client.Get("http://" + hostPort)
if err != nil {
log.Println(err)
continue
}
if resp.StatusCode == http.StatusOK {
fmt.Printf("%v \n", hostPort)
}
}
}