6 use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT_NO $ASIP_PORT $LSOF);
8 # Written for linux; may have to be modified for your brand of Unix.
9 # Support for FreeBSD added by Joe Clarke <marcus@marcuscom.com>.
10 # Support Solaris added by Frank Lahm <franklahm@googlemail.com>.
11 # Support has also been added for 16 character usernames.
13 if ($ARGV[0] =~ /^(-v|-version|--version)$/ ) {
14 printf ("%s \(Netatalk @NETATALK_VERSION@\)\n", basename($0));
16 } elsif ($ARGV[0] =~ /^(-h|-help|--help)$/ ) {
17 printf ("usage: %s \[-v|-version|--version|-h|-help|--help\]\n", basename($0));
18 printf ("Show users connecting via AFP\n");
22 $MAC_PROCESS = "afpd";
23 if ($^O eq "freebsd" || $^O eq "openbsd") {
24 $PS_STR = "-awwxouser,pid,ppid,start,command";
25 $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)';
26 } elsif ($^O eq "solaris") {
27 $PS_STR = "-eo user,pid,ppid,c,stime,tty,time,comm";
28 $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
30 $PS_STR = "-eo user:32,pid,ppid,c,stime,tty,time,cmd";
31 $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)';
33 $ASIP_PORT = "afpovertcp";
36 # Change to 0 if you don't have lsof
40 if ($^O eq "freebsd") {
41 open(SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |");
44 next if ($_ !~ /$MAC_PROCESS/);
46 /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/;
47 my ($pid, $addr, $host);
50 $host = gethostbyaddr(pack('C4', split (/\./, $addr)), AF_INET);
51 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
55 "PID UID Username Name Logintime Mac\n";
57 } elsif ($^O eq "solaris") {
59 print "must be run as root\n";
62 print "PID UID Username Name Logintime Mac\n";
63 } elsif ($LSOF == 1) {
64 open(LSOF, "lsof -i :$ASIP_PORT |");
67 next if ($_ !~ /$ASIP_PORT/);
68 $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/;
72 ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ );
76 "PID UID Username Name Logintime Mac\n";
80 "PID UID Username Name Logintime\n";
83 open(PS, "ps $PS_STR |") || die "Unable to open a pipe to ``ps''";
86 next if ($_ !~ /$MAC_PROCESS/);
87 my ($user, $pid, $ppid, $time, $name, $uid, $t, $ip);
95 if ($^O eq "solaris") {
96 open(PFILES, "pfiles $pid |");
98 next if ($_ !~ /port: $ASIP_PORT_NO/);
100 next if ($_ !~ /peername/);
101 if ($_ =~ /AF_INET (.*) port/) {
103 if ($ip =~ /::ffff:(.*)/ ) {
115 ($t, $t, $uid, $t, $t, $t, $name, $t, $t) = getpwnam($user);
116 ($name) = ( $name =~ /(^[^,]+)/ );
117 printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user,
118 $name, $time, $mac{$pid};