]> arthur.barton.de Git - netatalk.git/blob - contrib/macusers/macusers
massive commenting/autoconf changes
[netatalk.git] / contrib / macusers / macusers
1 #!/usr/bin/perl
2 # $Id: macusers,v 1.4 2001-06-29 14:14:46 rufustfirefly Exp $
3
4 use strict;
5 use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT $LSOF);
6
7 # Written for linux; may have to be modified for your brand of Unix.
8
9 # Support for FreeBSD added by Joe Clarke <marcus@marcuscom.com>.
10 # Support could probably be extended for *BSD, but I do not have Net or
11 # OpenBSD machines to test with.  Code has also been cleaned up and made
12 # to compile under strict.
13 #
14 # The new lsof call should also be quicker as it does not involve a 
15 # second pipeline.
16 #
17 # Support has also been added for 16 character usernames.
18
19 $MAC_PROCESS = "afpd";
20 if ( $^O eq "freebsd" ) {
21         $PS_STR    = "-awwxouser,pid,ppid,start,command";
22         $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
23 }
24 else {
25         $PS_STR    = "-ef";
26         $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
27 }
28 $ASIP_PORT = "afpovertcp";
29
30 # Change to 0 if you don't have lsof
31 $LSOF = 1;
32 my %mac = ();
33
34 if ( $LSOF == 1 ) {
35         open( LSOF, "lsof -i :$ASIP_PORT |" );
36
37         while (<LSOF>) {
38                 next if ( $_ !~ /$ASIP_PORT/ );
39                 $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
40                 my ( $pid, $host );
41                 $pid = $1;
42                 $host = $2;
43                 ($host) = ( $host =~ /(^[\w\d\-]+)/ );
44                 $mac{$pid} = $host;
45         }
46
47         close(LSOF);
48         print
49 "PID      UID      Username         Name                 Logintime Mac\n";
50 }
51 else {
52         print
53           "PID      UID      Username         Name                 Logintime\n";
54 }
55
56 open( PS, "ps $PS_STR |" ) || die "Unable to open a pipe to ``ps''";
57
58 while (<PS>) {
59         next if ( $_ !~ /$MAC_PROCESS/ );
60         my ( $user, $pid, $ppid, $time, $name, $uid, $t );
61         $_ =~ /$MATCH_STR/;
62         $user = $1;
63         $pid  = $2;
64         $ppid = $3;
65         $time = $4;
66
67         if ( $ppid != 1 ) {
68                 ( $t, $t, $uid, $t, $t, $t, $name, $t, $t ) = getpwnam($user);
69                 printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
70                   $name, $time, $mac{$pid};
71         }
72 }
73
74 close(PS);