-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.go
More file actions
50 lines (46 loc) · 1.07 KB
/
Copy pathscan.go
File metadata and controls
50 lines (46 loc) · 1.07 KB
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"
"time"
)
type ScanResult struct {
port int
open bool
banner string
err error
}
func (s ScanResult) String() string {
return fmt.Sprintf("{ Port %d: Open {%t}\tBanner {%s}\tError str {%s} }", s.port, s.open, s.banner, s.err.Error())
}
func scanPort(host string, port int, timeout time.Duration) ScanResult {
fmt.Printf("Scanning %s:%d (timeout %s)\n", host, port, timeout.String())
isOpen := false
banner := ""
var err error
var conn, conn_err = net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
if conn_err != nil || conn == nil {
isOpen = false
err = conn_err
} else {
buff := make([]byte, 256)
defer conn.Close()
isOpen = true
conn.SetReadDeadline(time.Now().Add(timeout))
count, read_err := conn.Read(buff)
if read_err != nil {
fmt.Println("Could not read from buffer.")
err = read_err
} else {
banner = string(buff[:count])
fmt.Printf("Read %d bytes from port\n", count)
isOpen = true
}
}
return ScanResult{
open: isOpen,
banner: banner,
port: port,
err: err,
}
}