Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-23-2010
Registered User
 

Join Date: Aug 2007
Location: Dallas, GA
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Trying to do a compare with multiple lines in a file

Hey guys I am having a problem with being able to find unused profiles in a configuration check script I am trying to create for accountability purposes for managing a large number of systems. What I am trying to do is run a script that will look at the raw config data in a file and pull all the profiles entries that aren't being used. The file that I am pulling has a ton of information but what I am trying to do is pull the various profile data and compare it to the virtual servers that I have configured and report the profiles that are not being used at this time.Here is the output of Config Data from that script:


Code:
profile client test1 {
   defaults from clientssl
   key "blank_ssl.key"
   cert "blank_ssl.crt"
   chain "WF-CA-BUNDLE.crt"
   ca file "WF-CA-BUNDLE.crt"
   client cert ca "WF-CA-BUNDLE.crt"
   ciphers "DEFAULT:+DES-CBC-SHA"
   peer cert mode require
   authenticate depth 3
}
profile client test2 {
   defaults from clientssl
   key "blank_ssl.key"
   cert "blank_ssl.crt"
   chain "WF-CA-BUNDLE.crt"
   ca file "WF-CA-BUNDLE.crt"
   client cert ca "WF-CA-BUNDLE.crt"
   ciphers "DEFAULT:+DES-CBC-SHA"
   peer cert mode require
   authenticate depth 3
}
profile client test3 {
   defaults from clientssl
   key "blank_ssl.key"
   cert "blank_ssl.crt"
   chain "WF-CA-BUNDLE.crt"
   ca file "WF-CA-BUNDLE.crt"
   client cert ca "WF-CA-BUNDLE.crt"
   ciphers "DEFAULT:+DES-CBC-SHA"
   peer cert mode require
   authenticate depth 3
}
virtual test_1 {
   limit 1
   destination 1.2.3.4:telnet
   ip protocol tcp
   rules test1_rule
   profiles test1
}
virtual test_2 {
   limit 1
   destination 1.2.3.4:telnet
   ip protocol tcp
   rules test1_rule
   profiles 
	test2
	tcp
}
virtual test_3 {
   limit 1
   destination 1.2.3.4:telnet
   ip protocol tcp
   rules test1_rule
   profiles 
	test2
}

So what I am trying to do is pull all the above profile entries and compare it to the entries in each of the Virtuals so I want it retun that test3 isn't being used. As you can see it can either be on the same line as "profiles ...." or could be the following lines before the "}'

I am using the following method but doesn't work on the lines that follow the "profiles"


Code:
echo "-------------------------------------------------------------------------"
echo "|"
echo "| Checking for orphaned monitors..."
echo "|"
profiles=`grep -i "^profile" ${CONF} | cut -f3 -d" "`
for i in $profiles; do
   profilecheck=`grep "profiles $i" ${CONF}`
   [ "${profilecheck}" != "" ] || echo "|   profile $i may be unused."
done
echo "|"
echo "-------------------------------------------------------------------------"

Any assistance would greatly be appreciated.
Sponsored Links
    #2  
Old 02-23-2010
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,278
Thanks: 9
Thanked 86 Times in 77 Posts
Hi.

Here is a quick hack that produces the desired output using standard commands. Many intermediate files are produced so that you can see the steps of refinement. One nonstandard command is used, cgrep, complex grep:
cgrep home page
which has compiled easily for me in many environments.

Basically, the data file is parsed into two sections, each of which is refined, until the profiles names of each category is placed into separate files. Then either comm or grep may be used.

Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate parsing, sorting, filtering.

# Infrastructure details, environment, commands for forum posts. 
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo ; echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p grep cgrep awk sed tr sort comm
set -o nounset
echo

FILE=${1-data1}

# Remove debris.
rm -f t[1-9]

echo " Sample of $(wc -l <$FILE) lines in data file $FILE:"
head -5 $FILE
echo -----
tail -5 $FILE

echo
echo " Results:"
grep -w profile $FILE |
tee t1 |
cat > defs

cgrep -D +w '^}' virtual $FILE |
tee t2 |
cat > virs

awk '{ print $3 }' defs |
tee t3 |
cat > defs-names

cgrep -D +I2 +w '^}' profile virs |
tee t4 |
sed '/^[ \t]*$/d' |
sort > virs-names

sed 's/profiles//' virs-names |
tee t5 |
tr '\n' ' ' |
tee t6 |
sed -e 's/^ *//' -e 's/[ \t][ \t]*/ /g' |
tee t7 |
tr ' ' '\n' |
tee t8 |
sed '/^[ \t]*$/d' |
sort -u > virs-final

# use:
# comm -13 virs-final defs-names
# if unmatched profiles should not be displayed.

comm -3 virs-final defs-names

exit 0

producing:

Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
GNU grep 2.5.3
cgrep - (local: ~/executable/cgrep May 29 2009 )
GNU Awk 3.1.5
GNU sed version 4.1.5
tr (GNU coreutils) 6.10
sort (GNU coreutils) 6.10
comm (GNU coreutils) 6.10

 Sample of 58 lines in data file data1:
profile client test1 {
   defaults from clientssl
   key "blank_ssl.key"
   cert "blank_ssl.crt"
   chain "WF-CA-BUNDLE.crt"
-----
   rules test1_rule
   profiles 
	test2
}


 Results:
tcp
	test3

There clearly are other solutions, and someone will probably drop by with one soon.

Best wishes ... cheers, drl
Sponsored Links
    #3  
Old 02-23-2010
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,305
Thanks: 77
Thanked 1,085 Times in 991 Posts
You could try if this works out...

Code:
awk '$1=="profile"{A[$3]=1} $1=="profiles"{sub($1,"");p=1} $1=="}"{p=0} p{for(i=1;i<=NF;i++)A[$i]--}
     END{for(i in A)if (A[i]>0) print i}' infile


Code:
test3

    #4  
Old 02-24-2010
Registered User
 

Join Date: Aug 2007
Location: Dallas, GA
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Thx Guys. I was able to tweak your suggestions to use. I appreciate your assistance.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare values in different lines of file s-layer Shell Programming and Scripting 6 10-12-2009 06:56 PM
Grep multiple lines from a file dwgi32 Shell Programming and Scripting 5 06-23-2009 07:24 AM
How do I compare the last two lines of a file? limshady411 Shell Programming and Scripting 2 08-25-2008 01:41 PM
Help! How to compare two lines in a file sabertooth2000 Shell Programming and Scripting 3 04-23-2008 12:26 AM
retrieved multiple lines on multiple places in a file dala Shell Programming and Scripting 8 03-14-2008 02:28 PM



All times are GMT -4. The time now is 12:49 AM.