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