|
| 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