using variables with modules

M

marc.wyburn

Hi, I am trying to move away from Windows only scripting to Python.
I've written a quick script that will pull the product version from the
client registry. I can get the IP addresses from a file into a list
and then pull each element in the list using the for loop. I am
setting each element to a varuable ClientIP. When I try to use that
variable in _winreg values it is not substituting the element from the
list. If I type the list value straight into the _winreg command it
works fine. The IPaddresses in the list are surrounded by quotes. So

1. can I use variables with modules? (I have assumed that I can!)
2. does putting quotes around the IPaddress in the list create a
problem (my list is something like ["192.168.0.1","192.168.0.2"]

I've also tried not having the list elements in quotes and using %s
substitution in the _winreg command.

Any help would be much appreciated, Cheers, MW.

#Query ProductName in windows Registry

import _winreg
import sys

readfile = open("c:\scripts\ip.txt", 'r')
IPList = readfile.readlines()

for ClientIP in IPList:
print ClientIP
key = _winreg.ConnectRegistry(ClientIP, _winreg.HKEY_LOCAL_MACHINE)
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\Microsoft\Windows NT\CurrentVersion")
OSver=_winreg.QueryValueEx( hkey, "ProductName")
print OSver

print "Exiting Script"
sys.exit()
 
R

runes

The only problem I can see, is trailing whitespace from the ip.txt
file. Perhaps
ClientIP.strip() will help?
 
R

runes

I tested your code and made a few changes:

import _winreg
import sys

readfile = open("C:\scripts\ip.txt", 'r')
IPList = readfile.readlines()

for ClientIP in IPList:
ClientIP = ClientIP.strip()
ClientIP = r'\\' + ClientIP
try:
key = _winreg.ConnectRegistry(ClientIP,
_winreg.HKEY_LOCAL_MACHINE)
except EnvironmentError:
sys.exit("EnvironmentError occured")
hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\Microsoft\Windows
NT\CurrentVersion")
OSver=_winreg.QueryValueEx( hkey, "ProductName")
print OSver

print "Exiting Script"
sys.exit()

It gives the following output on my XP box when running it against a
ip.txt with two IP's, the firste being the box it self, the second a
non-domain 2003-server in the network:

(u'Microsoft Windows XP', 1)
EnvironmentError occured
 
M

marc.wyburn

thanks runes, that makes sense, time for me to go off and read some
more about strip and try:

Cheers, MW.
 
D

Dennis Lee Bieber

import _winreg
import sys

# readfile = open("c:\scripts\ip.txt", 'r')
# IPList = readfile.readlines()
IPList = ["192.168.0.1","192.168.0.2"]
for ClientIP in IPList:
print ClientIP
key = _winreg.ConnectRegistry(ClientIP, _winreg.HKEY_LOCAL_MACHINE)


IOW, exactly what /is/ in ip.txt? I'm assuming one IP number per
line -- though I don't recall if readlines() strips the new-line from
the end of each line.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,811
Latest member
SaulFernan

Latest Threads

Top