From 487d32df6300b939eca1015bb895847ad4720913 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 8 Dec 2002 04:10:46 +0000 Subject: Update to macusers from netatalk 1.5.5. Strip gecos noise. Recognize IP-numbers (not only hostnames). --- macusers | 113 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 37 deletions(-) (limited to 'macusers') diff --git a/macusers b/macusers index ac930d2..04c20c6 100755 --- a/macusers +++ b/macusers @@ -1,52 +1,91 @@ #!/usr/bin/perl +use strict; +use Socket; +use vars qw($MAC_PROCESS $PS_STR $MATCH_STR $ASIP_PORT_NO $ASIP_PORT $LSOF); + # Written for linux; may have to be modified for your brand of Unix. -$MAC_PROCESS="afpd"; -$PS_STR="-ef"; -$ASIP_PORT="afpovertcp"; +# Support for FreeBSD added by Joe Clarke . +# Support could probably be extended for *BSD, but I do not have Net or +# OpenBSD machines to test with. Code has also been cleaned up and made +# to compile under strict. +# +# The new lsof call should also be quicker as it does not involve a +# second pipeline. +# +# Support has also been added for 16 character usernames. + +$MAC_PROCESS = "afpd"; +if ( $^O eq "freebsd" ) { + $PS_STR = "-awwxouser,pid,ppid,start,command"; + $MATCH_STR = '(\w+)\s+(\d+)\s+(\d+)\s+([\d\w:]+)'; +} +else { + $PS_STR = "-ef"; + $MATCH_STR = '\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)'; +} +$ASIP_PORT = "afpovertcp"; +$ASIP_PORT_NO = 548; # Change to 0 if you don't have lsof -$LSOF=1; +$LSOF = 1; +my %mac = (); -if ($LSOF == 1 ) -{ - open(LSOF,"lsof -i | grep $ASIP_PORT |"); +if ( $^O eq "freebsd" ) { + open( SOCKSTAT, "sockstat -4 | grep $MAC_PROCESS | grep -v grep |" ); - while() - { - if ($_ !~ /$ASIP_PORT/) - { - next; + while () { + next if ( $_ !~ /$MAC_PROCESS/ ); + $_ =~ /\S+\s+\S+\s+(\d+)\s+\d+\s+[\w\d]+\s+[\d\.:]+\s+([\d\.]+)/; + my ( $pid, $addr, $host ); + $pid = $1; + $addr = $2; + $host = gethostbyaddr( pack( 'C4', split ( /\./, $addr ) ), AF_INET ); + ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ ); + $mac{$pid} = $host; } - $_=~/\w+\s+(\d+).*->([\w-]+).*/; - $pid=$1; $host=$2; - $mac{$pid}=$host; - } + print + "PID UID Username Name Logintime Mac\n"; + close(SOCKSTAT); +} +elsif ( $LSOF == 1 ) { + open( LSOF, "lsof -i :$ASIP_PORT |" ); - close(LSOF); - print "PID UID Usercode Name Logintime Mac\n"; + while () { + next if ( $_ !~ /$ASIP_PORT/ ); + $_ =~ /\w+\s+(\d+).*->([\w\.-]+).*/; + my ( $pid, $host ); + $pid = $1; + $host = $2; + ($host) = ( $host =~ /(^(\d+\.){3}\d+|[\w\d\-]+)/ ); + $mac{$pid} = $host; + } + print + "PID UID Username Name Logintime Mac\n"; + close(LSOF); } -else -{ - print "PID UID Usercode Name Logintime\n"; +else { + print "PID UID Username Name Logintime\n"; } -open(PS," ps $PS_STR |") || die "cannot do ps"; - -while() -{ - if ($_ !~ /$MAC_PROCESS/ ) - { - next; - } - $_=~ /\s*(\w+)\s+(\d+)\s+(\d+)\s+\d+\s+([\d\w:]+)/; - $user=$1; $pid=$2; $ppid=$3; $time=$4; - if ($ppid != 1) - { - ($t,$t,$uid,$t,$t,$t,$name,$t,$t)=getpwnam($user); - printf "%-8d %-8d %-8s %-20s %-9s %s\n",$pid,$uid,$user,$name,$time,$mac{$pid}; - } +open( PS, "ps $PS_STR |" ) || die "Unable to open a pipe to ``ps''"; + +while () { + next if ( $_ !~ /$MAC_PROCESS/ ); + my ( $user, $pid, $ppid, $time, $name, $uid, $t ); + $_ =~ /$MATCH_STR/; + $user = $1; + $pid = $2; + $ppid = $3; + $time = $4; + + if ( $ppid != 1 ) { + ( $t, $t, $uid, $t, $t, $t, $name, $t, $t ) = getpwnam($user); + ($name) = ( $name =~ /(^[^,]+)/ ); + printf "%-8d %-8d %-16s %-20s %-9s %s\n", $pid, $uid, $user, $name, + $time, $mac{$pid}; + } } -close(PS); +close(PS); -- cgit v1.2.3