-
Notifications
You must be signed in to change notification settings - Fork 4
/
port-scanner.go
50 lines (45 loc) · 922 Bytes
/
port-scanner.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
package main
import (
"fmt"
"net"
"sort"
"strconv"
"strings"
"time"
)
func main() {
fmt.Println("Enter a host to scan:")
var host string
fmt.Scanln(&host)
fmt.Println("Enter the range of ports to scan (e.g. 1-1024):")
var portRange string
fmt.Scanln(&portRange)
fmt.Println("Scanning...")
ports := getPorts(portRange)
for _, port := range ports {
address := fmt.Sprintf("%s:%d", host, port)
conn, err := net.DialTimeout("tcp", address, time.Second*10)
if err == nil {
fmt.Printf("%d open\n", port)
conn.Close()
}
}
fmt.Println("Scan complete.")
}
func getPorts(portRange string) []int {
ports := []int{}
parts := strings.Split(portRange, "-")
start, err := strconv.Atoi(parts[0])
if err != nil {
return ports
}
end, err := strconv.Atoi(parts[1])
if err != nil {
return ports
}
for i := start; i <= end; i++ {
ports = append(ports, i)
}
sort.Ints(ports)
return ports
}