📄 ch15.htm
字号:
variable is a list that contains alternate names for the <TT><FONT FACE="Courier">$name</FONT></TT>
network, and each item is separated from the other with spaces.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$addrType</FONT></TT>
value is set to the defined constant <TT><FONT FACE="Courier">&AF_INET</FONT></TT>
for your machine. (You'll need the file <TT><FONT FACE="Courier">socket.ph</FONT></TT>
somewhere in your <TT><FONT FACE="Courier">@Inc</FONT></TT> path.)
<TT><FONT FACE="Courier">$addrtype</FONT></TT> is the address
type; at present, this is always whatever value is defined for
the system constant <TT><FONT FACE="Courier">&AF_INET</FONT></TT>,
which indicates that the address is an Internet address. Usually,
the subroutine <TT><FONT FACE="Courier">&AF_INET</FONT></TT>
is set to return the integer <TT><FONT FACE="Courier">2</FONT></TT>
on UNIX systems.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$inet</FONT></TT>
address is set to the address of this network in the <TT><FONT FACE="Courier">A.B.C.D</FONT></TT>
form for Internet addresses. The A is the class A portion of the
network address; B is for the class B portion; and C for the class
C portion of the address.
</UL>
<P>
Listing 15.4 shows how you can use <TT><FONT FACE="Courier">getnetent</FONT></TT>
to list the machine names and addresses at your site.
<HR>
<BLOCKQUOTE>
<B>Listing 15.4. A program that uses </B><TT><B><FONT FACE="Courier">getnetent</FONT></B></TT><B>.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 #!/usr/bin/perl<BR>
2 print ("Networks on this machine:\n");<BR>
3 while (($name, $aliases, $atype, $inet) = getnetent()) {<BR>
4 ($a,$b,$c,$d)
= unpack ("cccc", $inet);<BR>
5 print "$name
= ";<BR>
6 printf " $ %d %d %d
%d \n",$a,$b,$c,$d;<BR>
7 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Each iteration in the <TT><FONT FACE="Courier">while</FONT></TT>
reads one entry in the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file. If the last entry in the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file has been read, the <TT><FONT FACE="Courier">getnetent</FONT></TT>
function returns an empty list and the <TT><FONT FACE="Courier">while</FONT></TT>
loop terminates. Each non-empty entry read is assigned to the
variables <TT><FONT FACE="Courier">$name</FONT></TT>, <TT><FONT FACE="Courier">$aliases</FONT></TT>,
<TT><FONT FACE="Courier">$atype</FONT></TT>, and <TT><FONT FACE="Courier">$inet</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">getnetbyaddr</FONT></TT> function
returns the next available entry from <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
with a given network number. Here's the syntax for the <TT><FONT FACE="Courier">getnetbyaddr</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">($name, $aliases, $atype, $inet) = getnetbyaddr
($inaddr, $itype);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">getnetbyaddr()</FONT></TT> function
returns the same types of values as the <TT><FONT FACE="Courier">getnetent()</FONT></TT>
function. The input parameters to <TT><FONT FACE="Courier">getnetbyaddr()</FONT></TT>
differ from the <TT><FONT FACE="Courier">getnetent()</FONT></TT>
function. The <TT><FONT FACE="Courier">$inaddr</FONT></TT> is
the network number that you are looking for. The <TT><FONT FACE="Courier">$inaddr</FONT></TT>
value must be a packed four-byte integer whose four bytes are
the <I>A</I>, <I>B</I>, <I>C</I>, and <I>D</I> components of an
Internet address. Use the <TT><FONT FACE="Courier">pack</FONT></TT>
command to create the <TT><FONT FACE="Courier">$inet</FONT></TT>
word:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">@bytes = (204,251,103,2);<BR>
$inaddr = pack ("C4", @bytes);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">itype</FONT></TT> variable is almost
always set to <TT><FONT FACE="Courier">&AF_INET</FONT></TT>
for Perl scripts on UNIX systems.
<P>
The <TT><FONT FACE="Courier">getnetbyname()</FONT></TT> function
is just like the <TT><FONT FACE="Courier">getnetbyaddr()</FONT></TT>
function except that it takes a network name (or alias) instead
of an address. The returned values for an entry in the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file are the same, too. Here's the syntax for the <TT><FONT FACE="Courier">getnetbyname</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">($name, $aliases, $atype, $inet) = getnetbyname
($networkName);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">setnetent</FONT></TT> and <TT><FONT FACE="Courier">endnetent</FONT></TT>
functions in Perl rewind and close the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file for access. The <TT><FONT FACE="Courier">setnetent</FONT></TT>
function rewinds the /<TT><FONT FACE="Courier">etc/networks</FONT></TT>
file. After a call to <TT><FONT FACE="Courier">setnetent()</FONT></TT>,
the next <TT><FONT FACE="Courier">getnetent()</FONT></TT> call
returns the first item in the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file. Here's the syntax for the <TT><FONT FACE="Courier">setnetent</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">setnetent (keepopen);</FONT></TT>
</BLOCKQUOTE>
<P>
If <TT><FONT FACE="Courier">keepopen</FONT></TT> is non-zero,
the <TT><FONT FACE="Courier">/etc/networks</FONT></TT> file is
left open for reading, and any previously cached information about
the file is kept in memory. If <TT><FONT FACE="Courier">keepopen</FONT></TT>
is set to zero, any cached information in memory is flushed and
the file is read again with the first entry available for a call
to <TT><FONT FACE="Courier">getnetent()</FONT></TT>. The <TT><FONT FACE="Courier">endnetent()</FONT></TT>
function accepts no parameters and simply closes the <TT><FONT FACE="Courier">/etc/networks</FONT></TT>
file.
<H3><A NAME="WorkingwithHostNamesUsinggethostbya">Working with
Host Names Using <TT><FONT SIZE=4 FACE="Courier">gethostbyaddr()</FONT></TT><FONT SIZE=4>
Functions</FONT></A></H3>
<P>
The <TT><FONT FACE="Courier">gethostbyaddr()</FONT></TT> function
accesses the <TT><FONT FACE="Courier">/etc/hosts</FONT></TT> file
for the host name given a particular Internet address. Here's
the syntax for the <TT><FONT FACE="Courier">gethostbyaddr</FONT></TT>
function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">($name, $aliases, $addrtype, $len, $addr)
<BR>
= gethostbyaddr ($inaddr, $atype);</FONT></TT>
</BLOCKQUOTE>
<P>
This function needs two arguments:
<UL>
<LI><FONT COLOR=#000000>The Internet address of the host</FONT>
<LI><FONT COLOR=#000000>The address type (which is usually set
to </FONT><TT><FONT FACE="Courier">AF_INET</FONT></TT>)
</UL>
<P>
The Internet address is in the packed form as in the <TT><FONT FACE="Courier">getnetaddr()</FONT></TT>
call. The <TT><FONT FACE="Courier">$inaddr</FONT></TT> value must
be a packed four-byte integer whose four bytes are the <I>A</I>,
<I>B</I>, <I>C</I>, and <I>D</I> components of an Internet address.
Use the <TT><FONT FACE="Courier">pack</FONT></TT> command to create
the <TT><FONT FACE="Courier">$inet</FONT></TT> word:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">@bytes = (204,251,103,2);<BR>
$inaddr = pack ("C4", @bytes);</FONT></TT>
</BLOCKQUOTE>
<P>
The<TT><FONT FACE="Courier"> gethostbyaddr</FONT></TT> function
returns a list with five items in it:
<UL>
<LI><FONT COLOR=#000000>The first item is the host name corresponding
to the Internet address specified by </FONT><TT><FONT FACE="Courier">$inaddr</FONT></TT>.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$aliases</FONT></TT>
variable is assigned the list of aliases or alternative names
by which the host can be referred.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">addrtype</FONT></TT>,
like <TT><FONT FACE="Courier">inaddrtype</FONT></TT>, is always
<TT><FONT FACE="Courier">&AF_INET</FONT></TT>.
<LI><FONT COLOR=#000000>The </FONT><TT><FONT FACE="Courier">$addrs</FONT></TT>
is a list of addresses (the main address and alternatives) corresponding
to the host node named. Each address is stored as a four-byte
integer.
<LI><FONT COLOR=#000000>The variable </FONT><TT><FONT FACE="Courier">len</FONT></TT>
is the length of the <TT><FONT FACE="Courier">addrs</FONT></TT>
field; this length is always four multiplied by the number of
addresses returned in <TT><FONT FACE="Courier">addrs</FONT></TT>.
</UL>
<P>
Listing 15.5 shows how you can use <TT><FONT FACE="Courier">gethostbyaddr</FONT></TT>
to retrieve the Internet address corresponding to a particular
machine name.
<HR>
<BLOCKQUOTE>
<B>Listing 15.5. A program that uses </B><TT><B><FONT FACE="Courier">gethostbyaddr</FONT></B></TT><B>.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 #!/usr/bin/perl<BR>
2<BR>
3 print ("Enter
the Internet address to look for :\n");<BR>
4 $machine = <STDIN>;<BR>
5 $machine =~ s/^\s+|\s+$//g;
# remove whitespaces around it<BR>
6<BR>
7 @bytes = split (/\./,
$machine);<BR>
8<BR>
9 $packaddr = pack
("C4", @bytes);<BR>
10<BR>
11 if (!(($host, $aliases, $addrtype, $len, @addrlist) =<BR>
12 gethostbyaddr
($packaddr, &AF_INET))) {<BR>
13 die ("No
such $machine was found.\n");<BR>
14 }<BR>
15<BR>
16 if ($aliases ne "") { # i.e. you have
more than one alias<BR>
17 print
("$host: Aliases for $host are :\n");<BR>
18 @alternates
= split (/\s+/, $aliases);<BR>
19 for ($i
= 0; $i < @alternates; $i++) {<BR>
20
printf "%d: %s \n",$i, $alternates[$i];<BR>
21 }<BR>
22 }<BR>
23 else {<BR>
24 print " This $host has no aliases ";<BR>
25 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The following is sample output for a machine using the script
called <TT><FONT FACE="Courier">15_6.pl</FONT></TT>:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$ <B>15_6.pl<BR>
</B>Enter the Internet address to look for :<BR>
<B>204.222.245.10<BR>
</B>pop.ikra.comAliases for pop.ikra.com are :<BR>
0: pop<BR>
1: www.ikra.com</FONT></TT>
</BLOCKQUOTE>
<P>
You can get the host information by specifying the name to the
<TT><FONT FACE="Courier">gethostbyname</FONT></TT> function. The
<TT><FONT FACE="Courier">gethostbyname</FONT></TT> function is
like <TT><FONT FACE="Courier">gethostbyaddr</FONT></TT>, except
it uses a name instead of an address. Here's the syntax for the
<TT><FONT FACE="Courier">gethostbyname</FONT></TT> function:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">($name, $aliases, $addrtype, $len, $addr)
<BR>
= gethostbyname ($nameString);</FONT></TT>
</BLOCKQUOTE>
<P>
Here, <TT><FONT FACE="Courier">$nameString</FONT></TT> is the
machine name to look for. The returned values from the <TT><FONT FACE="Courier">gethostbyname</FONT></TT>
function are the same as those for <TT><FONT FACE="Courier">gethostbyaddr</FONT></TT>.
Look at Listing 15.6. The host name entered by the user may have
leading or trailing blanks. These are removed by the statement
on line 5.
<HR>
<BLOCKQUOTE>
<B>Listing 15.6. Using </B><TT><B><FONT FACE="Courier">gethostbyname</FONT></B></TT><B>.
<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 #!/usr/bin/perl<BR>
2<BR>
3 print ("Enter a machine name or Internet site
name:\n");<BR>
4 $machine = <STDIN>;<BR>
5 $machine =~ s/^\s+|\s+$//g;<BR>
6 if (!(($name, $altnames, $addrtype, $len, @addrlist)
=<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -