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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | #!/usr/bin/perl
use Switch;
sub parsefstabline
{
my $counter = -1;
foreach my $word ( @_ )
{
$counter++;
switch( $counter )
{
case "0"
{
print "<file system> $word\n";
break;
}
case "1"
{
print "<mount point> $word\n";
break;
}
case "2"
{
print "<type> $word\n";
break;
}
case "3"
{
print "<options> $word\n";
break;
}
case "4"
{
print "<dump> $word\n";
break;
}
case "5"
{
print "<pass> $word\n";
break;
}
else
{ break;
}
} # end switch
} # end foreach
} # end parsefstabline()
# -- main ---------------------------#
my $data_file = "/etc/fstab";
open( FILE , "$data_file" ) or die "can't open $data_file ";
foreach my $line ( <FILE> )
{
$ishash = $line =~ m/^#/ ; # skip comments (begin with a # hash symbol)
if( $ishash != 1 )
{
my $isdoubleslash = $line =~ m/^\/\// ; # network shares begin with a double slash
if( $isdoubleslash )
{
my @line_array = split( " " , $line );
parsefstabline( @line_array );
}
}
}
close( FILE );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/perl
use Switch;
use File::Copy;
use strict;
my $target = "\#//";
my $newstring = "//";
open( SOURCE , "</home/ubuntu/test-fstab-source" ) or die "can't open source ";
open( TARGET , ">/home/ubuntu/test-fstab" ) or die "can't open target ";
foreach my $line ( <SOURCE> )
{
my $matchresult = $line =~ m/^($target)/ ;
if( $matchresult )
{
print "\n FOUND \n";
print "$line\n";
$line =~ s/$target/$newstring/g;
print "$line\n";
print TARGET "$line";
}else
{
print TARGET "$line";
}
} # end foreach line in the SOURCE file
close( SOURCE );
close( TARGET );
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/perl
use strict;
use warnings;
use Switch;
# 2012-12-14 johnpfeiffer
# todo more defensive trim chomp statements
use constant PYTHONPROPERTIESFILE => "/tmp/test.py";
# Require Parameters: string=command , string=arguments
sub systemcommand( $$ )
{
my $command = $_[0] ;
my @args = split( " " , $_[1] ) ;
my $result = system $command,@args;
return $result;
}
sub getTimestamp()
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$sec = sprintf( "%2.2d" , $sec );
$min = sprintf( "%2.2d" , $min );
$hour = sprintf( "%2.2d" , $hour );
$mday = sprintf( "%2.2d" , $mday );
$mon = sprintf( "%2.2d" , $mon );
$mon = $mon + 1; # to account for the fact that January starts at 0
$year = $year + 1900;
my $timestamp = "$year-$mon-$mday--$hour-$min-$sec" ;
return $timestamp;
}
sub systemCopySourceToTarget( ;$$ )
{
my( $source,
$target ) = @_;
defined $source or die 'Required parameter "source" not defined';
defined $target or die 'Required parameter "target" not defined';
systemcommand( "/bin/cp" , "-a $source $target" );
}
sub backupFile( ;$ )
{
my( $sourceFile ) = @_; defined $sourceFile or die 'Required parameter "sourceFile" not defined';
my $backupFile = "";
if( ! -e $sourceFile )
{ print "Cannot create a backup, file does not exist.\n";
}else
{ my $timestamp = getTimestamp();
$backupFile = "$sourceFile.$timestamp" ;
systemCopySourceToTarget( $sourceFile , $backupFile );
}
return $backupFile;
}
sub fileExists( ;$ )
{
my( $targetFile ) = @_; defined $targetFile or die 'Required parameter "targetFile" not defined';
my $result = 0;
if( -s $targetFile > 0 )
{ $result = 1;
}
return $result;
}
sub trim( ;$ )
{
my( $string ) = @_; defined $string or die 'Required parameter "string" not defined';
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub stripTrailingSingleQuoteWhiteSpaceNewlines( ;$ )
{
my( $string ) = @_;
defined $string or die 'Required parameter "String" not defined';
chomp( $string );
$string = trim( $string );
if( length( $string ) > 0 )
{
print $string . " " . length( $string ) . "\n";
my $lastCharacter = substr( $string , -1 , 1 );
print "DEBUG LAST: " . $lastCharacter . "\n";
if( $lastCharacter eq "'" )
{
$string = substr( $string , 0 , -1 );
}
}
return $string;
}
sub getPythonConfigFileProperty( ;$$ )
{
my( $identifier,
$sourceFile ) = @_;
defined $identifier or die 'Required parameter "Identifier" not defined';
defined $sourceFile or die 'Required parameter "sourceFile" not defined';
my $property = "UNDEFINED";
my $isHash = "UNDEFINED";
if( ! fileExists( $sourceFile ) )
{
$property = "ERROR: $sourceFile not found";
}elsif( $identifier eq "" ){
$property = "ERROR: cannot replace an empty string \n";
}else{
$identifier = $identifier . "'"; # python config file specific single quote
open( FILE , "$sourceFile" ) or die "can't open $sourceFile ";
foreach my $line ( <FILE> )
{
$isHash = $line =~ m/^#/ ; # skip comments (begin with a # hash symbol)
if( $isHash != 1 )
{
my $matchResult = $line =~ m/^($identifier)/ ;
if( $matchResult )
{
my @tokens = split( $identifier , $line ) ;
shift( @tokens ); # remove the identifier
$property = shift( @tokens );
}
}
}
close( FILE );
}
print "before strip DEBUG: " . $property . "\n";
$property = stripTrailingSingleQuoteWhiteSpaceNewlines( $property );
print "after strip DEBUG2: " . $property . "\n";
return $property;
}
sub setPythonConfigFileProperty( ;$$$ )
{
my( $identifier,
$newValue,
$sourceFile ) = @_;
defined $identifier or die 'Required parameter "Identifier" not defined';
defined $newValue or die 'Required parameter "newValue" not defined';
defined $sourceFile or die 'Required parameter "sourceFile" not defined';
my $result = "ERROR";
chomp( $sourceFile );
if( ! fileExists( $sourceFile ) )
{
$result = "ERROR: $sourceFile not found";
}elsif( $identifier eq "" ){
$result = "ERROR: cannot replace an empty string \n";
}else{
chomp( $identifier );
chomp( $newValue );
$identifier = $identifier . "'"; # python config file specific single quote
$newValue = $newValue . "'"; # python config file specific single quote
my $backup = backupFile( $sourceFile );
open( SOURCE , "<$backup" ) or die "can't open $backup";
open( TARGET , ">$sourceFile" ) or die "can't open $sourceFile ";
foreach my $line ( <SOURCE> )
{
my $match = $line =~ m/^$identifier/ ; # this line starts with the identifier
if( $match )
{ print TARGET $identifier . $newValue . "\n"; #this assumes the old line had a trailing newline
}else
{ print TARGET $line;
}
}
close( SOURCE );
close( TARGET );
$result = "SUCCESS";
}
return $result
}
# TESTING #
# MAIN #
my $password = getPythonConfigFileProperty( "password=" , PYTHONPROPERTIESFILE );
print length( $password ) . " characters = " . $password . "\n";
print "Testing last quote " . stripTrailingSingleQuoteWhiteSpaceNewlines( $password ) . "\n" ;
print "Testing last quote " . stripTrailingSingleQuoteWhiteSpaceNewlines( $password ) . "\n" ;
print "\n\n";
my $result = setPythonConfigFileProperty( "password=" , "'~!@#%^&*()_+" , PYTHONPROPERTIESFILE );
print $result . "\n";
$password = getPythonConfigFileProperty( "password=" , PYTHONPROPERTIESFILE );
print "Testing last quote " . stripTrailingSingleQuoteWhiteSpaceNewlines( $password ) . "\n" ;
print "Testing last quote " . stripTrailingSingleQuoteWhiteSpaceNewlines( $password ) . "\n" ;
print "\n\n";
# TODO: don't require configuring NTP twice
sub auditMenu()
{
print "\nCurrently configured User: " . getPythonConfigFileProperty( "oxygen_id='" , PYTHONPROPERTIESFILE );
print "\n\n 1. Configure Remote RSYSLOG Hostname or IP Address \n";
print " 2. Set the NTP Server to be used with Auditing\n";
print " 3. Configure the Auditing Login Username (must be an Oxygen Administrator) \n";
print " 4. Configure the Auditing Login Password \n";
print " 5. Configure the Auditing API Key \n";
print "\n 9. Return to the main menu \n\n";
print ": ";
my $userInput = "";
my $exitChoice = "9";
while( $userInput ne $exitChoice )
{
$userInput = getUserInput();
switch( $userInput )
{
case "1"
{ configureRemoteRyslog();
last;
}
case "2"
{ configurePythonNTP();
last;
}
case "3"
{ setAuditUserPrompt();
last;
}
case "4"
{ setAuditPasswordPrompt();
last;
}
case "5"
{ setAuditAPIKeyPrompt();
last;
}
case "9"
{ system( "clear" );
last;
}
else
{ displayMenuInputError( $userInput );
last;
}
} # end switch
} # end while loop
} #end AuditMenu()
|