############################################################################## # # # ########################################################################## # # # Title : Netmount.pl # # # # Description : Mount's network shares as network drives according to a # # # # user's LocalGroup settings. (win32 only, sorry!) # # # # Coded by : Justin Lee # # # # Version : 1.4-RC11 (1.4.11) # # # # Date : Sunday, March 23, 2008 # # # # License : GNU General Public License v.2 # # # # Copyright (C) 2008 Justin Lee # # # ########################################################################## # # # # ########################################################################## # # # This program is free software; you can redistribute it and/or # # # # modify it under the terms of the GNU General Public License version 2 # # # # as published by the Free Software Foundation. # # # # # # # # This program is distributed in the hope that it will be useful, # # # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # # # GNU General Public License for more details. # # # # # # # # You should have received a copy of the GNU General Public License # # # # along with this program; if not, write to the Free Software # # # # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # # # # 02110-1301, USA. Or see < http://www.gnu.org/licenses >. # # # ########################################################################## # # # ############################################################################## use strict; use File::Basename; #use warnings; use Getopt::Std; my %opt; getopts('hpmPf:',\%opt) or die; help() if $opt{h}; use Win32::NetAdmin qw(LocalGroupIsMember); my %groups; # load settings from getopt or use defaults my $persistent = $opt{P} ? 'YES' : 'NO'; my $data_file = defined($opt{f}) ? $opt{f} : "$ENV{WINDIR}/@{[ split(/\./,basename($0)) ]}[0].dat"; open(DATA,"$data_file") or die "Netmount: (FATAL) Unable to open data file.\n"; while () { # cookbook chomp; s/^\s+//; s/\s+$//; s/\s+;.*//; # ; = comments next unless length; # parse SIMPLE csv (stfu, pc486) my ($drive,$host,$share,$group) = split(/\s*,\s*/,$_); # are we looking for drives only? next if ( not $opt{p} and $drive =~ /^lpt\d$/io ); # are we looking for printers only? next if ( $opt{p} and not $opt{m} and $drive =~ /^\w$/o ); # construct hash of drives to be mapped $groups{$group}->{$drive} = [uc($host),pretty($share)] if $group; } close(DATA) or warn "Netmount: (NONFATAL) Unable to close data file.\n"; # look for and add per-group drives foreach my $key (keys %groups) { # skip 'users', they're in the next loop next if $key eq 'users'; if (LocalGroupIsMember('',$key,$ENV{USERNAME})) { foreach my $drive (keys %{$groups{$key}}) { # creates \\server\share from the [$host,$share] array my $path = '\\\\' . join('\\',@{$groups{$key}->{$drive}}); print "\r\nDelete \u$drive:\n "; system(qq(net use $drive: /DELETE)); print "\r\nMount (/P:$persistent) \u$drive:\n $path\n "; system(qq(net use $drive: "$path" /PERSISTENT:$persistent)); } print "\r\n"; } } # add default drives (users) foreach my $drive (keys %{$groups{'users'}}) { # creates \\server\share from the [$host,$share] array my $path = '\\\\' . join('\\',@{$groups{'users'}->{$drive}}); print "\r\nDelete \u$drive:\n "; system(qq(net use $drive: /DELETE)); print "\r\nMount (/P:$persistent) \u$drive:\n $path\n "; system(qq(net use $drive: "$path" /PERSISTENT:$persistent)); } exit 0; # and why not? ^^ sub pretty { my @words = split(/(_|\s+|:|-)/,shift); my $nocap = 'and|in|the|are'; my $fullc = 'gis|ftp|http|qms'; my @new; foreach my $word (@words) { if ($word =~ /^(?:$nocap)$/io) { push(@new,lc($word)); } elsif ($word =~ /^(?:$fullc)$/io) { push(@new,uc($word)); } else { push(@new,"\u$word"); } } return join('',@new); } sub help { print <<"EOH"; Name : \u@{[ basename($0) ]} Author : Justin Lee Synopsis : $0 [options] Installation : This is meant to simply be copied to $ENV{WINDIR} along with it's .dat file. Make sure that the security is set to be executable by all users (everyone). The netmount.dat will show you how it should be setup, give it a read. Options : -h Prints the help message. -f < X:\\path\\to\\netmount.dat > Specify a specific path to the .dat file. (Not required if the netmount.dat file is in `$ENV{WINDIR}'.) -P This options turns on /PERSISTENT:YES, it is best to use this option if you only want to run this script once. -p Connect to printers instead of folders. -m Used with -p to connect to printers AND folders. Example Usage : To mount printers and folders and have them connect at log-on: $0 -Ppm To temporarily mount folders with an alternate config file: $0 -f "R:\\04_Software\\Scripts\\netmount\\netmount.dat" Example File : ############################################# # # # Example of a netmount.dat file. # # # ############################################# # ; start of netmount.dat # # r,localhost,roms,users # # t,hostname,transit_reports,administrators # # z,maca,gis_in_basket,power users # # lpt1,yuko,canon mp730 printer,users # ############################################# EOH exit 0; }