Machine identification

Brad Tilley bradtilley at usa.net
Thu Sep 9 02:05:05 EDT 2004


Greg Lindstrom wrote:
> How can I get the name (or ip) of the machine where my python script is
> running?  I'd like to add it to my log entry.
> 
> Thanks,
> 
> --greg
> 
> Greg Lindstrom                                         (501) 975-4859
> NovaSys Health                  greg.lindstrom at novasyshealth.com
> 
> "We are the music makers, and we are the dreamers of dreams"  W.W.

You might also consider getting this info from the Windows registry if 
you're running a Windows PC:

This code:

#-------------------------------------------------
from _winreg import *

def list_run_at_boot():
    run_key = 
r"SYSTEM\ControlSet001\Services\{A9A76D2B-8D75-413F-A2F1-013095F2B61C}\Parameters\Tcpip"
    open_run_key = OpenKey(HKEY_LOCAL_MACHINE, run_key, 0, KEY_READ)
    print "Name \t Value\n"
    for i in range (10000):
       try:
          n,v,t = EnumValue(open_run_key, i)
          print n, "\t", v
       except EnvironmentError:
          print "\nThere are", i, "values under this key."
          break
    CloseKey(open_run_key)

list_run_at_boot()

#---------------------------------------------------

Produces this output:

#---------------------------------------------------

Name 	 Value

EnableDHCP 	1
IPAddress 	[u'0.0.0.0']
SubnetMask 	[u'0.0.0.0']
DefaultGateway 	[]
DhcpIPAddress 	192.168.1.100
DhcpSubnetMask 	255.255.255.0
DhcpServer 	192.168.1.1
Lease 	86400
LeaseObtainedTime 	1094701976
T1 	1094745176
T2 	1094777576
LeaseTerminatesTime 	1094788376
DhcpDefaultGateway 	[u'192.168.1.1']
DhcpSubnetMaskOpt 	[u'255.255.255.0']

There are 14 values under this key.

#---------------------------------------------------

Best of luck,
Brad





More information about the Python-list mailing list