Skip to content

Commit fc81100

Browse files
committed
Initial commit
0 parents  commit fc81100

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/topscoder/oldhost
2+
3+
go 1.20

oldhost.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"net/http"
8+
"os"
9+
"sync"
10+
"time"
11+
)
12+
13+
func main() {
14+
ipsArg := flag.String("ips", "", "IPs (string or filename)")
15+
hostsArg := flag.String("hosts", "", "Hosts (string or filename)")
16+
silent := flag.Bool("silent", false, "Silent mode")
17+
flag.Parse()
18+
19+
ips, err := readLines(*ipsArg)
20+
if err != nil {
21+
fmt.Println("Error reading IP addresses:", err)
22+
return
23+
}
24+
25+
hosts, err := readLines(*hostsArg)
26+
if err != nil {
27+
fmt.Println("Error reading hosts:", err)
28+
return
29+
}
30+
31+
client := http.Client{
32+
Timeout: 1 * time.Second,
33+
}
34+
35+
var wg sync.WaitGroup
36+
semaphore := make(chan struct{}, 5) // Limit concurrent calls to 5
37+
38+
for _, ip := range ips {
39+
for _, host := range hosts {
40+
wg.Add(1)
41+
go func(ip, host string) {
42+
defer wg.Done()
43+
44+
semaphore <- struct{}{} // Acquire a slot from the semaphore
45+
defer func() {
46+
<-semaphore // Release the slot back to the semaphore
47+
}()
48+
49+
url := fmt.Sprintf("http://%s", ip)
50+
req, err := http.NewRequest("GET", url, nil)
51+
if err != nil {
52+
if !*silent {
53+
fmt.Printf("Error creating request for IP %s and host %s: %s\n", ip, host, err)
54+
}
55+
return
56+
}
57+
req.Host = host
58+
59+
resp, err := client.Do(req)
60+
if err != nil {
61+
if !*silent {
62+
fmt.Printf("Error making request for IP %s and host %s: %s\n", ip, host, err)
63+
}
64+
return
65+
}
66+
defer resp.Body.Close()
67+
68+
if *silent && resp.StatusCode != http.StatusOK {
69+
return
70+
}
71+
72+
if resp.StatusCode == http.StatusOK {
73+
fmt.Printf("%s\t%s\n", ip, host)
74+
}
75+
}(ip, host)
76+
}
77+
}
78+
79+
wg.Wait()
80+
}
81+
82+
func readLines(arg string) ([]string, error) {
83+
lines := []string{}
84+
85+
if fileExists(arg) {
86+
file, err := os.Open(arg)
87+
if err != nil {
88+
return lines, err
89+
}
90+
defer file.Close()
91+
92+
scanner := bufio.NewScanner(file)
93+
for scanner.Scan() {
94+
lines = append(lines, scanner.Text())
95+
}
96+
} else {
97+
lines = append(lines, arg)
98+
}
99+
100+
return lines, nil
101+
}
102+
103+
func fileExists(path string) bool {
104+
_, err := os.Stat(path)
105+
if os.IsNotExist(err) {
106+
return false
107+
}
108+
return err == nil
109+
}

0 commit comments

Comments
 (0)