Use go to write a packet to check the status of tcpudp
The goal of this year is to learn the go language because we want to write Docker management programs with our dockerpool friends.
After reading the go getting started tutorial, I still cannot understand the advanced code, so I decided to try something that can be used normally first. The nc command under mac can detect many things, but I am not used to it, so I decided to write a check tcp udp package.
Package mainimport ("flag" "fmt" "net") const version = "0.1.0" var Input_protocol = flag. string ("p", "tcp", "The protocol you want to check") func tcp (url string) int {_, err: = net. dial ("tcp", url) if err! = Nil {fmt. println (err) return 0} else {return 1} func udp (url string) int {_, err: = net. dial ("udp", url) if err! = Nil {fmt. println (err) return 0} else {return 1} func main () {flag. parse () if flag. NArg () <1 {useage: = "Example: check-p tcp 192.168.7.26: 22 or check-p udp 192.168.7.23: 123" fmt. println (useage) return} p: = * Input_protocol switch {case p = "tcp": r: = tcp (flag. args () [0]) fmt. println (r) case p = "udp": r: = udp (flag. args () [0]) fmt. println (r )}}
After go build go install, you can use it in the command line.
$ Go build $ go install $ check example: check-p tcp 192.168.7.26: 22 or check-p udp 192.168.7.23: 123 $ check-p tcp www.sina.com: 801 $ check-p tcp www.sina.com: 88 dial tcp 61.172.201.20: 88: connection refused0