Book HomeJava and XSLTSearch this book

18.3. FTP Configuration with Net::Netrc

Unix-based FTP clients use a file called .netrc, which you can configure to automate FTP access to sites you frequently visit. With a properly defined .netrc file, you can simply execute the FTP command to a favorite FTP host and be automatically logged in to the FTP server. Your .netrc file contains one line for each connection you want to make. The following tokens can be specified for each entry:

machine name
Specifies a remote machine to which you want to autologin:

machine remotehost.com

Instead of machine name, you can specify the single word default to match any machine name. This is usually used for anonymous logins to machines not listed in .netrc.

login name
If present, identifies the user who logs in to the remote machine, in which name is the login name.

password passwd
If present, gives the password for the user. The autologin process uses the specified password if the remote server requires one.

account acctpw
If present, gives an additional password for the user. The autologin process uses this password if the remote server requires one.

macdef name
If present, defines a macro with the specified name. Net::Netrc simply parses this field, to maintain compatibility with FTP.

Here's an example of a typical .netrc entry:

machine remotehost.com login username password userpasswd

Entering your username and password for remote sites in unencrypted form has serious security implications. Many sites consider .netrc files a violation of security policy and do not allow them. In addition, most FTP clients require that the .netrc file be owned by you and readable by only you, with permissions set to 0400 or 0600. If the permissions aren't correctly set, the autologin process aborts. Net::Netrc follows this security convention—if the permissions and ownership are not correct, the .netrc file isn't read, and a warning message is issued.

Net::Netrc implements a simple interface to the .netrc file, telling you which hosts you can automatically connect to. It doesn't actually connect to any remote hosts; it's simply used to query your configuration file and return the value of the tokens.

The following methods are supplied by Net::Netrc.

lookup

$machine = Net::Netrc->lookup(host[, login])

Constructor. Looks up and returns a reference to the entry for the specified host machine host. Without the optional login argument, lookup returns the first entry in the .netrc file for the machine.

If there is no matching entry, lookup returns a reference to the default entry.

account

$machine->account

Returns account information specified by the .netrc account entry, if there is any.

login

$machine->login

Returns the username (login ID) specified by the .netrc login token.

lpa

(@login_pass_account) = $machine->lpa

Returns a list of login, password, and account information specified by the .netrc tokens.

password

$machine->password

Returns the password specified by the .netrc password token.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.