getservent
优质
小牛编辑
133浏览
2023-12-01
描述 (Description)
此函数从服务条目列表中获取下一个条目,返回 -
($ name,$ aliases,$ port_number,$ protocol_name)
此调用遍历/ etc/services文件。
语法 (Syntax)
以下是此函数的简单语法 -
getservent
返回值 (Return Value)
此函数在错误时返回undef,否则在标量上下文中返回服务名称,在错误上返回空列表,否则在列表上下文中返回服务记录(名称,别名,端口号,协议名称)。
例子 (Example)
以下是显示其基本用法的示例代码 -
#!/usr/bin/perl
while(($name, $aliases, $port_number, $protocol_name) = getservent()) {
print "Name = $name\n";
print "Aliases = $aliases\n";
print "Port Number = $port_number\n";
print "Protocol Name = $protocol_name\n";
}
执行上述代码时,会产生以下结果 -
Name = tcpmux
Aliases =
Port Number = 1
Protocol Name = tcp
Name = tcpmux
Aliases =
Port Number = 1
Protocol Name = udp
Name = rje
Aliases =
Port Number = 5
Protocol Name = tcp
Name = rje
Aliases =
Port Number = 5
Protocol Name = udp
.
.
.
Name = iclpv-sc
Aliases =
Port Number = 1390
Protocol Name = tcp
Name = iclpv-sc
Aliases =
Port Number = 1390
Protocol Name = udp
Name = iclpv-sas
Aliases =
Port Number = 1391
Protocol Name = tcp
Name = iclpv-sas
Aliases =
Port Number = 1391
Protocol Name = udp
Na