Skip to content

Commit cb82859

Browse files
authored
Merge pull request #726 from pritamp17/issue_705
Port Scanner in Go
2 parents b16b731 + a3d08f8 commit cb82859

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

Go/port-scanner-go/Readme.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Port scanner in Go
2+
Go-lang script for scanning specific or all open ports of the device.
3+
# Packages Used
4+
- fmt
5+
- time
6+
- sync
7+
- strings
8+
- net
9+
10+
# How to run
11+
Navigate to the project directory.
12+
```bash
13+
cd Rotten-Scripts/Go/port-scanner-go
14+
```
15+
```bash
16+
go run port_scanner.go
17+
```
18+
## Output
19+
20+
![](https://i.imgur.com/xMil1Kh.jpg)
21+
![](https://i.imgur.com/VEQPbz3.jpg)
22+
![](https://i.imgur.com/tMiEYNk.jpg)
23+
24+
## Author
25+
- [Pritam Pawar](https://github.com/pritamp17)

Go/port-scanner-go/port_scanner.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"time"
7+
"sync"
8+
"strings"
9+
10+
)
11+
12+
13+
var wg sync.WaitGroup
14+
func main() {
15+
16+
var option int
17+
var ip string
18+
fmt.Print("[+] Enter IP address to scan: ")
19+
fmt.Scan( & ip)
20+
21+
var typeOfPort string
22+
fmt.Print("[+] Enter type of port you want to scan for eg(tcp,udp..etc): ")
23+
fmt.Scan( & typeOfPort)
24+
25+
fmt.Print("[+] For scanning all open ports press : 1 \n[+]For scanning Specific ports press :2 \n")
26+
fmt.Scan( & option)
27+
if option == 1 {
28+
scanAllPorts(ip, typeOfPort)
29+
} else if option == 2 {
30+
scanSelectedPorts(ip, typeOfPort)
31+
} else {
32+
fmt.Println("Please chose valid option")
33+
}
34+
}
35+
36+
// function for scanning all Ports
37+
func scanAllPorts(ip string, typeOfPort string) {
38+
wg.Add(65535)
39+
for port: = 1;
40+
port < 65535;
41+
port++{
42+
hostIp: = fmt.Sprintf("%s:%d", ip, port)
43+
// fmt.Println(hostIp)
44+
45+
_,
46+
err: = net.DialTimeout(typeOfPort, hostIp, 10 * time.Millisecond)
47+
48+
if err == nil {
49+
fmt.Printf("Port %d is open \n", port)
50+
}
51+
}
52+
wg.Wait()
53+
54+
wg.Done()
55+
}
56+
// function for scanning specific Ports
57+
func scanSelectedPorts(ip string, typeOfPort string) {
58+
var portsInput string
59+
fmt.Print("[+] Enter Ports to scan: ")
60+
fmt.Scan( & portsInput)
61+
ports: = strings.Split(portsInput, ",")
62+
for _, port: = range ports {
63+
hostIp: = fmt.Sprintf("%s:%s", ip, port)
64+
_,
65+
err: = net.DialTimeout(typeOfPort, hostIp, 80 * time.Millisecond)
66+
if err != nil {
67+
fmt.Printf("Port %s is closed \n", port)
68+
} else {
69+
fmt.Printf("Port %s is open \n", port)
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)