PERL script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL script help
# 1  
Old 12-12-2005
PERL script help

Hi all

since you all so graciously helped me out last time, here's to hoping you can help me again. What I have is a windows domain with LOTS of security Groups and Distro Lists. Well, i have the group list and members using dsget and dsquery. But what i want to do is read from that file, then run the command and write each group name to a text file in a directory. After that, I want to run a diff (or build my own diff function) and compare the file sizes rather than openeing ever single text file.

here is my code so far..not all of it works..

#Make sure you copy the text file into the same directory you are running
#the script from, otherwise you will get a failure error

$filename = "insert your filename here";

open (DATA,$filename) || die("Could Not Open Your File, Check the path!:");
while ($filename=<DATA>);
chop($filename);
$members=`dsget group $filename -members -expand | dsget -user -display`; $_=~/$CN=(.*),/;
$filename=$1;
open(OUT,">c:\some\output\path\$filename");
print(OUT,$members);
close(DATA);

Can anyone help??

thanks

Joe
# 2  
Old 12-12-2005
I'm not familiar with the shell commands involved, so bear with me.

Quote:
Originally Posted by caddyjoe77
$_=~/$CN=(.*),/;
What are you trying to do with the '$' inside the pattern? If it is a literal character you want to match, you need \$. Otherwise, $ means the end of the string, which by definition doesn't follow by anything else.

Quote:
open(OUT,">c:\some\output\path\$filename");
print(OUT,$members);
You know you need to escape all backslashes, right? i.e. C:\\some\\output\\path

and the correct way to print() to a filehandle is

Code:
print OUT $members;

If doing all these still doesn't help, I guess you should provide more information as to what is not working and give us clues of things you tried.
# 3  
Old 12-13-2005
Well, I have not written a lot of Perl or anything for that matter, but from what i know this is one of the best ones to learn on.

its mostly just syntax errors that I am getting.

I am using primal script to write this in..here are the errors.

syntax error at dsget.pl line 22, near "while $filename"
syntax error at dsget.pl line 25, near "=."
Backslash found where operator expected at dsget.pl line 25, near "*,\"
(Missing operator before \?)
syntax error at dsget.pl line 37, at EOF

here are the changes that I have made and what the new script looks like. Maybe there is a good source somewhere on the net that maybe i can be pointed to??

$filename = "insert your filename here";

open (DATA,$filename) || die("Could Not Open Your File, Check the path!:");
while $filename=<DATA>;
chop($filename); #takes the newline character off
$members=`dsget group $filename -members -expand | dsget -user -display`;
$_=~\$CN=.*,\; #
$filename=$1;
open(OUT,">c:\\some\\output\\path\\$filename");
print OUT $members;
close(DATA);

Ok so basically what i am trying to do is this: I work on a windows domain and we have wayyy to many duplicate memberships in security groups and distrobution groups. I am trying to clean everything up , remove duplicate memberships and the like. You can run the command dsquery and dsget to enumerate the memberships.

So what i thought to do was write a sccript that takes the input of a file name with the LDAP path and feed that into the dsget command and output the results to a text file for each group in the input file.

then run some sort of a diff to compare the filesizes starting with the first file all the way to the end and since the output would be the same in each file (no extra characters) log the differences and compare them rather than looking at each file individually.

I hope that makes sense what i am trying to do. Let me know if it does not and i will try and explain it further.

Anyone confused yet?? :P

thanks!!

Joe
# 4  
Old 12-14-2005
Code:
$filename = "insert your filename here";

 open (DATA,$filename) || die("Could Not Open Your File, Check the path!:");
 while ($filename=<DATA>) {
 chop($filename);			#takes the newline character off
$members=`dsget group $filename -members -expand | dsget -user -display`;
	$_=~/$CN=.*,/;		# <!-- Still I don't know what you want to do here
	$filename=$1;
	open(OUT,">c:\\some\\output\\path\\$filename");
	print OUT $members;
	close(DATA);
}

# 5  
Old 12-14-2005
I almost have it...however the text file i am reading from now has quotes so how do I escape the quotes? So far I have tried \"$filename"\, \\$filename\\, "\$filename\" and I get the same error returned each time.

For Instance, if you were to run this from a windows command shell, it would look like this:
c:\dsget group "CN=People Admins,OU=Groups,OU=My,DC=Domain,DC=dot,DC=com" -members -expand | dsget user -display

I can run this from the command shell, but not through the script. I get the error Target object is missing. So im pretty sure its the way PERL is interpreting the "'s when reading from the file.

the file it is reading from looks like this ""CN=People Admins,OU=Groups,OU=My,DC=Domain,DC=dot,DC=com" line by line.

Here is the script

# Get member list for each group and stuff into file named for group
# Assumes C:\some\path\groups.txt has one DN per line
open(GROUPS,"<c:\\groups.txt") or die ("Can't open C:\\groups.txt");
while(<GROUPS>) {
chop($_);
$_=~/$CN=(.*),/;
$filename=$1;
$members=`dsget group $filename -members -expand | dsget user -display`;
open(OUT,">d:\\crap\\$filename");
print(OUT $members);
close(OUT);
}
close(GROUPS);

# Diff each file and look for identical member lists
# This might not work - if not, get a list of files somehow
@files=`ls c:\some\output\path`;

for($i=0;$i<$#files;$i++) {
for($j=$i+1;$j<$#files;$j++) {
$result=diff($files[$i],$files[$j]);
if($result) { print $result; }
}
}

# You need a working diff subroutine
# This one might work
# Returns a string if both files are identical, undef otherwise
sub diff {
($f1,$f2)=shift;
open(F1,$f1);
open(F2,$f2);
$l1=<F1>;
$l2=<F2>;
while($l1 && $l2) {
if ($l1 eq $l2) {
$l1=<F1>;
$l2=<F2>;
next;
}
return undef;
}

return("$f1 and $f2 are identical");
}

the $_=~/$CN=(.*),/; CN is for the LDAP path. the first CN is the actual group name so I am only looking to take that and everything up to the first comma since LDAP paths are seperated by commas.


hope this helps and clears the confusion.

Last edited by caddyjoe77; 12-14-2005 at 03:40 PM.. Reason: Clarity
# 6  
Old 12-15-2005
Still not getting fully. But do you mean $filename variable content contains quotes, that you want to escape, or you want to add a pair of quotes outside?

If that is escape, you can

$filename =~ s/(['"])/\\$1/g;

to make ' and " inside $filename to be escaped. If you do not mean this, please ask again.
# 7  
Old 12-15-2005
Got It!!! :)

Hey thanks for the help. I was able to get it to work. Here is a copy of the script if anyone would want to use it.


# Get member list for each group and stuff into file named for group
# Assumes C:\some\path\groups.txt has one DN per line

open(GROUPS,"<c:\\groups.txt") or die ("Can't open C:\\groups.txt");
while(<GROUPS>) {
chomp($_);
print "$_\n";
@stuff=split(/"/,$_);
$members=`dsget group $_ -members -expand | dsget user -display`;
open(OUT,">d:\\test\\$stuff[1]");
print(OUT $members);
close(OUT);
}
close(GROUPS);

that will run the query, create the text file for each group object, and enumerate the users in each of those files.

works well

if you want to learn about dsget and dsquery if for some reason you are ever on a windows network, check out www.technet.com and search, there are several whitepapers on the subject.

Thanks again for your help. Sorry i couldnt make it a little clearer Smilie

Joe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Excuting perl script from within a perl script with variables.

Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'. #!/usr/bin/perl open FILE, "upslist.txt"; while ($line=<FILE>){ if ($line=~/^(.*?),(.*?)$/){ #print "ups:$1 string:$2\n"; do 'check_snmp_mgeups-0.1.pl -H $1 -C $2'; } ... (1 Reply)
Discussion started by: mrlayance
1 Replies

3. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

calling a perl script with arguments from a parent perl script

I am trying to run a perl script which needs input arguments from a parent perl script, but doesn't seem to work. Appreciate your help in this regard. From parent.pl $input1=123; $input2=abc; I tried calling it with system("/usr/bin/perl child.pl $input1 $input2"); and `perl... (1 Reply)
Discussion started by: grajp002
1 Replies

6. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Perl: Run perl script in the current process

I have a question regarding running perl in the current process. I shall demonstrate with an example. Look at this. sh-2.05b$ pwd /tmp sh-2.05b$ cat test.sh #! /bin/sh cd /etc sh-2.05b$ ./test.sh sh-2.05b$ pwd /tmp sh-2.05b$ . ./test.sh sh-2.05b$ pwd /etc sh-2.05b$ So... (10 Replies)
Discussion started by: vino
10 Replies
Login or Register to Ask a Question