Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

perlTk sample that shows sambastatus

by teabag (Pilgrim)
on Sep 10, 2003 at 12:05 UTC ( #290390=snippet: print w/replies, xml ) Need Help??
Description: I was fooling around with perlTk and came up with this snippet. It might be handy for people writing gui programs like log checkers or wanting to use other unix program output.
UPDATED: it now uses the code enhancements liz suggested. Thanks liz ;)
#!/usr/bin/perl
#sambastatus in PerlTk

use strict;
use Tk;
use Tk::HList;

my $user=0;

my $top = new MainWindow(-title => 'Samba TK sample');

my $hlist = $top->Scrolled("HList",
            -header => 1,
            -columns => 7,
            -scrollbars => 'osoe',
            -width => 80,
            -selectbackground => 'SeaGreen3',
               )->pack(-expand => 1, -fill => 'both');

#updated with liz example!
my $option = 0;
foreach (qw(user gid pid machine service ip-adres logon)) {
   $hlist->header('create', $option++, -text => $_);
}

open SMBST, "smbstatus |grep \)|"; #filters out the nonsense
while(<SMBST>){
(my $service,my $uid,my $gid,my $pid,my $machine,my $ipadres,my $rest)
+ = split /\s+/, $_, 7;

chomp $rest;

$hlist->add($user);
$hlist->itemCreate($user, 0, -text => "$uid");
$hlist->itemCreate($user, 1, -text => "$gid");
$hlist->itemCreate($user, 2, -text => "$pid");
$hlist->itemCreate($user, 3, -text => "$machine");
$hlist->itemCreate($user, 4, -text => "$service");
$hlist->itemCreate($user, 5, -text => "$ipadres");
$hlist->itemCreate($user, 6, -text => "$rest");

$user++;
}
close SMBST;

MainLoop;
Replies are listed 'Best First'.
Re: perlTk sample that shows sambastatus
by liz (Monsignor) on Sep 10, 2003 at 12:18 UTC
    Some coding critique (not knowing anything about Tk):
    $hlist->header('create', 0, -text => 'user'); $hlist->header('create', 1, -text => 'gid'); $hlist->header('create', 2, -text => 'pid'); $hlist->header('create', 3, -text => 'machine'); $hlist->header('create', 4, -text => 'service'); $hlist->header('create', 5, -text => 'ip-adres'); $hlist->header('create', 6, -text => 'logon time');
    I would code this as:
    my $option = 0; foreach (qw(user gid pid machine service ip-adres logon)) { $hlist->header('create', $option++, -text => $_); }
    because I hate to repeat things myself that can be repeated by the computer. And I would try to do something similar for the rest of the snippet. And yes, I cheated a little by changing "logon time" to "logon" to make it work with qw(). ;-)

    Liz

      Excellent suggestion. I changed the code and will see if I can change the rest asap.

      thanks liz
      Teabag
      Sure there's more than one way, but one just needs one anyway - Teabag

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://290390]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2020-11-26 01:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?