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.


In reply to Samba, Linux, Winbind, UTMP, usernames and IP addresses by Kickstart

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.