Use go to write a packet to check the status of tcpudp

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.