📄 ch12.htm
字号:
4 #<BR>
5 setprotoent(1);<BR>
6 while (($name, $aliases, $protonum) = getprotoent) {<BR>
7 print " Name=$name,
Aliases=$aliases, Protocol=$protonum \n";<BR>
8 }<BR>
9 endprotoent;</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The output should be similar to what is shown here:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Name=ip, Aliases=IP, Protocol=0<BR>
Name=icmp, Aliases=ICMP, Protocol=1<BR>
Name=igmp, Aliases=IGMP, Protocol=2<BR>
Name=ggp, Aliases=GGP, Protocol=3<BR>
Name=tcp, Aliases=TCP, Protocol=6<BR>
Name=pup, Aliases=PUP, Protocol=12<BR>
Name=udp, Aliases=UDP, Protocol=17<BR>
Name=idp, Aliases=IDP, Protocol=22<BR>
Name=raw, Aliases=RAW, Protocol=255</FONT></TT>
</BLOCKQUOTE>
<P>
To keep the file open between successive calls to the <TT><FONT FACE="Courier">getprotoent()</FONT></TT>
call, you should call the <TT><FONT FACE="Courier">setprotoent()</FONT></TT>
function with a nonzero parameter. To stop querying the file,
use the <TT><FONT FACE="Courier">endprotoent()</FONT></TT> call.
<P>
To determine whether you have a specific protocol present, you
can use the system call <TT><FONT FACE="Courier">getprotobyname</FONT></TT>
or <TT><FONT FACE="Courier">getprotobynumber</FONT></TT>. A return
value of <TT><FONT FACE="Courier">NULL</FONT></TT> indicates that
the protocol is not there. The name passed to the function is
not case-sensitive. Therefore, to list the names, aliases, and
the protocol number for TCP, you can use this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">if (($name, $aliases, $protonum)
= getprotobyname('tcp')) {<BR>
print "\n Name=$name, Aliases=$aliases,
Protocol=$protonum";<BR>
}<BR>
print "\n"</FONT></TT>
</BLOCKQUOTE>
<P>
A comparable set of calls is available for determining what services
are available for your machine. This call queries the <TT><FONT FACE="Courier">/etc/services</FONT></TT>
file. Listing 12.2 illustrates how to use these calls. The <TT><FONT FACE="Courier">setservent</FONT></TT>
call with a nonzero file rewinds the index into the services file
for you, the <TT><FONT FACE="Courier">getservent</FONT></TT> gets
the four items in the service entry, and the <TT><FONT FACE="Courier">endservent</FONT></TT>
call terminates the lookup. The output from this file can be a
bit lengthy and is shown in Listing 12.2 starting at line 15.
<P>
In Listing 12.2, lines 1 and 2 clear the screen and show the output
of the <TT><FONT FACE="Courier">showme.pl</FONT></TT> file with
the script in it. At line 13, we execute this script. Your output
may be different than the one shown in Listing 12.2 depending
on what services you have installed on your system.
<HR>
<BLOCKQUOTE>
<B>Listing 12.2. Listing server services.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 $ <B>clear<BR>
</B> 2 $ <B>cat showme.pl<BR>
</B> 3 #!/usr/bin/perl<BR>
4 setservent(1);<BR>
5 printf "%15s %15s %4s %15s\n",<BR>
6 "Name","Aliases","Port","Protocol";
<BR>
7 while(($nm,$al,$pt,$pr) = getservent) {<BR>
8 # print "Name=$nm,
Aliases=$al, Port=$pt, $Protocol=$pr\n";<BR>
9 printf "%15s %15s %4d
%15s\n", $nm,$al,$pt,$pr;<BR>
10 }<BR>
11 endservent;<BR>
12 $<BR>
13 $<BR>
14 $ showme.pl<BR>
15 Name Aliases Port  
;Protocol
<BR>
16 tcpmux
1
tcp<BR>
17 echo
7
tcp<BR>
18 echo
7
udp<BR>
19 discard
sink null
9
tcp<BR>
20 discard
sink null
9
udp<BR>
21 systat
users 11
tcp<BR>
22 daytime 13  
;
tcp<BR>
23 daytime 13  
;
udp<BR>
24 netstat 15  
;
tcp<BR>
25 qotd
quote 17
tcp<BR>
26
msp 18
tcp<BR>
27
msp 18
udp<BR>
28 chargen ttytst
source 19
tcp<BR>
29 chargen ttytst
source 19
udp<BR>
30
ftp 21
tcp<BR>
31 telnet 23
tcp<BR>
32 smtp mail 25 &nbs
p;
tcp<BR>
33 time
timserver 37
tcp<BR>
34 time
timserver 37
udp<BR>
35
rlp resource 39
udp<BR>
36 nameserver name 42 &nbs
p;
tcp<BR>
37
whois nicname 43
tcp<BR>
38 domain nameserver 53 &n
bsp;
tcp<BR>
39 domain nameserver 53 &n
bsp;
udp<BR>
40
mtp 57
tcp<BR>
41 bootps 67
tcp<BR>
42 bootps 67
udp<BR>
43 bootpc 68
tcp<BR>
44 bootpc 68
udp<BR>
45 tftp 69&n
bsp;
udp<BR>
46 gopher 70
tcp<BR>
47 gopher 70
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -