Use go to write a packet that detects TCP UDP status

Source: Internet
Author: User

This year's goal is to learn the go language, because you want to follow Dockerpool's friends to write Docker-managed programs.

After reading the Go tutorial, Advanced Code also do not understand, so decided to try to write some common things can be used, the NC command under the MAC can detect a lot of things, but with not used to, so decided to write a check TCP UDP packet.

 PackageMainImport("Flag"    "FMT"    "NET")ConstVersion ="0.1.0"varInput_protocol = flag. String ("P","TCP","The protocol want to check")funcTCP (URLstring)int{_, Err: = Net. Dial ("TCP", URL)ifErr! =Nil{FMT. PRINTLN (ERR)return0}Else{return1}}funcUDP (URLstring)int{_, Err: = Net. Dial ("UDP", URL)ifErr! =Nil{FMT. PRINTLN (ERR)return0}Else{return1}}funcMain () {flag. Parse ()ifFlag. Narg () <1{useage: ="Use example: Check-p TCP 192.168.7.26:22 or check-p UDP 192.168.7.23:123"Fmt. Println (useage)return} p: = *input_protocolSwitch{ Casep = ="TCP": r: = TCP (Flag). Args ()[0]) FMT. Println (R) Casep = ="UDP": r: = UDP (flag. Args ()[0]) FMT. Println (r)}}

go build go install After that, you can use it again in the command line.

$ go build$ go install$ check使用示例: check -p tcp 192.168.7.26:22 或者 check -p udp 192.168.7.23:123$ check -p tcp www.sina.com:801$ check -p tcp www.sina.com:88dial tcp 61.172.201.20:88: connection refused0

Use go to write a packet that detects TCP UDP status

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.