I have a Perl script that works well on Windows, however one function requires the use of a .netrc
file for retrieving credentials.
Is there a Windows equivalent of .netrc
, or other solution, on Windows?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityWindows should be identical, except for the fact that your home directory is in a different location.
Try looking in the %userprofile%
directory for .netrc
.
If it's missing you can create it in Explorer by creating a new file named .netrc.
(the final .
will disappear).
You may need to create an environmental variable named HOME
that points to %USERPROFILE%
, like so: setx HOME %USERPROFILE%
.netrc
by default doesn't exist under Windows, and would need to be created especially for this script. Putting it in %userprofile%
would fail to live up to the Windows convention, which has per-user configuration files in c:\Users\<Username>\AppData\...
. See my answer for more.
Jul 15, 2013 at 18:46
~/.netrc
was just imported from Unix. (Windows has a completely different credential storage mechanism anyway.) And no, such files don't exist by default on Unix either.
Jul 15, 2013 at 18:58
copy nul .netrc
) or by entering the name with double quotes (".netrc"
) in Notepad's "Save" dialog.
Jul 15, 2013 at 19:00
On Windows OS open cmd.exe
, type
echo %HOME%
and then
echo %USERPROFILE%
Output should be the same. If not do this
setx HOME %USERPROFILE%
Afterwards create _netrc
file inside %USERPROFILE%
directory and add
machine <hostname1>
login <login1>
password <password1>
Example here
Important! on Windows you should create
_netrc
instead of.netrc
More on this you can read here
You could check for whether you're running on Windows, and if so, inquire of a separate configuration file, rather than looking for .netrc
:
my $config_file = '';
if ($^O eq 'MSWin32') {
$config_file = 'c:\\path\\to\\config.ini';
}
else {
$config_file = '~/.netrc';
};
The .netrc
file by default lives in a user's $HOME
, whose equivalent on the Windows platform is c:\{Users|Documents and Settings}\<Username>
-- 'Users' for Windows Vista and newer, 'Documents and Settings' for XP and older. You could create a .netrc
file there and populate it with the necessary credentials, which would require fewer (or no) changes in your code -- however, it fails to live up to Windows' conventions, which would have configuration files in C:\Users\<Username>\AppData\Local\<Developer>\<Program>\
or similar. It's up to you whether you'd rather modify your code to meet the convention, or just look in the user profile base directory for convenience.
Of course, if you're running the script under Cygwin Perl, you can just create ~/.netrc
for the user account, populate it, and have done.
.netrc
in C:\Users\<Username>
, and see if the script can find it from there. If it doesn't, you'll probably need to dig into the code to find out where and how it's looking for the file, and either modify it to do something more sensible or put the .netrc
file wherever the script expects to find it.
Jul 15, 2013 at 18:52