Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Be done using protocols fileGet host record given its address (Berikutnya)
Fetching network info

Be done using services file

Daftar Isi

  • endservent

    These routines are the same as their counterparts in thesystem C library. In list context, the return values from thevarious get routines are as follows:

    1. ($name,$passwd,$uid,$gid,
    2. $quota,$comment,$gcos,$dir,$shell,$expire) = getpw*
    3. ($name,$passwd,$gid,$members) = getgr*
    4. ($name,$aliases,$addrtype,$length,@addrs) = gethost*
    5. ($name,$aliases,$addrtype,$net) = getnet*
    6. ($name,$aliases,$proto) = getproto*
    7. ($name,$aliases,$port,$proto) = getserv*

    (If the entry doesn't exist you get an empty list.)

    The exact meaning of the $gcos field varies but it usually containsthe real name of the user (as opposed to the login name) and otherinformation pertaining to the user. Beware, however, that in manysystem users are able to change this information and therefore itcannot be trusted and therefore the $gcos is tainted (seeperlsec). The $passwd and $shell, user's encrypted password andlogin shell, are also tainted, for the same reason.

    In scalar context, you get the name, unless the function was alookup by name, in which case you get the other thing, whatever it is.(If the entry doesn't exist you get the undefined value.) For example:

    1. $uid = getpwnam($name);
    2. $name = getpwuid($num);
    3. $name = getpwent();
    4. $gid = getgrnam($name);
    5. $name = getgrgid($num);
    6. $name = getgrent();
    7. #etc.

    In getpw*() the fields $quota, $comment, and $expire are specialin that they are unsupported on many systems. If the$quota is unsupported, it is an empty scalar. If it is supported, itusually encodes the disk quota. If the $comment field is unsupported,it is an empty scalar. If it is supported it usually encodes someadministrative comment about the user. In some systems the $quotafield may be $change or $age, fields that have to do with passwordaging. In some systems the $comment field may be $class. The $expirefield, if present, encodes the expiration period of the account or thepassword. For the availability and the exact meaning of these fieldsin your system, please consult getpwnam(3) and your system's pwd.h file. You can also find out from within Perl what your$quota and $comment fields mean and whether you have the $expire fieldby using the Config module and the values d_pwquota, d_pwage,d_pwchange, d_pwcomment, and d_pwexpire. Shadow passwordfiles are supported only if your vendor has implemented them in theintuitive fashion that calling the regular C library routines gets theshadow versions if you're running under privilege or if there existsthe shadow(3) functions as found in System V (this includes Solarisand Linux). Those systems that implement a proprietary shadow passwordfacility are unlikely to be supported.

    The $members value returned by getgr*() is a space-separated list ofthe login names of the members of the group.

    For the gethost*() functions, if the h_errno variable is supported inC, it will be returned to you via $? if the function call fails. The@addrs value returned by a successful call is a list of rawaddresses returned by the corresponding library call. In theInternet domain, each address is four bytes long; you can unpack itby saying something like:

    1. ($a,$b,$c,$d) = unpack('W4',$addr[0]);

    The Socket library makes this slightly easier:

    1. use Socket;
    2. $iaddr = inet_aton("127.1"); # or whatever address
    3. $name = gethostbyaddr($iaddr, AF_INET);
    4. # or going the other way
    5. $straddr = inet_ntoa($iaddr);

    In the opposite way, to resolve a hostname to the IP addressyou can write this:

    1. use Socket;
    2. $packed_ip = gethostbyname("www.perl.org");
    3. if (defined $packed_ip) {
    4. $ip_address = inet_ntoa($packed_ip);
    5. }

    Make sure gethostbyname() is called in SCALAR context and thatits return value is checked for definedness.

    The getprotobynumber function, even though it only takes one argument,has the precedence of a list operator, so beware:

    1. getprotobynumber $number eq 'icmp' # WRONG
    2. getprotobynumber($number eq 'icmp') # actually means this
    3. getprotobynumber($number) eq 'icmp' # better this way

    If you get tired of remembering which element of the return listcontains which return value, by-name interfaces are providedin standard modules: File::stat, Net::hostent, Net::netent,Net::protoent, Net::servent, Time::gmtime, Time::localtime,and User::grent. These override the normal built-ins, supplyingversions that return objects with the appropriate namesfor each field. For example:

    1. use File::stat;
    2. use User::pwent;
    3. $is_his = (stat($filename)->uid == pwent($whoever)->uid);

    Even though it looks as though they're the same method calls (uid),they aren't, because a File::stat object is different froma User::pwent object.

    Portability issues: getpwnam in perlport to endservent in perlport.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Be done using protocols fileGet host record given its address (Berikutnya)