#!/usr/bin/perl -w # Script to deliver the IP address or get name based on the samba # connections list in 'who' (choice depending on parameter match) use strict; my @whocontents = `who`; my ($line, %user, %ipaddress, $username, $ip); # Process contents of the 'who' command; foreach (@whocontents) { if (m/\\([a-z0-9]+)/) { $username = $1; m/(\d+\.\d+\.\d+\.\d+)/; $ip = $1; $user{$username} = $1; $ipaddress{$1} = $username; } } # Set up formatting for the 'runall' subroutine format something = @<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<< $_, $user{$_} . # Perl format case statement for ($ARGV[0]) { if (/^\d+\.\d+\.\d+\.\d+/) {&runip;} # Prints username elsif (/^all$/) {&runall;} # Prints all u & ip elsif (/^[a-z]+/) {&runname;} # Prints out IP elsif (!$ARGV[0]) {&runusage;} # Prints usage } sub runusage { print "GETIPUSER: Prints the IP address of a supplied username\n"; print "or a username associated with a supplied IP address.\n"; print "(OR enter 'all' to get a list of all users and ips)\n"; print "\n"; print "Examples:\n"; print " ./getipuser 10.0.29.33\n"; print " ./getipuser gwebster\n"; print " ./getipuser all\n"; print "(requires that usernames not start with a number)\n"; } sub runname { if ($user{$ARGV[0]}) { print "$user{$ARGV[0]}\n"; } else { print "User is not connected to a samba share on this machine.\$ } }; sub runip { if ($ipaddress{$ARGV[0]}) { print "$ipaddress{$ARGV[0]}\n"; } else { print "User is not connected to a samba share on this machine.\$ } }; sub runall { $~ = 'something'; foreach (sort (keys(%user))) { write; } }