1

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?

3 Answers 3

4

Windows 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%

10
  • 2
    .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
  • I actually tried to create one, but it's not a valid filename. Jul 15, 2013 at 18:51
  • 1
    you actually can have a file named that... windows is just dumb and thinks the .netrc is the extension without a filename. Try naming the file ".netrc." (with a dot at the end as well as the beginning)
    – TheCompWiz
    Jul 15, 2013 at 18:57
  • 1
    @AaronMiller: It's not supposed to live up to the Windows convention anyway; the function that looks for ~/.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
  • 2
    @Louis: Either create it via command line (e.g. copy nul .netrc) or by entering the name with double quotes (".netrc") in Notepad's "Save" dialog. Jul 15, 2013 at 19:00
1

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

0

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.

3
  • Sorry, I should have mentioned that it's not my script (and it's over 600KB in size). Jul 15, 2013 at 18:50
  • 2
    Well, in that case, you may find it simplest just to create a correctly formatted .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
  • Or the filename should be _netrc? Aug 28, 2018 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .