bash 解析服务名称
作为启动脚本的一部分,我需要检查特定的 TCP 服务是否实际启动并运行。通过读取配置文件,我知道目标主机和服务名称是什么。我打算使用 nc 向此服务发送示例请求,因为我知道如何使用它并且它的脚本很好,问题是 nc 无法解析服务名称在我的配置中添加一个端口号...
这让我想到了我的(两部分)问题: 是否有 bash 内置或通用分布式实用程序(可能是 RHEL 5 发行版的一部分)可以将服务名称解析为端口号? 除了 netcat 之外,是否有一个简单的实用程序可以在我的脚本中使用?
As part of a start script I need to check to see if a particular TCP service is actually up and running. By reading a config file I know what the target host and service name is. I intended to use nc to send a sample request to this service, because I know how to use it and it scripts well, the problem is that nc cannot resolve the service name in my config to a port number...
Which brings me to my (2-part) question:
Is there a bash built-in or commonly distributed utility (would be part or a RHEL 5 distro) which can resolve the service name to a port number?
Is there a simple utility other than netcat which I could use in my script instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getent
就是你想要的。例如: getent services ftp 无论如何,
您都需要解析输出,但您有端口。
getent
is what you want.For instance:
getent services ftp
You'll need to parse the output anyway, but you have the port.
您的系统中是否有/etc/services文件?可能每个可移植程序(包括
nc
、getent
等)都使用getservbyname(3)
来解析给定服务名称的端口号。反过来,该函数从/etc/services
文件读取数据。Is there /etc/services file in your system? Probably every portable program (including
nc
,getent
and many others) usesgetservbyname(3)
to resolve port number for the given service name. In turn, that function reads the data from/etc/services
file.