R
Ron Eggler
Hi,
I would like to open a file for appending content and create it if it
doesn't exist. I have been trying to do all I can but it just wouldn't open
it. My code looks like:
@files = </usr/share/NovaxTSP/logs/*>; #path where the log files are saved
my $key_indicator="----Public key----"; #string that specifies that there
is an ssh public key in the next lines and it will need to be appended to
~/.ssh/authorized_keys
my $key_file="~/.ssh/authorized_keys"; #path the the authorized-keys file
foreach $file (@files) { # read out the directory content
print "parse file ".$file.":\n";
file_parser($file);
}
sub file_parser
{
$file = $_[0]; # get 1st parameter (filename)
open(INFO, $file); #opens file systemstats
@lines = <INFO>; #assigns lines to array
$num=0;
foreach $line (@lines){ #go through each line in file
$num++;
print " ".$num ." - ". $line;
if(rindex($line,$key_indicator)>-1){
print "Dude, append the rest of the file to ~/.ssh/authorized_keys
file\n"; #ah, there's an ssh key, the rest of the file will need to be
appended to ~/.ssh/authorized_keys
sysopen (KEY, ">>$key_file", 0644);
print $lines[$num+1]."\n";
print KEY $lines[$num+1];
print $lines[$num+2]."\n";
print KEY $lines[$num+2];
close(KEY);
}
}
}
I appreciate any kind of help!
Thank you so much!
I would like to open a file for appending content and create it if it
doesn't exist. I have been trying to do all I can but it just wouldn't open
it. My code looks like:
@files = </usr/share/NovaxTSP/logs/*>; #path where the log files are saved
my $key_indicator="----Public key----"; #string that specifies that there
is an ssh public key in the next lines and it will need to be appended to
~/.ssh/authorized_keys
my $key_file="~/.ssh/authorized_keys"; #path the the authorized-keys file
foreach $file (@files) { # read out the directory content
print "parse file ".$file.":\n";
file_parser($file);
}
sub file_parser
{
$file = $_[0]; # get 1st parameter (filename)
open(INFO, $file); #opens file systemstats
@lines = <INFO>; #assigns lines to array
$num=0;
foreach $line (@lines){ #go through each line in file
$num++;
print " ".$num ." - ". $line;
if(rindex($line,$key_indicator)>-1){
print "Dude, append the rest of the file to ~/.ssh/authorized_keys
file\n"; #ah, there's an ssh key, the rest of the file will need to be
appended to ~/.ssh/authorized_keys
sysopen (KEY, ">>$key_file", 0644);
print $lines[$num+1]."\n";
print KEY $lines[$num+1];
print $lines[$num+2]."\n";
print KEY $lines[$num+2];
close(KEY);
}
}
}
I appreciate any kind of help!
Thank you so much!