Hey, first and foremost, I'd like to say hi to make sure you know this is my first post here. So please be gentle with me for now

Now, I recently started working with perl and my first big task was to write up a script to test whether or not I could access a certain Filesystem/Folder/Share and create Files, write into them and read from them.

We are using a Monitoring Tool that runs perl scripts on machines and based on their output generates events that tell us whether or not "something" was successful. What I set out to do, was create a Script, that given a path (-f) a type (-t (local, NFS, Samba/SMB(mounted and via UNC))) a Searchpattern (-s) as well as Username and Password were needed, would create the file whichs path I have provided, write the Searchpattern into it, close the handle, open the file for reading and then would be successful if what I read was equal to our pattern. The Script is also supposed to run on, at least, Windows and Linux.

My first successful script worked well on local files. But it was the easiest and pretty much most basic part. My Priority is now to check if I can access a Samba share from a Windows Machine via a UNC path. My prior research was fruitless and led me to the Filesys::smbclient module. But I am confused in how to install/use it. Installing it on my test-system led to it asking about the libsmbclient.so/h, which, some more research later, was revealed to be part of a Samba suite. I am quite new at all this, so I am certainly a bit overwhelmed here.

Since the script is supposed to handle multiple combinations of OS, Path-Formats and Shares I dont know if I can install Filesys::smbclient everwywhere. I dont know if what I want would be achievable without it, and I am quite unsure about how to proceed here. As I said, I am to prioritize accessing a SMB Share via UNC from a Windows Machine, create a file and write into and read from it.

I came here looking to see if any of you would know how to achieve this goal.

Having received my first reply, I was reminded, that I should state some restrictions I am under while creating this script. First, it will have to work with as few additional Modules as possible. Since we are using a perl distribution that is shipped with another software package, it would be hard to make sure that every machine we want to use it on has, or can, install additional modules. Secondly, I was told to try and find a way to do it, without temporarily mounting the share as a drive. But since I am, as I had remarked earlier, quite new at this, I don't quite know, and can't seem to find information on, how to do that.

use Getopt::Std; shift(@ARGV); &getopts("f:t:s:u:p:"); print "ZAXML-TXT\n"; my $Filepfad; my $Volumetype; my $Username; my $Password; my $Searchpattern; print "${opt_f}"; if ($opt_f) { $Filepfad=$opt_f; } else { print "CURRVALUE_STRING: a File-Path must be given\n"; print "RETURNID: 1\n"; exit 0; } if ($opt_t){ if ($opt_t ne "UNC" && $opt_t ne "local" && $opt_t ne "NFS" && $op +t_t ne "SMB") { print "CURRVALUE_STRING: Filesystem-type must be UNC, local, N +FS or SMB\n"; print "RETURNID: 1\n"; exit 0; } elsif ($opt_t eq "SMB") { $Volumetype=$opt_t; if ($opt_u && $opt_p) { $Username=$opt_u; $Password=$opt_s; } else { print "CURRVALUE_STRING: Username and Password are require +d for access to SMB-Shares\n"; print "RETURNID: 1\n"; exit 0; } } else { $Volumetype=$opt_t; } } else { print "CURRVALUE_STRING: Volumetype must be given\n"; print "RETURNID: 1\n"; exit 0; } if ($opt_u){ $Username=$opt_u; } if ($opt_p){ $Password=$opt_p; } if($opt_s) { $Searchpattern = $opt_s; } else { print "CURRVALUE_STRING: a Searchpattern must be given\n"; print "RETURNID: 1\n"; exit 0; } my $os=$^O; my $last_slash; if ($os eq "MSWin32" && $Volumetype eq "local") { $last_slash = rindex($Filepfad, "\\",); } else { $last_slash = rindex($Filepfad, "/",); } my $Test_Volume=substr($Filepfad,0, $last_slash); if (!(-d $Test_Volume)) { print "CURRVALUE_STRING: Volume does not exist, isn't mounted or c +an't be read\n"; print "RETURNID: 2\n"; exit 0; } if (-f $Filepfad) { if ( ! unlink($Filepfad) ) { print "CURRVALUE_STRING: can't delete existing File ${Filepfad +}\n"; print "RETURNID: 2\n"; exit 0; } } my $Filehandle; if(!(open($Filehandle, '>', $Filepfad))) { print "CURRVALUE_STRING: can't create new file ${Filepfad} for wri +te access \n"; print "RETURNID: 2\n"; exit 0; } if(!(print $Filehandle "Test")) { print "CURRVALUE_STRING: Couldn't write into File\n"; print "RETURNID: 2\n"; exit 0; } if(!(close $Filehandle)) { print "CURRVALUE_STRING: can't close file ${Filepfad} after writin +g\n"; print "RETURNID: 2\n"; exit 0; } if(!(open($Filehandle, '<', $Filepfad))) { print "CURRVALUE_STRING: can't create new file ${Filepfad} for rea +d access \n"; print "RETURNID: 2\n"; exit 0; } while ($Content = <$Filehandle>) { chomp $Content; if ($Content eq $Searchpattern) { print "CURRVALUE_STRING: Files can be written and read\n"; print "RETURNID: 3\n"; exit 0; } else { print "CURRVALUE_STRING: File can be written but not r +ead or file-content does not match Searchpattern\n"; print "RETURNID: 4\n"; exit 0; } #else }#while

Whenever I print something, it's something needed by our monitoring tool to extract whether or not my attempts where successful. Some things will clearly have to change, but this is the barebones script I came up with that works perfectly well on local directories.


In reply to Filesystem and SMB/NFS share Access by PerlingAround

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.