Andrew Gaffney wrote:
I am writing a Perl script to automatically generate a netlogon.bat file for Samba whenever a user logs onto a domain. The only parameter that is passes to it is the username. My problem is that different groups get some of the same mappings. What I really need to do is filter out duplicate lines in the finished output.
Whenever you want unique values think "hash".
Well, it would have been weird to have a hash with keys named 'NET USE F: \\\\SKYLINE\\SKYLINEF\r\n'.
I tried piping the output through 'uniq' but it only filters successive duplicate lines. Anyone have any suggestions?
Your example does not have 'uniq' (or 'sort -u') in it so I am not sure what you are trying to do.
I mean that I tried 'cat /tmp/user.bat > uniq' after the script had run to see how it would work.
#!/usr/bin/perl
use warnings; use strict;
my $user = shift; my $drives = {F => "NET USE F: \\\\SKYLINE\\SKYLINEF\r\n", H => "NET USE H: \\\\SKYLINE\\SHARE\r\n", I => "NET USE I: \\\\SHIPPING1\\INVENTORY\r\n", M => "NET USE M: \\\\SKYLINE\\SKYLINEM\r\n", S => "NET USE S: \\\\SHIPPING1\\SHOP\r\n", Y => "NET USE Y: \\\\ACCOUNTING\\FLTSCHOOL\r\n", Z => "NET USE Z: \\\\ACCOUNTING\\MAINT\r\n"};
Why not just use a hash instead of a reference to a hash? The use of "\r\n" is non-portable, you should use "\015\012" instead.
I'm not that worried about portability since this was something I threw together for use on MY system with MY particular setup.
my $CRLF = "\015\012";
my %drives = ( F => 'NET USE F: \\SKYLINE\SKYLINEF' . $CRLF, H => 'NET USE H: \\SKYLINE\SHARE' . $CRLF, I => 'NET USE I: \\SHIPPING1\INVENTORY' . $CRLF, M => 'NET USE M: \\SKYLINE\SKYLINEM' . $CRLF, S => 'NET USE S: \\SHIPPING1\SHOP' . $CRLF, Y => 'NET USE Y: \\ACCOUNTING\FLTSCHOOL' . $CRLF, Z => 'NET USE Z: \\ACCOUNTING\MAINT' . $CRLF, );
my $which = {accounting => "F H I M S Y Z", mech => "I M S Z", dispatch => "M", instructors => "M"};
You should probably use a hash of arrays for this (so you don't have to split the string later):
my %which = ( accounting => [ qw(F H I M S Y Z) ], mech => [ qw(I M S Z) ], dispatch => [ qw(M) ], instructors => [ qw(M) ], );
I'll probably change this.
my $groups = `cat /etc/group | grep ${user} | cut -d ':' -f 1`;
Ick, ick, ick! Perl provides built-in functions to access /etc/group and /etc/passwd
perldoc -f getgrnam perldoc -f getgrgid perldoc -f getgrent perldoc -f setgrent perldoc -f endgrent
perldoc -f getpwnam perldoc -f getpwuid perldoc -f getpwent perldoc -f setpwent perldoc -f endpwent
This script wasn't much more than a quick hack, anyway. I'll work on stuff like that later.
$groups =~ s/\n/\:/sg;
# Start generating logon script #open LOGON, ">/usr/local/samba/netlogon/${user}.bat"; open LOGON, ">/tmp/${user}.bat";
You should _ALWAYS_ verify that the file opened correctly.
open LOGON, ">/tmp/$user.bat" or die "Cannot open /tmp/$user.bat: $!";
I agree. I just modified existing code and didn't think about that.
print LOGON "[EMAIL PROTECTED] OFF\r\n";
foreach $group (split /:/, $groups) { foreach $drive (split / /, $which->{$group}) { print LOGON $drives->{$drive}; } }
close LOGON;
John
Thanks for all the suggestions.
-- Andrew Gaffney
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>