Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Tie and Filesys::SmbClient

by tweetiepooh (Hermit)
on Mar 07, 2008 at 12:13 UTC ( [id://672748]=perlmeditation: print w/replies, xml ) Need Help??

We recently had need to grab a small file from each of some 100+ boxes (network management devices running NT4 client) to a Perl script running on Solaris. We can't make changes on these clients but the file is available on a network share.

Enter the Filesys::SmbClient module, the documentation of which shows the use of a Tie that would make reading the file much easier. The example however does not pass any parameters like login credentials.

We eventually got the following to work.

#!perl -w use strict; use POSIX; my @args = ( username => 'userid', password => 'passwd', debug => 10 ); my $filename = 'smb://host/directory/file'; local *FD; tie *FD, 'Filesys::SmbClient', $filename, '0666', @args; while (<FD>) {print} close(FD);
We haven't yet used this in anger, it works on a single device and hopefully on all the rest too.

Comment are welcome on any better way to go about this and the program will only need to be run 2-3 times a year.

Replies are listed 'Best First'.
Re: Tie and Filesys::SmbClient
by hipowls (Curate) on Mar 08, 2008 at 02:53 UTC

    It works for me;) I made some changes just to try out the object API and found a bug (I think) in version 3.1 of Filesys::SmbClient. The docs say that the return from read is undef at EOF but it is actually an empty string.

    Personally I'd use a hash for the arguments to emphasize that they are key value pairs. Makes no difference to perl but it will be easier to maintain and you get a warning if the numbers aren't even. The use POSIX isn't required by your example.

    The object oriented approach (which tie uses behind the scenes)

    use strict; use warnings; use Filesys::SmbClient; my %args = ( username => 'account', password => 'secret', workgroup => 'domain', debug => 0 ); my $smb = new Filesys::SmbClient(%args); my $filename = 'smb://server/share/directory/file.name'; my $remote = $smb->open($filename) or die "Can't open $filename: $!\n" +; open my $local, '>', 'file.name' or die "Can't open file.name: $!\n"; while ( defined( my $buffer = $smb->read($remote) ) ) { last if $buffer eq ''; print {$local} $buffer; } $smb->close($fd);
Re: Tie and Filesys::SmbClient
by okram (Monk) on Mar 07, 2008 at 23:37 UTC
    Hi
    tie works like this: "tie VARIABLE,CLASSNAME,LIST".
    The "variable" is your *FD, "classname", correctly, is 'Filesys::SmbClient', and all the other stuff afterwards is exactly what you'd pass to Filesys::SmbClient->new().
    You would basically want to change your code to read:
    local *FD; tie *FD, 'Filesys::SmbClient', @args; open FD, 'smb://host/directory/file' or die "Can't read file:", $!, "\n"; print while (<FD>); close (FD);

    If you skip the part about "using the two or three params open()" (use the right one), this *should* work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://672748]
Approved by Corion
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-23 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found