0

I'm trying to re-code some Perl script I have to Python (and extend it). With Perl, using the Net library, I can give a hostname and get the Windows username. Here is a snippet of Perl code:

my $NetBios = Net::NBName->new;
my $nbName = $NetBios->node_status($hostname);
if ( $nbName ) {
    if ( $nbName->as_string =~ /^(\S+)\s+/ ) {
        ($user = $1) =~ tr/A-Z/a-z/;
    }
}

I'd like to do the same thing in Python, but I cannot seem to find the right library to do this. Is there an equivalent way?

For the background, I get the hostname of the user that is connected to a terminal server port. From that, if the hostname has "dhcp" or "vpn" in it, I want to use NetBIOS and try to get the username for that system (likely a PC).

pcm
  • 397
  • 6
  • 12
  • It looks like the NBName method for Perl no longer works either. Does anyone know how I could do the equivalent? IOW, given a host name, like dhcp-192-168-1-200, to a username for that PC or Mac? – pcm Sep 07 '12 at 12:57

1 Answers1

0

although afaik you cannot pass it a domain

 import getpass
 print getpass.getuser()

platform uname may also be useful

 import platform
 platform.uname()

I found the following at activestate (although I can make no guarantees as to what it does)

http://code.activestate.com/recipes/66314-get-user-info-on-windows-for-current-user/

import win32api
import win32net
import win32netcon
def UserGetInfo():
    dc=win32net.NetServerEnum(None,100,win32netcon.SV_TYPE_DOMAIN_CTRL)
    user=win32api.GetUserName()
    if dc[0]:
        dcname=dc[0][0]['name']
        return win32net.NetUserGetInfo("\\\\"+dcname,user,1)
    else:
        return win32net.NetUserGetInfo(None,user,1)
if __name__=="__main__":
    print UserGetInfo()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179