Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net"
- "time"
- )
- func isPortOpen2(dest_host string, port int, out chan string) {
- // defer close(out)
- // If I do this, it closes the channel after the first execution of function which makes sense.
- // But I dont do this, when I print the output I need to send ctrl c to exit the program.
- timeout := time.Second * 1
- target := fmt.Sprintf("%s:%d", dest_host, port)
- conn, err := net.DialTimeout("tcp", target, timeout)
- if err != nil {
- out <- fmt.Sprintf("[%s]\t %s", "CLOSED", target)
- }
- if conn != nil {
- out <- fmt.Sprintf("[%s]\t %s", "OPEN", target)
- }
- }
- func main() {
- out := make(chan string)
- hosts := []string{"google.com", "reddit.com", "duckduckgo.com", "python.org"}
- ports := []int{123, 43, 534, 325, 12, 443, 8080, 34, 5455, 23543, 9432, 843}
- for _, host := range hosts {
- for _, port := range ports {
- go isPortOpen2(host, port, out)
- }
- }
- for i := range out {
- fmt.Println(i)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement