Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "net"
  5.     "time"
  6.     "fmt"
  7. )
  8.  
  9. func main() {
  10.     // this is an "array" or "slice" (a collection of something)
  11.     // the []string defines how it is to be stored, string is "text" where as an "int" or "integer" can only be a number
  12.     // putting [] infront of a type (such as string or int) makes it a collection
  13.     // in bash this is probably written something like Array("address1", "address2")
  14.     hosts := []string{"10.0.0.200", "10.0.0.84", "google.com", "telstra.com", "syscad.net"}
  15.  
  16.     // this is a "for loop", it loops through a collection (array or slice)
  17.     // in this example it will go through each item in hosts
  18.     //             `range hosts`    // loop through hosts from start to end
  19.     //     `host`                   // this is how you 'access' each item that's benig looped over
  20.     // host will equal: "10.0.0.200" then "10.0.0.84" then "google.com" then "telstra.com" then "syscad.net" then stop
  21.     for _, host := range hosts {
  22.  
  23.         // here I'm running a function, this is similar to running any command in bash with parameters
  24.         // such as `ping 8.8.8.8` where 'ping'(checkIp) is the function name and '8.8.8.8'(host) is the parameter
  25.         // I have an extra parameter here for port so you specifically target a service - 80 is for a web service,
  26.         // but you could also target a printing port service
  27.         checkIp(host, 80)
  28.     }
  29.  
  30. }
  31.  
  32. func checkIp(host string, port int)  {
  33.  
  34.     // fmt.Sprintf is a text formatting package
  35.     // here I'm setting `address` to address:port ie google.com:80
  36.     address := fmt.Sprintf("%s:%d", host, port)
  37.  
  38.     // this is essentially the same as you netcat command
  39.     // it attempts to connect to the address over tcp with a 5 second timeout (so it doesn't hang forever)
  40.     conn, err := net.DialTimeout("tcp", address, time.Second * 5)
  41.  
  42.     // don't worry about this it's just cleaning up the connection as it will leave a network socket open if you don't
  43.     if conn != nil {
  44.         defer conn.Close()
  45.     }
  46.  
  47.     // if there was an error with connecting (ie host is down) print out that the connection had failed
  48.     if err != nil {
  49.         fmt.Printf("%s failed\n", address)
  50.     } else {
  51.         // otherwise print out that it was a success
  52.         fmt.Printf("%s success\n", address)
  53.     }
  54. }
  55.  
  56. // the output of this program is
  57. //  10.0.0.200:80 success
  58. //  10.0.0.84:80 failed
  59. //  google.com:80 success
  60. //  telstra.com:80 success
  61. //  syscad.net:80 success
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement