Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Using smbstatusinfo in scripts

by teabag (Pilgrim)
on Apr 07, 2003 at 10:00 UTC ( [id://248550]=CUFP: print w/replies, xml ) Need Help??

Hi Monks. I needed to know which windows users were connected through Samba to the server and use the info in another perl program on my site. Now I can check the logged in users in the building on-line. Handy!
#!/usr/bin/perl -w #example samba-user checker use strict; my $user; my $pid; my $loginname; my $machine; my $dow; my $mon; my $day; my $time; my $year; my $string; my $uptime; # first filter out the relevant userinfo: open SMB, "smbstatus -b|grep :|"; while(<SMB>){ ($pid,$loginname,$machine,$dow,$mon,$day,$time,$year) = split /\s+/, $ +_, 8; print "$loginname logged in on machine: $machine (login: $mon $day - $ +time)\n"; $user++; } close SMB; print "\ntotal users: $user\n";

Replies are listed 'Best First'.
Re: Using smbstatusinfo in scripts
by teabag (Pilgrim) on Apr 08, 2003 at 15:57 UTC
    There is absolutely no reason I'm declaring all those variables up front, I guess. Still learning how to be strict. ;) I've always thought the (linux) grep was a bit faster then the builtin one in perl. But with this script it probably doesn't matter much.
    And you're right. Your last code does look a lot nicer. I just went for the quick and dirty code. One correction on your last code though:
    use constant INFOFIELDS => (LOGIN, MACHINE, MON, DAY, TIME);
    MON should be MONTH. Aside from that it works perfectly.

    Teabag.

    chmod 666 *.* 'cause everyone should have the right to read and write!

Re: Using smbstatusinfo in scripts (improved)
by Aristotle (Chancellor) on Apr 07, 2003 at 16:07 UTC
    Any reason you're declaring all those variables up front, one per line? Also, why spawn a process for a grep when Perl can do that with the same amount of effort?
    #!/usr/bin/perl -w use strict; my @info = grep /:/, do { open my $pipe, "smbstatus -b|" or die "Couldn't spawn 'smbstatus': $!\n"; <$pipe>; }; for(@info) { my ($pid, $loginname, $machine, $dow, $mon, $day, $time, $year) = split /\s+/, $_, 8; print "$loginname logged in on machine: $machine (login: $mon $day + - $time)\n"; } print "\ntotal users: " . @info . "\n";
    Or maybe even
    #!/usr/bin/perl -w use strict; use constant PID => 0; use constant LOGIN => 1; use constant MACHINE => 2; use constant WEEKDAY => 3; use constant MONTH => 4; use constant DAY => 5; use constant TIME => 6; use constant YEAR => 7; use constant INFOFMT => "%s logged in on machine: %s (login: %s %s - % +s)\n"; use constant INFOFIELDS => (LOGIN, MACHINE, MONTH, DAY, TIME); my @info = grep /:/, do { open my $pipe, "smbstatus -b|" or die "Couldn't spawn 'smbstatus': $!\n"; <$pipe>; }; printf INFOFMT, (split /\s+/, $_, 8)[INFOFIELDS] for @info; print "\ntotal users: " . @info . "\n";

    I actually like the second version a lot more, code wise, but wish it was possible to reduce the verbosity of the constant declarations..

    Update: s/\bMON\b/MONTH/

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found