5 use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT_NO $ASIP_PORT $LSOF);
7 # Written for linux; may have to be modified for your brand of Unix.
8 # Support for FreeBSD added by Joe Clarke <marcus@marcuscom.com>.
9 # Support Solaris added by Frank Lahm <franklahm@googlemail.com>.
10 # Support has also been added for 16 character usernames.
12 $MAC_PROCESS = "afpd";
13 if ($^O eq "freebsd" || $^O eq "openbsd") {
14 $PS_STR = "-awwxouser,pid,ppid,start,command";
15 $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
16 } elsif ($^O eq "solaris") {
17 $PS_STR = "-eo user,pid,ppid,c,stime,tty,time,comm";
18 $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
20 $PS_STR = "-eo user:32,pid,ppid,c,stime,tty,time,cmd";
21 $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
23 $ASIP_PORT = "afpovertcp";
26 # Change to 0 if you don't have lsof
30 if ($^O eq "freebsd") {
31 open(SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |");
34 next if ($_ !~ /$MAC_PROCESS/);
36 /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/;
37 my ($pid, $addr, $host);
40 $host = gethostbyaddr(pack('C4', split (/\./, $addr)), AF_INET);
41 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
45 "PID UID Username Name Logintime Mac\n";
47 } elsif ($^O eq "solaris") {
49 print "must be run as root\n";
52 print "PID UID Username Name Logintime Mac\n";
53 } elsif ($LSOF == 1) {
54 open(LSOF, "lsof -i :$ASIP_PORT |");
57 next if ($_ !~ /$ASIP_PORT/);
58 $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
62 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
66 "PID UID Username Name Logintime Mac\n";
70 "PID UID Username Name Logintime\n";
73 open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''";
76 next if ($_ !~ /$MAC_PROCESS/);
77 my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip);
85 if ($^O eq "solaris") {
86 open(PFILES, "pfiles $pid |");
88 next if ($_ !~ /port: $ASIP_PORT_NO/);
90 next if ($_ !~ /peername/);
91 if ($_ =~ /AF_INET (.*) port/) {
93 if ($ip =~ /::ffff:(.*)/ ) {
105 ($t, $t, $uid, $t, $t, $t, $name, $t, $t) = getpwnam($user);
106 ($name) = ( $name =~ /(^[^,]+)/ );
107 printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
108 $name, $time, $mac{$pid};