473,473 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using variables with modules

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()

Jul 19 '05 #1
4 1312
The only problem I can see, is trailing whitespace from the ip.txt
file. Perhaps
ClientIP.strip() will help?

Jul 19 '05 #2
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

Jul 19 '05 #3
thanks runes, that makes sense, time for me to go off and read some
more about strip and try:

Cheers, MW.

Jul 19 '05 #4
On 5 May 2005 02:36:49 -0700, ma*********@gb.unisys.com declaimed the
following in comp.lang.python:

What happens if you try:
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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
6
by: dkeeney | last post by:
I have a newby type question on how global variables work between modules. I have a module test2.py that defines a global variable as well as two functions to operate on that variable. A script...
4
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
9
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.