<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w
#use strict;
use vars qw/$AUTOLOAD/;

package Smbclient;

sub new {
   my $class = shift;
   my $fullshare = shift;
   my %options = @_;

   chomp $fullshare;

   if(!defined($fullshare)) {
      warn "No share!\n";
      return;
   }

   my $self = {} ;
   bless $self, ref $class || $class ;
   $self-&gt;fullshare($fullshare);
   foreach my $key ( keys %options ) {
      $lkey = lc($key);
      $self-&gt;$lkey( $options{$key} );
   }
   return $self;
}

sub AUTOLOAD {
   my $self = shift;
   my $var = shift;
   my $method = $AUTOLOAD;
   $method =~ s{^.*::}{};
   if ( defined($var) ) {
      $self-&gt;{data}{$method} = $var;
   }
   return $self-&gt;{data}{$method};
}

sub computer() {
   my $self = shift;
   my @comp = split /\//, $self-&gt;fullshare();
   return $comp[2];
}

sub share() {
   my $self = shift;
   my @comp = split /\//, $self-&gt;fullshare();
   return $comp[3];
}

sub directory() {
   my $self = shift;
   my @comp = split /\//, $self-&gt;fullshare();
   @comp = splice @comp, 4, -1;
   my $str = "/";
   $str .= $_."/" for(@comp);
   return $str;
}

sub localfilename() {
   my $self = shift;
   my $str = $self-&gt;target();
   $str =~ s/'//g;
   return $str;
}

sub target() {
   my $self = shift;
   my @comp = split /\//, $self-&gt;fullshare();
   return $comp[ $#comp ];
}

sub check() {
   my $self = shift;
}

sub smbclient() {
   my $self = shift;
   my $command     = shift;
   my $directory   = shift;
   if (!defined($directory)) {
      $directory = "";
   }

   if ( defined($self-&gt;{cache}{smbclient}{$command.$directory}) ) {
      return @{ $self-&gt;{cache}{smbclient}{$command.$directory} };
   }

   my $share = "\"//".$self-&gt;computer()."/";
   if ( defined($self-&gt;share()) ) {
      $share .= $self-&gt;share();
   }
   $share .= "\"";
   my @commands ;
   push @commands, "smbclient";
   push @commands, $share;
   push @commands, $self-&gt;password if defined($self-&gt;password());
   if ( $directory ne "" ) {
      push @commands, "-D \"".$directory."\"";
   } else {
      push @commands, "-D \"".$self-&gt;directory()."\"";
   }
   push @commands, "-I ".$self-&gt;ip() if defined($self-&gt;ip());
   push @commands, "-U ".$self-&gt;username() if defined($self-&gt;username());
   push @commands, "-N", if !defined($self-&gt;password());

   push @commands, $command;

   print STDERR "\e[31m\n","@commands","\e[0m\n" if $self-&gt;verbose() ;

   $self-&gt;{status}{smb_running} = 1;
   @result = qx/@commands/;
   $self-&gt;{status}{smb_running} = 0;

   @{ $self-&gt;{cache}{smbclient}{$command.$directory} } = @result;
   return @result;
}

sub printDebug() {
   my $self = shift;
   foreach my $key ( keys %{ $self-&gt;{data} } ) {
      print $key,"\t",$self-&gt;{data}{$key},"\n";
   }
   print "Computer:\t",$self-&gt;computer(),"\n";
   print "Share:\t\t",$self-&gt;share(),"\n";
   print "Directory:\t",$self-&gt;directory(),"\n";
   print "Target:\t\t",$self-&gt;target(),"\n";
}

sub parseListing {
   my $self = shift;
   my @lines = (@_);
   my @files = ();

   LOOP: foreach (@lines) {
      my $item;
      if ( m/^  ([\S ]*\S|\.+) {5,}([HDRSA]+) +(\d+)  (\S[\S ]+\S)$/ ||
           m/^  ([\S ]*\S|[\.]+) {6,}()([0-9]+)  (\S[\S ]+\S)$/ ) {
         $item-&gt;{name} = $1;
         $item-&gt;{attr} = $2;
         $item-&gt;{size} = $3;
         $item-&gt;{date} = $4;
      } elsif ( m/^\s+([\S ]+\S)\s+Disk\s+(([\S ]+\S)|)$/ ) {
         if ( $1 =~ /\$$/ ) {
            next LOOP;
         }
         $item-&gt;{name} = $1;
         $item-&gt;{attr} = "S";
         $item-&gt;{size} = "0";
         $item-&gt;{date} = "unknown";
      } else {
         next LOOP;
      }
      push @files, $item;
   }
   return @files;
}

sub online() {
   my $self = shift;
   my $item = $self-&gt;listself();
   return defined($item);
}

sub listself() {
   my $self = shift;
   if ( defined($self-&gt;{cache}{selflist}) ) {
      return $self-&gt;{cache}{selflist};
   }
   my @list = $self-&gt;smbclient("-c 'ls'",$self-&gt;directory() );
   my @files = $self-&gt;parseListing( @list );
   LOOP: foreach my $file (@files) {
      if ( $file-&gt;{name} eq $self-&gt;target() ) {
         $self-&gt;{cache}{selflist} = $file;
         last LOOP;
      }
   }
   return $self-&gt;{cache}{selflist};
}

sub type() {
   my $self = shift;
   my $item = $self-&gt;listself();
   if ( defined($item) ) {
      if ( $item-&gt;{attr} =~ /D/ ) {
         return "directory";
      } elsif ( $item-&gt;{attr} =~ /Service/ ) {
         return "service";
      } else {
         return "file";
      }
   }
}

sub list() {
   my $self = shift;
   if ( defined($self-&gt;share()) ) {
      my $dir = $self-&gt;directory();
      if ( $self-&gt;type() eq "directory" ) {
         $dir .= $self-&gt;target();
      }
      @files = $self-&gt;smbclient("-c ls", $dir);
   } else {
      @files = $self-&gt;smbclient("-L ".$self-&gt;computer(), "");
   }
   my @files = $self-&gt;parseListing(@result);
   return @files;
}

sub size() {
   my $self = shift;
   if ( $self-&gt;type() eq "directory" ) {
      return $self-&gt;recursive_size();
   } else {
      my $item = $self-&gt;listself();
      return $item-&gt;{size};
   }
}

sub recursive_size() {
   my $self = shift;
   my $dir = $self-&gt;directory.$self-&gt;target();

   my @output = $self-&gt;smbclient("-c 'recurse;ls'", $dir);
   my @files = $self-&gt;parseListing(@output);
   my $sum = 0;
   foreach (@files) {
      $sum += $_-&gt;{size};
   }
   return $sum;
}

sub get() {
   my $self = shift;
   my $target = $self-&gt;target();
   my @output;
   $target =~ s/'/'\\''/g;
   if ( $self-&gt;type() eq "directory" ) {
      @output = $self-&gt;smbclient("-c 'recurse; prompt; mget \"$target\"'");
   } else {
      @output = $self-&gt;smbclient("-c 'get \"$target\"'");
   }
   $self-&gt;parseGetOutput(@output);
}

sub parseGetOutput() {
   my $self = shift;
   my @output = (@_);
   $self-&gt;{lastdownload}{completed} = 1;
   print $_ for (@output);
}

sub tar() {
   my $self = shift;
   my $filename = shift;
   my $target = $self-&gt;target();
   if ( !defined($filename) ) {
      $filename = $self-&gt;localfilename().".tar";
   }
   $directory = $self-&gt;directory();
   if ($self-&gt;type() eq "directory") {
      $directory .= $self-&gt;target();
   }
   @output = $self-&gt;smbclient("-Tc \"$filename\"", $directory);
   print $_ for (@output);
}

sub showcache() {
   my $self = shift;
   foreach my $key ( keys %{ $self-&gt;{cache} } ) {
      print "Cache for $key:\n";
      foreach my $key2 ( keys %{ $self-&gt;{cache}{$key} } ) {
         print "\t$key2\t",$self-&gt;{cache}{$key}{$key2},"\n" ;
      }
   }
}

sub download_completed() {
   my $self = shift;
   return defined($self-&gt;{lastdownload}{completed});
}

sub speed() {
   my $self = shift;
   warn "speed() Not implemented\n";
   return 0.0;
}
1;
</pre></body></html>