go-ps
is a simple implementation of a port-scanner in golang.
To obtain the module, simply execute in shell:
$ go get github.com/NovusEdge/go-ps
- Importing the module:
import gops "github.com/NovusEdge/go-ps"
- Declaring an a variable of type
PortScanner
:
// Decalring a PortScanner object.
/*
struct definition:
type PortScanner struct {
Domain string
Protocol string
}
*/
ps := gops.PortScanner{
Domain: "scanme.nmap.org",
Protocol: "tcp",
}
-
Scan()
iterates over all ports in range [startPort, endPort] and reports which ports are open by printing to stdout.
/* Scan() parameters:
startPort [int] Port from which to start scanning (inclusive)
endPort [int] Port on which to stop the scanning. (inclusive)
timeout [time.Duration] timeout for each port being scanned.
*/
ps.Scan(1, 1024, 500*time.Millisecond)
Output:
[*] Port 80 open
...
...
-
Scans()
iterates over all ports in range [startPort, endPort] and returns a list of open ports.
/* Scans() parameters:
startPort [int] Port from which to start scanning (inclusive)
endPort [int] Port on which to stop the scanning. (inclusive)
timeout [time.Duration] timeout for each port being scanned.
Returns: []int
*/
ports := ps.Scans(1, 1024, 500*time.Millisecond)
fmt.Println("Open Ports: ", ports)
Output:
Open Ports: [ 80 448 ... ]
package main
import (
"fmt"
"time"
gops "github.com/NovusEdge/go-ps"
)
func main() {
ps := gops.PortScanner{
Domain: "scanme.nmap.org",
Protocol: "tcp",
}
// Scan and report open ports in stdout:
ps.Scan(1, 1024, 500*time.Millisecond)
// Scan ports and get a list of open ports:
openPorts := ps.Scans(1, 1024, 500*time.Millisecond)
// Use in whatever way you like :)
// For this sample, we'll just print it to stdout:
fmt.Println(openPorts)
}