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