首页 > 解决方案 > 给定一个端口,在 Golang 中返回相应的服务(例如 http、ssh、...)

问题描述

net我在 go中玩弄包裹。虽然我看到了许多有用的 Lookup* 函数,但似乎它们中的任何一个都没有返回给定端口使用的服务。

例如(这纯粹是编造的):

service, err := net.LookUpService(23)
// service is ssh

我看到这实际上可以在节点中实现,dns.lookupService其中实际提供的回调被提供服务。go 中有类似的可能吗?我发现在给定服务和网络(例如 tcp、udp)的情况下,它会返回端口,但我有点想要相反的结果。我希望这是有道理的。

标签: godnsport

解决方案


在包netdb中查看https://godoc.org/honnef.co/go/netdb

包 netdb 为 netdb.h 中定义的 protoent 和 servent 结构提供 Go 接口

通过解析 /etc/protocols 和 /etc/services 使用纯 Go 实现

特别是:

函数 GetServByPort

func GetServByPort(port int, 协议 *Protoent) *Servent

GetServByPort 返回给定端口号和协议的 Servent。如果协议为 nil,则返回与端口号匹配的第一个服务。


推荐阅读