Re: win32 samba shares
From: Keith R. Watson (keith.watson_at_gtri.gatech.edu)
Date: 09/14/04
- Previous message: Mina Naguib: "Re: How do I set the "+DAportable" option??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 14 Sep 2004 17:42:01 +0000 (UTC)
"Bill Kurland" <bill@shakespeare-nyc.com> wrote in
news:cfp15d$hk6$1@reader1.panix.com:
> I have written a perl program that runs under Windows 2000 and it
> would be very useful to be able to move files created by the program
> onto a unix server when the program exits. I thought I might do this
> by mapping a Samba share from the unix box and moving the the files
> over with Win32API::File or system(). But I don't know
> 1) how to be sure what drive letter Windows used to map the remote
> directory, which I guess really comes down to knowing how to ask
> Windows if a particular drive is a network share,
> 2) whether the directory is actually writable - will -W work as
> expected ? 3) how to remount the directory from within perl if it
> isn't available. I've looked without success for a module that does
> this as I expect it is beyond my modest skills.
>
> I would be greatful if anyone could suggest ways to achieve these
> things or perhaps suggest an alternative approach.
>
> Thanks
>
>
Bill,
A Samaba share is writable based on a combination of the Samba setup and
the underlying UNIX file system permissions and/or ACLs. This can make it
difficult to determine if the drive is writable with -w or -W. A simple
way to test it is to attempt creation of a dummy file.
I have included four example scripts. They do not test for all possible
error conditions but they will give you a starting point.
MapDrive.pl
DisconnectDrive.pl
ShowNetworkDrives.pl
IsDriveWritable.pl
#!c:\perl\bin\perl.exe -w
# File: MapDrive.pl
#
# Author: Keith R. Watson
# keith.watson@gtri.gatech.edu
#
# Date Last Modified: 9/14/2004 1245
#
# Abstract: Demonstrates how to map an SMB share from Perl.
use strict;
use Win32::NetResource;
my $usage = <<USAGE;
Usage:
MapDrive.pl <user's password>
If the user's password contains spaces
surround it with quotes.
MapDrive.pl "some password"
USAGE
if ($#ARGV != 0) {
die ("\n\nInvalid number of objects on command line.\n\n", $usage);
}
my $pw = $ARGV[0];
my $userid = 'kw3';
my $drive = 'x:';
my $unc = '\\\\aist074\\c$';
my $persistence = 1;
my %NetResource = (
'Scope' => RESOURCE_CONNECTED,
'Type' => RESOURCETYPE_DISK,
'DisplayType' => RESOURCEDISPLAYTYPE_GENERIC,
'Usage' => RESOURCEUSAGE_CONNECTABLE,
'LocalName' => $drive,
'RemoteName' => $unc
);
if (Win32::NetResource::AddConnection (
\%NetResource,
$pw, $userid,
$persistence
)) {
print ("\n\nDrive ", $drive, " mapped successfully\n\n");
} else {
my $error = undef;
Win32::NetResource::GetError ($error);
my @message = (
"\n\nSystem error ",
$error,
" has occurred:\n\n",
Win32::FormatMessage ($error),
"\n\n"
);
print (@message);
}
#!c:\perl\bin\perl.exe -w
# File: DisconnectDrive.pl
#
# Author: Keith R. Watson
# keith.watson@gtri.gatech.edu
#
# Date Last Modified: 9/14/2004 1302
#
# Abstract: Demonstrates how to disconnect an SMB share from Perl.
use strict;
use Win32::NetResource;
my $drive = 'x:';
if (Win32::NetResource::CancelConnection ("$drive",1,0)) {
print ("\n\nDrive ", $drive, " disconnected successfully.\n\n");
} else {
my $error = undef;
Win32::NetResource::GetError ($error);
my @message = (
"\n\nSystem error ",
$error,
" has occurred:\n\n",
Win32::FormatMessage ($error),
"\n\n"
);
print (@message);
}
#!c:\perl\bin\perl.exe -w
# File: ShowNetworkDrives.pl
#
# Author: Keith R. Watson
# keith.watson@gtri.gatech.edu
#
# Date Last Modified: 9/14/2004 1320
#
# Abstract: Demonstrates how to find mapped newtwork drives
# in Windows.
use strict;
use Win32API::File;
my @drives = Win32API::File::getLogicalDrives();
my $drive;
my @net_drives;
foreach my $drive (@drives) {
my $drive_type = Win32API::File::GetDriveType($drive);
if ($drive_type == 4) {
push (@net_drives, $drive);
}
}
if ($#net_drives >= 0) {
print ("\n\nThe following network drive(s) were found:\n\n");
foreach $drive (@net_drives) {
print (' ', $drive, "\n");
}
} else {
print ("\n\nDidn't find any network drives.\n\n");
}
#!c:\perl\bin\perl.exe -w
# File: IsDriveWritable.pl
#
# Author: Keith R. Watson
# keith.watson@gtri.gatech.edu
#
# Date Last Modified: 9/14/2004 1328
#
# Abstract: Demonstrates a way to test if a drive
# is writable.
use strict;
my $drive = 'C:\\';
my $file = $drive . time ();
if (open (TEST, ">$file")) {
close (TEST);
# unlink ($file);
print ("\n\nDrive ", $drive, " is writable.\n\n");
} else {
print ("\n\nDrive ", $drive, " is not writable.\n\n");
}
-- ------------- Keith R. Watson GTRI/ISD Systems Support Specialist III Georgia Tech Research Institute keith.watson@gtri.gatech.edu Atlanta, GA 30332-0816 404-894-0836
- Previous message: Mina Naguib: "Re: How do I set the "+DAportable" option??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|