Golang : Find network service name from given port and protocol
In this tutorial, we will learn how to discover the list of available network services on a machine. This method can be useful in developing network application that needs to probe a machine network configuration first before starting the main program.
The example program below uses the https://godoc.org/honnef.co/go/netdb package to discover the available network services. It maybe useful to specify either your program is probing for services that use UPD or TCP protocol.
Here you go!
package main
import (
"fmt"
"honnef.co/go/netdb"
)
func main() {
fmt.Println("---------------------------------------")
var proto *netdb.Protoent
var serv *netdb.Servent
// UDP
proto = netdb.GetProtoByName("udp")
fmt.Println("List of UDP services with port number : ")
for portNumber := 25; portNumber <= 80; portNumber++ {
serv = netdb.GetServByPort(portNumber, proto)
if serv != nil {
fmt.Println("Service name : ", serv.Name)
fmt.Println("Aliases for this service : ")
for k, v := range serv.Aliases {
fmt.Println(k, v)
}
fmt.Println("Service is using port number : ", serv.Port)
fmt.Println("######################")
}
}
fmt.Println("---------------------------------------")
// UDP
/* proto := netdb.GetProtoByName("udp")
serv := netdb.GetServByPort(80, proto)
fmt.Println("Service name : ", serv.Name)
fmt.Println("Aliases for this service : ")
for k, v := range serv.Aliases {
fmt.Println(k, v)
}
fmt.Println("Service is using port number : ", serv.Port)
fmt.Println("---------------------------------------") */
// TCP
/* proto2 := netdb.GetProtoByName("tcp")
serv2 := netdb.GetServByPort(80, proto2)
fmt.Println("Service name : ", serv2.Name)
fmt.Println("Aliases for this service : ")
for k, v := range serv2.Aliases {
fmt.Println(k, v)
}
fmt.Println("Service is using port number : ", serv2.Port) */
}
Output:
List of UDP services with port number :
Service name : smtp
Aliases for this service :
Service is using port number : 25
#
Service name : nsw-fe
Aliases for this service :
Service is using port number : 27
#
Service name : msg-icp
Aliases for this service :
Service is using port number : 29
#
Service name : msg-auth
Aliases for this service :
Service is using port number : 31
#
Service name : dsp
Aliases for this service :
Service is using port number : 33
#
Service name : time
Aliases for this service :
Service is using port number : 37
#
Service name : rap
Aliases for this service :
Service is using port number : 38
#
Service name : rlp
Aliases for this service :
Service is using port number : 39
#
Service name : graphics
Aliases for this service :
Service is using port number : 41
#
.....
References:
See also : Golang : Get UDP client IP address and differentiate clients by port number
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+15.3k Golang : How to convert(cast) IP address to string?
+18.3k Golang : Write file with io.WriteString
+13.4k Golang : Query string with space symbol %20 in between
+6k Golang : Scan forex opportunities by Bollinger bands
+10.3k Golang : Select region of interest with mouse click and crop from image
+8k Golang : Oanda bot with Telegram and RSI example
+10.1k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+8.6k Golang : GMail API create and send draft with simple upload attachment example
+21.5k Golang : Use TLS version 1.2 and enforce server security configuration over client
+18.6k Golang : Padding data for encryption and un-padding data for decryption
+10.8k Golang : Simple image viewer with Go-GTK
+4.5k JavaScript: Add marker function on Google Map