Last month I wrote a post about getting Windows system information and I mentioned in one of my comments that there was another script that did some of this as well as other stuff, but I couldn't find it. Well, today I went digging for it and found the script I wanted. So we're going back down the rabbit hole for some more tips and tricks for getting information about the wonderful world of Windows using Python.
The following scripts are taken from a login script that I use and maintain at my employment. We usually need several ways to identify a particular user's machine. Fortunately, most workstations have several unique identifiers, such as IP, MAC address and the name of the workstation (although none of these necessarily has to be unique...we actually had a workstation that had a NIC with the same MAC as one of our servers, for example). Anyway, let's get to the code!
In this section, we'll use the platform module to get our computer's name. We actually mentioned this trick in my previous in my previous article, but since we need this information for the next snippet, I am going to repeat this trick here:
from platform import node computer_name = node()
That was pretty painless, right? Only two lines of code and we have what we needed. But there's actually at least one other way to get it:
import socket computer_name = socket.gethostname()
This snippet is also extremely simple, although the first one is slightly shorter. All we had to do was import the builtin socket module and call its gethostname method. Now we're ready to get our PC's IP address.
We can use the information we garnered above to get at our PC's IP address:
import socket ip_address = socket.gethostbyname(computer_name) # or we could do this: ip_address2 = socket.gethostbyname(socket.gethostname())
In this example, we again use the socket module, but this time we its gethostbyname method and pass in the name of the PC. The socket module will then return the IP address.
You can also use Tim Golden's WMI module. The following example comes from his wonderful WMI Cookbook:
import wmi c = wmi.WMI () for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): print interface.Description for ip_address in interface.IPAddress: print ip_address print
All it does if loop over the installed network adapters and print out their respective descriptions and IP addresses.
Now we can turn our attention to getting the MAC address. We'll look at two different ways to get it, starting with an ActiveState recipe:
def get_macaddress(host='localhost'): """ Returns the MAC address of a network host, requires >= WIN2K. """ # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/347812 import ctypes import socket import struct # Check for api availability try: SendARP = ctypes.windll.Iphlpapi.SendARP except: raise NotImplementedError('Usage only on Windows 2000 and above') # Doesn't work with loopbacks, but let's try and help. if host == '127.0.0.1' or host.lower() == 'localhost': host = socket.gethostname() # gethostbyname blocks, so use it wisely. try: inetaddr = ctypes.windll.wsock32.inet_addr(host) if inetaddr in (0, -1): raise Exception except: hostip = socket.gethostbyname(host) inetaddr = ctypes.windll.wsock32.inet_addr(hostip) buffer = ctypes.c_buffer(6) addlen = ctypes.c_ulong(ctypes.sizeof(buffer)) if SendARP(inetaddr, 0, ctypes.byref(buffer), ctypes.byref(addlen)) != 0: raise WindowsError('Retreival of mac address(%s) - failed' % host) # Convert binary data into a string. macaddr = '' for intval in struct.unpack('BBBBBB', buffer): if intval > 15: replacestr = '0x' else: replacestr = 'x' if macaddr != '': macaddr = ':'.join([macaddr, hex(intval).replace(replacestr, '')]) else: macaddr = ''.join([macaddr, hex(intval).replace(replacestr, '')]) return macaddr.upper()
Since I didn't write the code above, I won't go into it in depth. However, it is my understanding that this script works by first checking to see if it can do an ARP request, which is only available on Windows 2000 and above. Once that's confirmed, it attempts to use the ctypes module to get the inet address. After that's done, it goes through some stuff that I don't really understand to build the MAC address.
When I first started maintaining this code, I thought there had to be a better way to get the MAC address. I thought that maybe Tim Golden's WMI module or maybe the PyWin32 package would be the answer. I'm fairly certain that he gave me the following snippet or I found it on one of the Python mailing list archives:
def getMAC_wmi(): """uses wmi interface to find MAC address""" interfaces = [] import wmi c = wmi.WMI () for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled=1): if interface.DNSDomain == 'www.myDomain.com': return interface.MACAddress
Unfortunately, while this method works, it slowed down the login script noticeably, so I ended up using the original method in the end. I think Mr. Golden has released a newer version of the wmi module, so it's possible that this is now faster.
Getting the current user's login name with Python is trivial. All you need is the PyWin32 package.
from win32api import GetUserName userid = GetUserName()
One quick import and we have the username in two lines of code.
We can use the userid we acquired above to find out what groups it's in.
import os from win32api import GetUserName from win32com.client import GetObject def _GetGroups(user): """Returns a list of the groups that 'user' belongs to.""" groups = [] for group in user.Groups (): groups.append (group.Name) return groups userid = GetUserName() pdcName = os.getenv('dcName', 'primaryDomainController') try: user = GetObject("WinNT://%s/%s,user" % (pdcName, userid)) fullName = user.FullName myGroups = _GetGroups(user) except Exception, e: try: from win32net import NetUserGetGroups,NetUserGetInfo myGroups = [] groups = NetUserGetGroups(pdcName,userid) userInfo = NetUserGetInfo(pdcName,userid,2) fullName = userInfo['full_name'] for g in groups: myGroups.append(g[0]) except Exception, e: fullname = "Unknown" myGroups = []
This is more intimidating than any of the previous scripts we looked at, but it's actually pretty easy to understand. First we import the modules or method we need. Next we have a simple function that takes a user object as its sole parameter. This function will loop over that user's groups and add them to a list, which it then returns to the caller. The next piece of the puzzle is getting the primary domain controller, which we use the os module for.
We pass the pdcName and userid to GetObject (which is part of the win32com module) to get our user object. If that works correctly, then we can get the user's full name and groups. If it fails, then we catch the error and try to get the information with some functions from the win32net module. If that also fails, then we just set some defaults.
Hopefully you have learned some valuable tricks to use in your own code. I've been using these scripts for several years with Python 2.4+ and they work great!
Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary