Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return list of files that do not contain a given string

Status
Not open for further replies.

tmf33uk

Technical User
Aug 23, 2007
21
GB
Hi all!

I include below my script and the files involved.

Currently, it prints the sqlnet.ora files that contain SQLNET.AUTHENTICATION_SERVICES set to something other than (NONE).

However, I need to add a condition that includes in this list files that don´t contain this parameter as well, but I can´t think of a way to check this without going line by line and erroring for each line that doesn´t contain it (which isn´t what I want).

Can anybody help me? This is probably easy for you gurus, and I´m learning a lot from this forum, but I´m kinda stuck now. :eek:)

Thanks in advance,
T.


My script:
=================================================
#!/usr/bin/perl

my $oratab='/home/tmillan/oratab';

open(BIN, "<$oratab\n") or die "Couldn't open file for reading: $!n";
while (<BIN>) {
s/#.*//; #ignore comments
next if /^(\s)*$/; #ignore blank lines
chomp;
my ($orahome) = (split /:/)[1];
push @homes, "$orahome" ;
}
close BIN;

foreach (@homes) {
my $sqlnet="$_/network/admin/sqlnet.ora";
# print "$sqlnet\n";
open (BIN2, "<$sqlnet\n") or die "cant open $sqlnet: $!";
while (<BIN2>) {
s/#*//; #ignore comments
next if /^(\s)+$/; #ignore blanks
chomp;
if ($_ =~ /^SQLNET\.AUTHENTICATION_SERVICES/i) {
(undef, $test) = split /\=/;
if ($test eq '(NONE)') {
print "good: SQLNET.AUTHENTICATION_SERVICES is set to (NONE) in $sqlnet.\n";
}
else {
push (@baddies, "$sqlnet contains $_, ");
}
}
}
close BIN2;
}

print "emrep: Change the value of SQLNET.AUTHENTICATION_SEVICES to (NONE) in the following configuration files to prevent SYS connections that do not require a password: @baddies\n";

exit;
====================================================

/home/tmillan/oratab:



# This file is used by ORACLE utilities. It is created by root.sh
# and updated by the Database Configuration Assistant when creating
# a database.

# A colon, ':', is used as the field terminator. A new line terminates
# the entry. Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
# $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively. The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
HELIO:/home/oracle10/oracle/product/10.2.0/db_1:Y
TUNEZ:/oracle/product/11g:N

================================================
/oracle/product/10.2.0/db_1/network/admin/sqlnet.ora:
==================================================
# sqlnet.ora Network Configuration File: /u02/db102/network/admin/sqlnet.ora
# Generated by Oracle configuration tools.

SQLNET.AUTHENTICATION_SERVICES=(baddie1)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)

=================================================
/oracle/product/11g/network/admin:
===============================================

# sqlnet.ora Network Configuration File: /u02/db102/network/admin/sqlnet.ora
# Generated by Oracle configuration tools.

SQLNET.AUTHENTICATION_SERVICES=(baddie2)
NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
 
Ok, regarding the same script, I´ve noticed that if the sqlnet.ora file doesn´t exist in the expected directory, the script fails and does not print the string starting with "em_result". How can I handle this (other than creating an empty sqlnet.ora file in each of the expected directories)?

It would either need to ignore any missing sqlnet.ora files and focus on the results we do have, or (preferably) somehow include a message in the em_result string notifying of missing sqlnet.ora files. It needs to be in that string because the Oracle product this will be executed from evaluates the string starting with em_result in order to determine whether an alert should be sent or not.

Thanks again!

T.
 
What about using the opposite condition to "if"...

Maybe use "unless"

Code:
unless ($_ =~ /^SQLNET\.AUTHENTICATION_SERVICES/i) {
print "files that don't contain parameter";
}
 
Before your second open statement, insert
Code:
next unless (-e $sqlnet);
to get rid of the problem of trying to open non-existent files.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks Steve.

1. The line

next unless (-e $sqlnet);

makes it work even if one of the files is missing. Thanks a lot!

2. Regarding adding

unless ($_ =~ /^SQLNET\.AUTHENTICATION_SERVICES/i) {

to identify the files that don´t contain that string doesn´t do what I need it to do, because this ($_) checks each line of the open file. It would meet the criteria many times for each file, instead of a maximum of once per file.

T.
 
foreach (@homes) {
my $good = "0";
my $sqlnet="$_/network/admin/sqlnet.ora";
# print "$sqlnet\n";
next unless (-e $sqlnet);
open (BIN2, "<$sqlnet\n") or die "cant open $sqlnet: $!";
while (<BIN2>) {
s/#*//; #ignore comments
next if /^(\s)+$/; #ignore blanks
chomp;
if ($_ =~ /^SQLNET\.AUTHENTICATION_SERVICES/i) {
(undef, $test) = split /\=/;
if ($test eq '(NONE)') {
print "good: SQLNET.AUTHENTICATION_SERVICES is set to (NONE) in $sqlnet.\n";
$good = "1";
}
else {
push (@baddies, "$sqlnet contains $_, ");
}
}
}
close BIN2;


if ($good == 0) {
print "$sqlnet is a BAD file\n";
}
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top