Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Samba, Linux, Winbind, UTMP, usernames and IP addresses

by Kickstart (Pilgrim)
on Jun 12, 2002 at 21:49 UTC ( [id://174029]=CUFP: print w/replies, xml ) Need Help??

We recently needed a way to get the usernames and IP addresses for our users, but in a mixed environment it was a little difficult. We've got no local users on the Linux/Samba machine, everyone authenticates via Winbind to Active Directory on Window 2000. With the inclusion of 'utmp = yes' in smb.conf, the users who are connected to a share (in our case all of them) show up under the 'who' command with an entry like:

<domainname>\<username> smb/116 Jun 12 09:32 (10.0.10.9)

So...because Windows 2000 Active Directory doesn't automatically grab the IP address of logged in users (apparently will do so with the ILS Telephony service installed), a little bit of Perl will allow us to request a specific username based on IP address, an IP address based on a submitted username, or a list of everyone nicely formatted. From there, all sorts of uses come up (in our case, being able to start a VNC connection without doing a IP address lookup automatically is useful).

Here's the script:

#!/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 usern +ame elsif (/^all$/) {&runall;} # Prints all u + & ip elsif (/^[a-z]+/) {&runname;} # Prints out I +P 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; } }

Kickstart

edited - 17 June 2002 (footpad): Added <READMORE> Tag.

Replies are listed 'Best First'.
Updated simple code
by Kickstart (Pilgrim) on Jun 26, 2002 at 20:38 UTC
    Sometimes I make things much more difficult on my self than it needs to be. With proper use of the nmblookup command this is simple parsing!
    #!/usr/bin/perl -w # Script to deliver the IP address or get name based on WINS data use strict; my $command = "nmblookup \'$ARGV[0]\#03\'"; my @contents = `$command`; while (<@contents>) { if ($_ =~ /\d+\.\d+\.\d+\.\d+/) { m/(\d+\.\d+\.\d+\.\d+)/; print $1 unless ($1 eq '10.0.255.255'); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://174029]
Front-paged by Kickstart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found