forked from ornldaac/gedi_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_netrc.pl
71 lines (49 loc) · 1.81 KB
/
create_netrc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl
use strict;
use warnings;
my $urs = "urs.earthdata.nasa.gov";
my $netrc_file = "$ENV{HOME}/.netrc";
$| = 1;
# Ask user for Earthdata Login username
print "Please enter your Earthdata Login username: ";
chomp(my $uid = <STDIN>);
# Ask user for Earthdata Login password
print "Please enter your Earthdata Login password: ";
system('/bin/stty', '-echo');
chomp(my $passwd = <STDIN>);
system('/bin/stty', 'echo');
print "\n";
# Escape '#' and \ (otherwise wget can get confused)
$passwd =~ s/\\/\\\\/g;
$passwd =~ s/^#/\\#/;
# Check to see if a .netrc file already exists. If it does, need to copy
# across all entries except the URS one (if it exists).
my @netrc = ("machine $urs login $uid password $passwd");
if ( -e $netrc_file ) {
open my $fh1, '<', $netrc_file or die "Could not open existing .netrc file for reading";
chomp(my @lines = <$fh1>);
close $fh1;
foreach (@lines) {
if ( /^\s*machine\s+$urs\s+login\s+([^\s]+)\s+password\s+([^\s]+)/ ) {
# This is the line we will be replacing
}
else {
push @netrc, $_;
}
}
}
# Write the new .netrc file. We use a temporary file first, and only
# rename it if everything else seems to go ok.
open my $fh2, '>', "$netrc_file.tmp" or die "Could not create .netrc file";
foreach (@netrc) {
print $fh2 "$_\n";
}
close $fh2;
# Move the existing .netrc file (we save a copy just in case)
if ( -e $netrc_file ) {
unlink "$netrc_file.old" if -e "$netrc_file.old";
rename $netrc_file,"$netrc_file.old";
}
rename "$netrc_file.tmp", "$netrc_file";
print "Your .netrc file has been created\n";
exit(0);