########################## ### dat file ### ########################## ### ### ;----------------------; ### ; netmount config file ; ### ;------------------------------------------------------; ### ; Welcome to Justin Lee's netmount config file. ; ### ; Basically it's comma separated values. ; ### ; Field 1: letter to map to ; ### ; Field 2: computer that the share is on ; ### ; Field 3: share name ; ### ; Field 4: who gets it, "users" will map to everyone ; ### ; Special Field 1: lpt1-3, connects to shared printers ; ### ; ; ### ; As you can tell `;' is the comment character ; ### ; Also, white space BEFORE and AFTER the comma is not ; ### ; included as data. ; ### ;------------------------------------------------------; ### ### ;[letter],[computer],[-----------share-name---------],[mapped-to] ### b, calu, business, administrators ### ### u, gis1, gis_projects, power users ### v, gis2, gis_projects, power users ### w, gis3, gis_projects, power users ### x, lyca, gis_projects, power users ### s, maca, gis_projects, power users ### z, maca, gis_in_basket, power users ### y, uram, gis_projects, power users ### ### l, calu, longterm_work, users ### m, calu, My Mail, users ; this one needs edited to "my_mail" ### t, calu, temp_crew_files, users ### n, lyca, image_depot, users ### q, lyca, managed_data_depot, users ### r, lyca, raw_data_depot, users ### o, thar, outputs, users ### i, thar, info_and_standards, users ### k, thar, key_data_entry, users ### p, vuvu, public_ftp, users ### ### ; letters used : -b------i-klmnopqrstuvwxyz ### ; system letters : a-cde--------------------- ### ; letters left : -----fgh-j---------------- ### ; Note: Most computers have CD-ROM drives, so they'll take up letters too. ### ### ; printers, note they only go from lpt1-lpt3 ### lpt1, saco, canon mp730 printer, users ### lpt2, saco, minolta-qms pagepro 1250e pcl5e, users ### lpt3, saco, epson stylus photo r1800, users ### ########################## ### script ### ########################## ############################################################################## # # # ########################################################################## # # # 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-RC9 (1.4.9) # # # # Date : Monday, February 18, 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/old-licenses/gpl-2.0.html > # # # ########################################################################## # # # ############################################################################## use strict; use File::Basename; #use warnings; use Getopt::Std; my %opt; getopts('hwpmPf:',\%opt) or die; help() if $opt{h}; # hide the window with Win32::GUI if (defined($opt{w})) { require Win32::GUI; my $root = Win32::GUI::GetPerlWindow(); Win32::GUI::Hide($root); } 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)); } # show the window if hidden with Win32::GUI if (defined($opt{w})) { my $root = Win32::GUI::GetPerlWindow(); Win32::GUI::Show($root); } 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. -w Uses Win32::GUI to hide the root window. -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; }