<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Mouse Vs. The Python &#187; System Admin</title>
	<atom:link href="http://www.blog.pythonlibrary.org/tag/system-admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Sun, 05 Sep 2010 16:58:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PyWin32: Getting Windows Event Logs</title>
		<link>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 12:14:28 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[PyWin32]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1071</guid>
		<description><![CDATA[The other day, there was a post on one of the mailing lists that I follow about accessing the Windows Event Logs. I thought that was an interesting topic, so I went looking for examples and found a pretty nice example on ActiveState. In this article, you&#8217;ll find out what I discovered. It&#8217;s probably easiest [...]]]></description>
			<content:encoded><![CDATA[<p>The  other day, there was a post on one of the mailing lists that I follow about accessing the Windows Event Logs. I thought that was an interesting topic, so I went looking for examples and found a pretty nice example on <a href="http://docs.activestate.com/activepython/2.5/pywin32/Windows_NT_Eventlog.html">ActiveState</a>. In this article, you&#8217;ll find out what I discovered.<span id="more-1071"></span></p>
<p>It&#8217;s probably easiest to just jump right into the code. Note that the only thing other than Python that you will need is the PyWin32 package. Once you&#8217;ve got that, then you can follow along:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">codecs</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">traceback</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32con
<span style="color: #ff7700;font-weight:bold;">import</span> win32evtlog
<span style="color: #ff7700;font-weight:bold;">import</span> win32evtlogutil
<span style="color: #ff7700;font-weight:bold;">import</span> winerror
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getAllEvents<span style="color: black;">&#40;</span>server, logtypes, basePath<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> server:
        serverName = <span style="color: #483d8b;">&quot;localhost&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        serverName = server
    <span style="color: #ff7700;font-weight:bold;">for</span> logtype <span style="color: #ff7700;font-weight:bold;">in</span> logtypes:
        path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>basePath, <span style="color: #483d8b;">&quot;%s_%s_log.log&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>serverName, logtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        getEventLogs<span style="color: black;">&#40;</span>server, logtype, path<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getEventLogs<span style="color: black;">&#40;</span>server, logtype, logPath<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    Get the event logs from the specified machine according to the
    logtype (Example: Application) and save it to the appropriately
    named log file
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Logging %s events&quot;</span> <span style="color: #66cc66;">%</span> logtype
    log = <span style="color: #dc143c;">codecs</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>logPath, encoding=<span style="color: #483d8b;">'utf-8'</span>, mode=<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    line_break = <span style="color: #483d8b;">'-'</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">80</span>
&nbsp;
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>%s Log of %s Events<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>server, logtype<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Created: %s<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">time</span>.<span style="color: black;">ctime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + line_break + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    hand = win32evtlog.<span style="color: black;">OpenEventLog</span><span style="color: black;">&#40;</span>server,logtype<span style="color: black;">&#41;</span>
    total = win32evtlog.<span style="color: black;">GetNumberOfEventLogRecords</span><span style="color: black;">&#40;</span>hand<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Total events in %s = %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>logtype, total<span style="color: black;">&#41;</span>
    flags = win32evtlog.<span style="color: black;">EVENTLOG_BACKWARDS_READ</span>|win32evtlog.<span style="color: black;">EVENTLOG_SEQUENTIAL_READ</span>
    events = win32evtlog.<span style="color: black;">ReadEventLog</span><span style="color: black;">&#40;</span>hand,flags,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    evt_dict=<span style="color: black;">&#123;</span>win32con.<span style="color: black;">EVENTLOG_AUDIT_FAILURE</span>:<span style="color: #483d8b;">'EVENTLOG_AUDIT_FAILURE'</span>,
              win32con.<span style="color: black;">EVENTLOG_AUDIT_SUCCESS</span>:<span style="color: #483d8b;">'EVENTLOG_AUDIT_SUCCESS'</span>,
              win32con.<span style="color: black;">EVENTLOG_INFORMATION_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_INFORMATION_TYPE'</span>,
              win32con.<span style="color: black;">EVENTLOG_WARNING_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_WARNING_TYPE'</span>,
              win32con.<span style="color: black;">EVENTLOG_ERROR_TYPE</span>:<span style="color: #483d8b;">'EVENTLOG_ERROR_TYPE'</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        events=<span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> events:
            events=win32evtlog.<span style="color: black;">ReadEventLog</span><span style="color: black;">&#40;</span>hand,flags,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">for</span> ev_obj <span style="color: #ff7700;font-weight:bold;">in</span> events:
                the_time = ev_obj.<span style="color: black;">TimeGenerated</span>.<span style="color: black;">Format</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#'12/23/99 15:54:09'</span>
                evt_id = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>winerror.<span style="color: black;">HRESULT_CODE</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">EventID</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                computer = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">ComputerName</span><span style="color: black;">&#41;</span>
                cat = ev_obj.<span style="color: black;">EventCategory</span>
        <span style="color: #808080; font-style: italic;">##        seconds=date2sec(the_time)</span>
                record = ev_obj.<span style="color: black;">RecordNumber</span>
                msg = win32evtlogutil.<span style="color: black;">SafeFormatMessage</span><span style="color: black;">&#40;</span>ev_obj, logtype<span style="color: black;">&#41;</span>
&nbsp;
                source = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>ev_obj.<span style="color: black;">SourceName</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> ev_obj.<span style="color: black;">EventType</span> <span style="color: #ff7700;font-weight:bold;">in</span> evt_dict.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                    evt_type = <span style="color: #483d8b;">&quot;unknown&quot;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    evt_type = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>evt_dict<span style="color: black;">&#91;</span>ev_obj.<span style="color: black;">EventType</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Event Date/Time: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> the_time<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Event ID / Type: %s / %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>evt_id, evt_type<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Record #%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> record<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Source: %s<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> source<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line_break<span style="color: black;">&#41;</span>
                log.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">traceback</span>.<span style="color: black;">print_exc</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">exc_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Log creation finished. Location of log is %s&quot;</span> <span style="color: #66cc66;">%</span> logPath
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    server = <span style="color: #008000;">None</span>  <span style="color: #808080; font-style: italic;"># None = local machine</span>
    logTypes = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;System&quot;</span>, <span style="color: #483d8b;">&quot;Application&quot;</span>, <span style="color: #483d8b;">&quot;Security&quot;</span><span style="color: black;">&#93;</span>
    getAllEvents<span style="color: black;">&#40;</span>server, logTypes, <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\d</span>ownloads&quot;</span><span style="color: black;">&#41;</span></pre>
<p>There are a couple potential caveats to this type of scripting. I tested this code as an Administrator on my PCs and as a Domain Administrator at work. I did not test it as any other type of user. So if you have problems getting this code to run, check your permissions. I tested this from Windows XP  and Windows 7. The UAC doesn&#8217;t appear to block this activity on Windows 7, so that made it just as easy to use as XP did. However, Windows 7&#8242;s events had some unicode in the message portion of the code whereas XP did not. Watch out for that and handle it accordingly.</p>
<p>Anyway, let&#8217;s unpack this script and see how it works. First we have a number of imports. We use the <em>codecs</em> modules to encode the log file in utf-8 just in case there&#8217;s some sneaky unicode in the message. We use PyWin32&#8242;s <em>win32evtlog</em> module to open the event log and pull information out of it. According to the article I mentioned at the beginning, to get all the events from the log, you need to call <em>win32evtlog.ReadEventLog</em> repeatedly until it stops returning events. Thus, we use <em>while </em>loop. Inside the while loop, we use a <em>for</em> loop to iterate over the events and extract the event ID, record number, event message, event source and a few other tidbits. We log it and then we exit the <em>for</em> loop and the <em>while</em> loop calls the <em>win32evtlog.ReadEventLog</em> again.</p>
<p>We use the <em>traceback</em> module to print out any errors that occur during the script&#8217;s run. And that&#8217;s all there is to it!</p>
<h2>Wrapping Up</h2>
<p>As you can see, using the PyWin32 package is easy. If you get stuck, it has some great documentation. If that documentation isn&#8217;t good enough though, you can fall back on MSDN instead. PyWin32 is a light wrapper around Windows&#8217; API, so using MSDN&#8217;s instructions is fairly simple. Anyway, I hope you learned a lot and will find it helpful.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://docs.python.org/howto/unicode.html">Python and Unicode</a></li>
<li><a href="http://sourceforge.net/projects/pywin32/">PyWin32</a></li>
<li>PyWin32 <a href="http://docs.activestate.com/activepython/2.5/pywin32/PyWin32.HTML">Documentation </a>from ActiveState</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python: Running Ping, Traceroute and More</title>
		<link>http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 12:42:18 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=907</guid>
		<description><![CDATA[Last year I needed to figure out a way to get the following information with Python: get the route table, capture the data from pinging a series of IPs, run tracert and get information about the NIC(s) installed. This all needed to be done on a Windows machine as it was part of a diagnostics [...]]]></description>
			<content:encoded><![CDATA[<p>Last year I needed to figure out a way to get the following information with Python: get the route table, capture the data from pinging a series of IPs, run tracert and get information about the NIC(s) installed. This all needed to be done on a Windows machine as it was part of a diagnostics script to try to figure out why the machine (usually a laptop) wouldn&#8217;t connect to our VPN. I ended up creating a wxPython GUI to make it easy for the user to run, but these scripts will work just fine without wx. Let&#8217;s see what they look like!<span id="more-907"></span></p>
<h2>The Main Script</h2>
<p>To start with, we&#8217;ll look at the entire script and then go over each important piece. If you&#8217;d like to use the code below, you will need <a href="http://www.wxpython.org">wxPython</a> and the <a href="http://sourceforge.net/projects/pywin32/files/pywin32/">PyWin32</a> package.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32net
<span style="color: #ff7700;font-weight:bold;">import</span> wx
&nbsp;
filename = r<span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\l</span>ogs<span style="color: #000099; font-weight: bold;">\n</span>ic-diag.log&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RedirectText:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,aWxTextCtrl<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">out</span>=aWxTextCtrl
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\l</span>ogs&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\l</span>ogs&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">filename</span> = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> write<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,<span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">out</span>.<span style="color: black;">WriteText</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">filename</span>.<span style="color: black;">closed</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">filename</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyForm<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #808080; font-style: italic;">#---------------------------------------------------------------------- </span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">&quot;Diagnostic Tool&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add a panel so it looks the correct on all platforms</span>
        panel = wx.<span style="color: black;">Panel</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span><span style="color: black;">&#41;</span>
        log = wx.<span style="color: black;">TextCtrl</span><span style="color: black;">&#40;</span>panel, wx.<span style="color: black;">ID_ANY</span>, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>,
                          style = wx.<span style="color: black;">TE_MULTILINE</span>|wx.<span style="color: black;">TE_READONLY</span>|wx.<span style="color: black;">HSCROLL</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># log.Disable()</span>
        btn = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span>panel, wx.<span style="color: black;">ID_ANY</span>, <span style="color: #483d8b;">'Run Diagnostics'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onRun</span>, btn<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Add widgets to a sizer        </span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>log, <span style="color: #ff4500;">1</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">EXPAND</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>btn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>|wx.<span style="color: black;">CENTER</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        panel.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>sizer<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># redirect text here</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">redir</span>=RedirectText<span style="color: black;">&#40;</span>log<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>=<span style="color: #008000;">self</span>.<span style="color: black;">redir</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------    </span>
    <span style="color: #ff7700;font-weight:bold;">def</span> runDiagnostics<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Run some diagnostics to get the machine name, ip address, mac,
        gateway, DNS, route tables, etc
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #808080; font-style: italic;"># create the route table:</span>
        <span style="color: #808080; font-style: italic;"># based on the following list comp from http://win32com.goermezer.de/content/view/220/284/</span>
        <span style="color: #808080; font-style: italic;"># route_table = [elem.strip().split() for elem in os.popen(&quot;route print&quot;).read().split(&quot;Metric\n&quot;)[1].split(&quot;\n&quot;) if re.match(&quot;^[0-9]&quot;, elem.strip())]</span>
        route_table = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;route print&quot;</span>, shell=<span style="color: #008000;">True</span>,
                        stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            line = proc.<span style="color: black;">stdout</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            route_table.<span style="color: black;">append</span><span style="color: black;">&#40;</span>line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line: <span style="color: #ff7700;font-weight:bold;">break</span>
        proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Log Created at %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">time</span>.<span style="color: black;">ctime</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------------------------------------------------------------------------------------------&quot;</span>
        info = win32net.<span style="color: black;">NetWkstaGetInfo</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #ff4500;">102</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">compname</span> = info<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;computername&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Computer name: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">compname</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------------------------------------------------------------------------------------------&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Route Table:&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%20s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Network Destination&quot;</span>, <span style="color: #483d8b;">&quot;Netmask&quot;</span>,
                                          <span style="color: #483d8b;">&quot;Gateway&quot;</span>, <span style="color: #483d8b;">&quot;Interface&quot;</span>, <span style="color: #483d8b;">&quot;Metric&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> route <span style="color: #ff7700;font-weight:bold;">in</span> route_table:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>route<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">5</span>:
                dst, mask, gateway, interface, metric = route
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%20s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>dst, mask, gateway, interface, metric<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------------------------------------------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
        ips = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;65.55.17.26&quot;</span>, <span style="color: #483d8b;">&quot;67.205.46.185&quot;</span>, <span style="color: #483d8b;">&quot;67.195.160.76&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> ip <span style="color: #ff7700;font-weight:bold;">in</span> ips:
            <span style="color: #008000;">self</span>.<span style="color: black;">pingIP</span><span style="color: black;">&#40;</span>ip<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">tracertIP</span><span style="color: black;">&#40;</span>ip<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>----------------------------------------------------------&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">getNICInfo</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;############ END OF LOG ############&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------   </span>
    <span style="color: #ff7700;font-weight:bold;">def</span> pingIP<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ip<span style="color: black;">&#41;</span>:
        proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ping %s&quot;</span> <span style="color: #66cc66;">%</span> ip, shell=<span style="color: #008000;">True</span>,
                                stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            line = proc.<span style="color: black;">stdout</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            wx.<span style="color: black;">Yield</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;&quot;</span>:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line: <span style="color: #ff7700;font-weight:bold;">break</span>
        proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> tracertIP<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ip<span style="color: black;">&#41;</span>:
        proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tracert -d %s&quot;</span> <span style="color: #66cc66;">%</span> ip, shell=<span style="color: #008000;">True</span>,
                                stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            line = proc.<span style="color: black;">stdout</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            wx.<span style="color: black;">Yield</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;&quot;</span>:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line: <span style="color: #ff7700;font-weight:bold;">break</span>
        proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------            </span>
    <span style="color: #ff7700;font-weight:bold;">def</span> getNICInfo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;

http://www.microsoft.com/technet/scriptcenter/scripts/python/pyindex.mspx?mfr=true

        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Interface information:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
        strComputer = <span style="color: #483d8b;">&quot;.&quot;</span>
        objWMIService = win32com.<span style="color: black;">client</span>.<span style="color: black;">Dispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WbemScripting.SWbemLocator&quot;</span><span style="color: black;">&#41;</span>
        objSWbemServices = objWMIService.<span style="color: black;">ConnectServer</span><span style="color: black;">&#40;</span>strComputer,<span style="color: #483d8b;">&quot;root<span style="color: #000099; font-weight: bold;">\c</span>imv2&quot;</span><span style="color: black;">&#41;</span>
        colItems = objSWbemServices.<span style="color: black;">ExecQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Select * from Win32_NetworkAdapterConfiguration&quot;</span><span style="color: black;">&#41;</span>
        numOfNics = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>colItems<span style="color: black;">&#41;</span>
        count = <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> objItem <span style="color: #ff7700;font-weight:bold;">in</span> colItems:
            <span style="color: #808080; font-style: italic;"># if the IP interface is enabled, grab its info</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;***Interface %s of %s***&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>count, numOfNics<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> objItem.<span style="color: black;">IPEnabled</span> == <span style="color: #008000;">True</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Arp Always Source Route: &quot;</span>, objItem.<span style="color: black;">ArpAlwaysSourceRoute</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Arp Use EtherSNAP: &quot;</span>, objItem.<span style="color: black;">ArpUseEtherSNAP</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Caption: &quot;</span>, objItem.<span style="color: black;">Caption</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Database Path: &quot;</span>, objItem.<span style="color: black;">DatabasePath</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Dead GW Detect Enabled: &quot;</span>, objItem.<span style="color: black;">DeadGWDetectEnabled</span>
                z = objItem.<span style="color: black;">DefaultIPGateway</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Default IP Gateway: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Default TOS: &quot;</span>, objItem.<span style="color: black;">DefaultTOS</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Default TTL: &quot;</span>, objItem.<span style="color: black;">DefaultTTL</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Description: &quot;</span>, objItem.<span style="color: black;">Description</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DHCP Enabled: &quot;</span>, objItem.<span style="color: black;">DHCPEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DHCP Lease Expires: &quot;</span>, objItem.<span style="color: black;">DHCPLeaseExpires</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DHCP Lease Obtained: &quot;</span>, objItem.<span style="color: black;">DHCPLeaseObtained</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DHCP Server: &quot;</span>, objItem.<span style="color: black;">DHCPServer</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DNS Domain: &quot;</span>, objItem.<span style="color: black;">DNSDomain</span>
                z = objItem.<span style="color: black;">DNSDomainSuffixSearchOrder</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DNS Domain Suffix Search Order: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DNS Enabled For WINS Resolution: &quot;</span>, objItem.<span style="color: black;">DNSEnabledForWINSResolution</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DNS Host Name: &quot;</span>, objItem.<span style="color: black;">DNSHostName</span>
                z = objItem.<span style="color: black;">DNSServerSearchOrder</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;DNS Server Search Order: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Domain DNS Registration Enabled: &quot;</span>, objItem.<span style="color: black;">DomainDNSRegistrationEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Forward Buffer Memory: &quot;</span>, objItem.<span style="color: black;">ForwardBufferMemory</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Full DNS Registration Enabled: &quot;</span>, objItem.<span style="color: black;">FullDNSRegistrationEnabled</span>
                z = objItem.<span style="color: black;">GatewayCostMetric</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Gateway Cost Metric: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IGMP Level: &quot;</span>, objItem.<span style="color: black;">IGMPLevel</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Index: &quot;</span>, objItem.<span style="color: black;">Index</span>
                z = objItem.<span style="color: black;">IPAddress</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Address: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Connection Metric: &quot;</span>, objItem.<span style="color: black;">IPConnectionMetric</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Enabled: &quot;</span>, objItem.<span style="color: black;">IPEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Filter Security Enabled: &quot;</span>, objItem.<span style="color: black;">IPFilterSecurityEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Port Security Enabled: &quot;</span>, objItem.<span style="color: black;">IPPortSecurityEnabled</span>
                z = objItem.<span style="color: black;">IPSecPermitIPProtocols</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Sec Permit IP Protocols: &quot;</span>, x
                z = objItem.<span style="color: black;">IPSecPermitTCPPorts</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Sec Permit TCP Ports: &quot;</span>, x
                z = objItem.<span style="color: black;">IPSecPermitUDPPorts</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPSec Permit UDP Ports: &quot;</span>, x
                z = objItem.<span style="color: black;">IPSubnet</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Subnet: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Use Zero Broadcast: &quot;</span>, objItem.<span style="color: black;">IPUseZeroBroadcast</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Address: &quot;</span>, objItem.<span style="color: black;">IPXAddress</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Enabled: &quot;</span>, objItem.<span style="color: black;">IPXEnabled</span>
                z = objItem.<span style="color: black;">IPXFrameType</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Frame Type: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Media Type: &quot;</span>, objItem.<span style="color: black;">IPXMediaType</span>
                z = objItem.<span style="color: black;">IPXNetworkNumber</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                    a = <span style="color: #ff4500;">1</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Network Number: &quot;</span>, x
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IPX Virtual Net Number: &quot;</span>, objItem.<span style="color: black;">IPXVirtualNetNumber</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Keep Alive Interval: &quot;</span>, objItem.<span style="color: black;">KeepAliveInterval</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Keep Alive Time: &quot;</span>, objItem.<span style="color: black;">KeepAliveTime</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;MAC Address: &quot;</span>, objItem.<span style="color: black;">MACAddress</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;MTU: &quot;</span>, objItem.<span style="color: black;">MTU</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Num Forward Packets: &quot;</span>, objItem.<span style="color: black;">NumForwardPackets</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;PMTUBH Detect Enabled: &quot;</span>, objItem.<span style="color: black;">PMTUBHDetectEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;PMTU Discovery Enabled: &quot;</span>, objItem.<span style="color: black;">PMTUDiscoveryEnabled</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Service Name: &quot;</span>, objItem.<span style="color: black;">ServiceName</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Setting ID: &quot;</span>, objItem.<span style="color: black;">SettingID</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcpip Netbios Options: &quot;</span>, objItem.<span style="color: black;">TcpipNetbiosOptions</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcp Max Connect Retransmissions: &quot;</span>, objItem.<span style="color: black;">TcpMaxConnectRetransmissions</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcp Max Data Retransmissions: &quot;</span>, objItem.<span style="color: black;">TcpMaxDataRetransmissions</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcp Num Connections: &quot;</span>, objItem.<span style="color: black;">TcpNumConnections</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcp Use RFC1122 Urgent Pointer: &quot;</span>, objItem.<span style="color: black;">TcpUseRFC1122UrgentPointer</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Tcp Window Size: &quot;</span>, objItem.<span style="color: black;">TcpWindowSize</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WINS Enable LMHosts Lookup: &quot;</span>, objItem.<span style="color: black;">WINSEnableLMHostsLookup</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WINS Host Lookup File: &quot;</span>, objItem.<span style="color: black;">WINSHostLookupFile</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WINS Primary Server: &quot;</span>, objItem.<span style="color: black;">WINSPrimaryServer</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WINS Scope ID: &quot;</span>, objItem.<span style="color: black;">WINSScopeID</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;WINS Secondary Server: &quot;</span>, objItem.<span style="color: black;">WINSSecondaryServer</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------------------------<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Interface is disabled!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            count += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onRun<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">runDiagnostics</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">redir</span>.<span style="color: black;">filename</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># Restore stdout to normal</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span> = <span style="color: #dc143c;">sys</span>.__stdout__
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------         </span>
<span style="color: #808080; font-style: italic;"># Run the program</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = wx.<span style="color: black;">PySimpleApp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    frame = MyForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>As with most Python programs, this one starts out with various imports. Next we create a a simple class (RedirectText) that we will use to help us redirect stdout to a wx.TextCtrl and a log file. This works by passing in an instance of wx.TextCtrl and then setting &#8220;sys.stdout&#8221; to point to it (see the __init__ method in the MyForm class). Following the <em>RedirectText</em> class, we have the <em>MyForm</em> class which is where we create out wxPython GUI. There&#8217;s actually not much to the GUI itself. Just a multi-line text control and a button on a panel, but that&#8217;s all we need. The rest of the class is made up of methods that gather all our required information and log it to the screen and to a file.</p>
<p>Let&#8217;s take a look at those methods now! Note that these methods are called from the <em>runDiagnostics</em> method which is launched from the <em>onRun</em> button event handler.</p>
<h2>Getting the Route Table (AKA: IP Routes)</h2>
<p>When I looked into how to do this, I found the following script on another <a href="http://win32com.goermezer.de/content/view/220/284/">blog</a>:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">re</span>
route_table = <span style="color: black;">&#91;</span>elem.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> elem <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;route print&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Metric<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">re</span>.<span style="color: black;">match</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;^[0-9]&quot;</span>, elem.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre>
<p>I found that rather hard to follow, so I re-wrote it (or found another example and forgot to make a note of it) like this:</p>
<pre class="python">route_table = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;route print&quot;</span>, shell=<span style="color: #008000;">True</span>,
                stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    line = proc.<span style="color: black;">stdout</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    route_table.<span style="color: black;">append</span><span style="color: black;">&#40;</span>line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line: <span style="color: #ff7700;font-weight:bold;">break</span>
proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------------------------------------------------------------------------------------------&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Route Table:&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%20s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Network Destination&quot;</span>, <span style="color: #483d8b;">&quot;Netmask&quot;</span>,
                                  <span style="color: #483d8b;">&quot;Gateway&quot;</span>, <span style="color: #483d8b;">&quot;Interface&quot;</span>, <span style="color: #483d8b;">&quot;Metric&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> route <span style="color: #ff7700;font-weight:bold;">in</span> route_table:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>route<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">5</span>:
        dst, mask, gateway, interface, metric = route
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%20s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %15s<span style="color: #000099; font-weight: bold;">\t</span> %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>dst, mask, gateway, interface, metric<span style="color: black;">&#41;</span></pre>
<p>I find the code above much easier to read and understand. All it does is use the subprocess module to run &#8220;route print&#8221; and write the result to stdout. Don&#8217;t be confused by the <em>proc.stdout</em> above. That&#8217;s the process&#8217;s stdout, not the normal stdout. We want to redirect that data to the normal stdout! To do that, we read the proc&#8217;s stdout (or some might say, the PIPE) and append each line of data to a list. Then we create a nice custom output using Python&#8217;s string formatting. Now let&#8217;s take a look at how to use Python to run Ping and Tracert.</p>
<h2>Running Ping / Tracert with Python</h2>
<p>Pinging with Python is pretty easy. We just need the subprocess module to do it as you can see from this snippet:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> pingIP<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ip<span style="color: black;">&#41;</span>:
    proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ping %s&quot;</span> <span style="color: #66cc66;">%</span> ip, shell=<span style="color: #008000;">True</span>,
                            stdout=<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">PIPE</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        line = proc.<span style="color: black;">stdout</span>.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        wx.<span style="color: black;">Yield</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;&quot;</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> line.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> line: <span style="color: #ff7700;font-weight:bold;">break</span>
    proc.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>In this code, we use wx.Yield to send the ping results to our text control in real time. If we didn&#8217;t, then we wouldn&#8217;t receive any of the ping results until ping had finished running. Note that we also use an infinite loop to grab the results. Once the results stop coming, we break out of the loop. If you look at the tracert code, you&#8217;ll see that the only difference is in out <em>subprocess.Popen</em> command. This would be a good candidate for refactoring, but I&#8217;ll leave that as an exercise for the reader.</p>
<h2>Getting NIC Information with Python</h2>
<p>Microsoft has a whole set of Python scripts on their <a href="http://www.microsoft.com/technet/scriptcenter/scripts/python/pyindex.mspx?mfr=true">Technet </a>sub-site and I ended up using on them to get all kinds of good information on the Network Interface Cards (NIC) in our PCs. I won&#8217;t reproduce the code here since it&#8217;s long and we already have it above. However, it&#8217;s pretty easy to follow and I suspect that you could use WMI to get the same information if you know what you&#8217;re doing. The main part that we were interested in was the MAC and IP addresses. Let&#8217;s just extract that info from the long code and see how easy it is to get:</p>
<pre class="python">strComputer = <span style="color: #483d8b;">&quot;.&quot;</span>
objWMIService = win32com.<span style="color: black;">client</span>.<span style="color: black;">Dispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WbemScripting.SWbemLocator&quot;</span><span style="color: black;">&#41;</span>
objSWbemServices = objWMIService.<span style="color: black;">ConnectServer</span><span style="color: black;">&#40;</span>strComputer,<span style="color: #483d8b;">&quot;root<span style="color: #000099; font-weight: bold;">\c</span>imv2&quot;</span><span style="color: black;">&#41;</span>
colItems = objSWbemServices.<span style="color: black;">ExecQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Select * from Win32_NetworkAdapterConfiguration&quot;</span><span style="color: black;">&#41;</span>
numOfNics = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>colItems<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> objItem <span style="color: #ff7700;font-weight:bold;">in</span> colItems:
    z = objItem.<span style="color: black;">IPAddress</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> z <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
        a = <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> z:
             <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IP Address: &quot;</span>, x
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;MAC Address: &quot;</span>, objItem.<span style="color: black;">MACAddress</span></pre>
<p>Pretty easy, huh? And look! You use what looks like SQL syntax to run the query. This is why I think you can probably use WMI (in fact, that may be what it&#8217;s doing in an obtuse manner). Anyway, that&#8217;s really all there is to it.</p>
<h2>Wrapping Up</h2>
<p>Now you know the secrets to get various bits of networking information from your PC and how to redirect subprocess&#8217;s PIPEs to a log file and a wxPython text control. How you choose to use this information is up to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Restarting PCs with Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 15:47:25 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=738</guid>
		<description><![CDATA[Have you ever wanted to restart your Windows PC with out pressing Start, Shutdown or CTRL+ALT+DEL? What about restarting your annoying co-worker&#8217;s PC&#8230;the one who just doesn&#8217;t know when to shut up? Well, Python has the answer and this blog will tell you how to do it! Note: I don&#8217;t actually recommend rebooting your neighbor&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to restart your Windows PC with out pressing Start, Shutdown or CTRL+ALT+DEL? What about restarting your annoying co-worker&#8217;s PC&#8230;the one who just doesn&#8217;t know when to shut up? Well, Python has the answer and this blog will tell you how to do it!<span id="more-738"></span> Note: I don&#8217;t actually recommend rebooting your neighbor&#8217;s PC at random&#8230;</p>
<h2>Rebooting with PyWin32</h2>
<p>Anyway, back when I was first learning about Python, I stumbled upon an ActiveState recipe on how to do this. Of course, now I can&#8217;t seem to find that recipe, but I found a similar one here: http://code.activestate.com/recipes/360649/. Thus, we&#8217;ll start with this method first and then look at a slightly different approach. If you&#8217;d like to follow along, then you&#8217;ll need to make sure you have the <a href="http://sourceforge.net/projects/pywin32/files/">PyWin32 package</a>.</p>
<p>Here&#8217;s my version of the recipe from my foggy past:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># rebootServer.py</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> win32security
<span style="color: #ff7700;font-weight:bold;">import</span> win32api
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> ntsecuritycon <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AdjustPrivilege<span style="color: black;">&#40;</span>priv, enable=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Get the process token</span>
    flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY
    htoken = win32security.<span style="color: black;">OpenProcessToken</span><span style="color: black;">&#40;</span>win32api.<span style="color: black;">GetCurrentProcess</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, flags<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Get the ID for the system shutdown privilege.</span>
    idd = win32security.<span style="color: black;">LookupPrivilegeValue</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, priv<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Now obtain the privilege for this process.</span>
    <span style="color: #808080; font-style: italic;"># Create a list of the privileges to be added.</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> enable:
        newPrivileges = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>idd, SE_PRIVILEGE_ENABLED<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        newPrivileges = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>idd, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #808080; font-style: italic;"># and make the adjustment</span>
    win32security.<span style="color: black;">AdjustTokenPrivileges</span><span style="color: black;">&#40;</span>htoken, <span style="color: #ff4500;">0</span>, newPrivileges<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> RebootServer<span style="color: black;">&#40;</span>message=<span style="color: #483d8b;">'Rebooting'</span>, timeout=<span style="color: #ff4500;">30</span>, bForce=<span style="color: #ff4500;">0</span>, bReboot=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        <span style="color: #808080; font-style: italic;"># Now we remove the privilege we just added.</span>
        AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> AbortReboot<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        win32api.<span style="color: black;">AbortSystemShotdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        AdjustPrivilege<span style="color: black;">&#40;</span>SE_SHUTDOWN_NAME, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    RebootServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Aborting shutdown'</span>
    AbortReboot<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>I actually took out the ability to reboot remote machines in this snippet just because I didn&#8217;t want to be tempted to do so. If you want to reboot a machine on your network, change the following line:</p>
<pre class="python">win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span></pre>
<p>to</p>
<pre class="python">win32api.<span style="color: black;">InitiateSystemShutdown</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;someMachineName&quot;</span>, message, timeout, bForce, bReboot<span style="color: black;">&#41;</span></pre>
<p>or just add a host parameter to the <em>RebootServer</em> function and modify the InitiateSystemShutdown as necessary. If you just pass <em>None </em>in, then you&#8217;ll just reboot your own machine. The <em>AdjustPrivilege</em> method is used to change the privilege of the process so that it can actually shut the PC down. I think that only applies if you run the script as a user with limited privileges, but I almost never run that way so I can&#8217;t be sure. I do remember the recipe stating that you needed to remove the privileges that you added, which is what the <em>finally</em> statement is for. However, I would assume that once the reboot is complete, those privileges would have been revoked anyway.</p>
<h2>Rebooting with just Plain Ol&#8217; Python</h2>
<p>The other way to initiate a shutdown is to learn the magic of your Windows command line. There is a shutdown command that you can put into your Run dialog (go to Start &#8211;> Run) that will reboot your PC too. Here&#8217;s the one I&#8217;m talking about:  shutdown -r -t 1   <em>Note that this will only restart the local machine!</em></p>
<p>Now we just need to figure out how to make Python call that for us. Probably the simplest way to do so is to import the <em>os</em> module and call its <em>system</em> method. Let&#8217;s take a look:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -r -t 1&quot;</span><span style="color: black;">&#41;</span></pre>
<p>This is much shorter than the PyWin32 method, but I don&#8217;t know that it&#8217;s as robust. I used this method in a quick and dirty wxPython application I created for our Sun Ray virtual machines at work. One of the biggest problems with the Sun Ray system is that the user is connected to their virtual machine using Remote Desktop. A part of Remote Desktop is to hide the Shutdown button, so there is no simple way for the user to restart their PC unless they&#8217;re installed some kind of hotfix that does it for them. There are times when rebooting a machine is a good thing, so I took the script above and put it into the following application:</p>
<p><a href="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/03/restarter.png"><img src="http://www.blog.pythonlibrary.org/wp-content/uploads/2010/03/restarter.png" alt="" title="restarter" width="273" height="169" class="aligncenter size-full wp-image-739" /></a></p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> wx
<span style="color: #ff7700;font-weight:bold;">from</span> wx.<span style="color: black;">lib</span>.<span style="color: black;">buttons</span> <span style="color: #ff7700;font-weight:bold;">import</span> GenBitmapButton
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> RestarterPanel<span style="color: black;">&#40;</span>wx.<span style="color: black;">Panel</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Panel</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent=parent<span style="color: black;">&#41;</span>
&nbsp;
        img = wx.<span style="color: black;">Bitmap</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cute-logoff.png&quot;</span><span style="color: black;">&#41;</span>
        logoutBtn = GenBitmapButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span>, img, style=wx.<span style="color: black;">BORDER_NONE</span><span style="color: black;">&#41;</span>
        logoutBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onLogout</span><span style="color: black;">&#41;</span>
&nbsp;
        img = wx.<span style="color: black;">Bitmap</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;quick_restart.png&quot;</span><span style="color: black;">&#41;</span>
        restartBtn = GenBitmapButton<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, wx.<span style="color: black;">ID_ANY</span>, img, style=wx.<span style="color: black;">BORDER_NONE</span><span style="color: black;">&#41;</span>
        restartBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onRestart</span><span style="color: black;">&#41;</span>
&nbsp;
        cancelBtn = wx.<span style="color: black;">Button</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=<span style="color: #483d8b;">&quot;Cancel&quot;</span><span style="color: black;">&#41;</span>
        cancelBtn.<span style="color: black;">Bind</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">EVT_BUTTON</span>, <span style="color: #008000;">self</span>.<span style="color: black;">onCancel</span><span style="color: black;">&#41;</span>
&nbsp;
        vSizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">HORIZONTAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">buttonBuilder</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Logout&quot;</span>, logoutBtn<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">buttonBuilder</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Restart&quot;</span>, restartBtn<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        vSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>sizer, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span><span style="color: black;">&#41;</span>
        vSizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>cancelBtn, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALIGN_RIGHT</span>|wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">SetSizer</span><span style="color: black;">&#40;</span>vSizer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> buttonBuilder<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label, button<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Creates a button with a label underneath it and puts them into
        a vertical BoxSizer, which is then returned
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        font = wx.<span style="color: black;">Font</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">12</span>, wx.<span style="color: black;">SWISS</span>, wx.<span style="color: black;">NORMAL</span>, wx.<span style="color: black;">BOLD</span><span style="color: black;">&#41;</span>
        sizer = wx.<span style="color: black;">BoxSizer</span><span style="color: black;">&#40;</span>wx.<span style="color: black;">VERTICAL</span><span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>button, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">ALL</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
&nbsp;
        lbl = wx.<span style="color: black;">StaticText</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, label=label<span style="color: black;">&#41;</span>
        lbl.<span style="color: black;">SetFont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
        sizer.<span style="color: black;">Add</span><span style="color: black;">&#40;</span>lbl, <span style="color: #ff4500;">0</span>, wx.<span style="color: black;">CENTER</span>|wx.<span style="color: black;">BOTTOM</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> sizer
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onCancel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Close the dialog
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">GetParent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">Close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onLogout<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Logs the current user out
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -t 0 -l&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> onRestart<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        Restarts the PC
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;shutdown -r -t 1&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> RestarterFrame<span style="color: black;">&#40;</span>wx.<span style="color: black;">Frame</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">Frame</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">None</span>, size=<span style="color: black;">&#40;</span><span style="color: #ff4500;">273</span>, <span style="color: #ff4500;">169</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        panel = RestarterPanel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Center</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Main<span style="color: black;">&#40;</span>wx.<span style="color: black;">App</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, redirect=<span style="color: #008000;">False</span>, filename=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        wx.<span style="color: black;">App</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, redirect, filename<span style="color: black;">&#41;</span>
        dlg = RestarterFrame<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        dlg.<span style="color: black;">Show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    app = Main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    app.<span style="color: black;">MainLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>I looked at a lot of icons for this project and I think I ended up just doing searches for <a href="http://commons.wikimedia.org/wiki/File:Quick_restart.png">shutdown </a>and <a href="http://www.iconfinder.net/icondetails/32136/128/?q=cute">logout </a>icons and downloaded the ones I liked the best. Both of these free or had Creative Commons licenses.</p>
<p>Now you know the tricks to restart local and remote machines. Use this knowledge wisely!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python&#8217;s _winreg: Editing the Windows Registry</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 12:26:30 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=698</guid>
		<description><![CDATA[Python&#8217;s standard library is known for including lots of handy modules and packages that can be used without installing anything else. That&#8217;s one of the primary reasons that its standard library is called &#8220;batteries included&#8221; so often. So it should come as no surprise that Python includes a Windows only module for editing the Windows [...]]]></description>
			<content:encoded><![CDATA[<p>Python&#8217;s standard library is known for including lots of handy modules and packages that can be used without installing anything else. That&#8217;s one of the primary reasons that its standard library is called &#8220;batteries included&#8221; so often. So it should come as no surprise that Python includes a Windows only module for editing the Windows Registry. This particular module goes by the odd name of <em>_winreg</em> (odd because it starts with an underscore). In this article, we&#8217;ll learn the basics of working with the Registry using this &#8220;battery&#8221;. <span id="more-698"></span></p>
<h2>Reading from the Registry</h2>
<p>Using Python to read data from the registry is very easy. In the following example, we&#8217;ll find out where Outlook Express is installed:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
key = OpenKey<span style="color: black;">&#40;</span>HKEY_LOCAL_MACHINE, r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\O</span>utlook Express'</span>, <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
QueryValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;InstallRoot&quot;</span><span style="color: black;">&#41;</span></pre>
<p>On my machine, this will return the following tuple: (u&#8217;%ProgramFiles%\\Outlook Express&#8217;, 2). The tuple is made up of the value and the registry type of said value. There are two other query methods that one can use called QueryInfoKey and QueryValue. The former gives you information about the key itself in form of three integers while the latter retrieves only the data for a key’s first value that has a NULL name. The <a href="http://docs.python.org/library/_winreg.html">documentation</a> recommends that you use QueryValueEx whenever possible. </p>
<p>We should probably quickly explain what&#8217;s going on in the code above as well. The OpenKey function takes an HKEY* constant, a subkey path string, a reserved integer (which must be zero) and the security mask. In this case, we passed in KEY_ALL_ACCESS which gives us complete control of that key. Since all we were doing was reading it, we probably should have just used KEY_READ though. As for what QueryValueEx does, it just accepts a key object and the field name that we want to query against.</p>
<h2>Writing to the Registry</h2>
<p>If you&#8217;ve been reading this blog lately, then you&#8217;ve probably already seen the _winreg module used for writing to the registry. This will just be review for you, so feel free to skip this section. We&#8217;ll start out with a practical example. In the following code snippet, we will set Internet Explorer&#8217;s home page. As always, please note that editing Registry entries can be dangerous. Be sure to back up your registry before attempting to edit it. Now, on with the show!</p>
<pre class="python">keyVal = r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\I</span>nternet Explorer<span style="color: #000099; font-weight: bold;">\M</span>ain'</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
    key = OpenKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, keyVal, <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
    key = CreateKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, keyVal<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;Start Page&quot;</span>, <span style="color: #ff4500;">0</span>, REG_SZ, <span style="color: #483d8b;">&quot;http://www.blog.pythonlibrary.org/&quot;</span><span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>In the code above, we attempt to open the following key: <em>HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main</em> and set the &#8220;Start Page&#8221; value to this blog. If the open fails, it&#8217;s usually because the key doesn&#8217;t exist, so we try to create the key in our exception handler. Then we use SetValueEx to actually set the value and like all good programmers, we clean up when we&#8217;re done and close the key. If you skipped the CloseKey command, you&#8217;d be fine in this case as the script is done and Python will do it for you. However, if you continued working on this key, you might have an access violation since it&#8217;s already open. Thus, the lesson is to always close a key when you finish editing it.</p>
<h2>Other _winreg Methods</h2>
<p>There are several other methods in the _winreg library worth pointing out. The DeleteKey method is handy when you need to remove a key. Unfortunately, I&#8217;ve had occasion when I need to remove keys recursively, such as when an uninstall goes bad and _winreg has no built-in way of doing that. You can write your own, of course, or you can download a wrapper like <a href="http://code.activestate.com/recipes/476229-yarw-yet-another-registry-wrapper/">YARW</a> (Yet Another Registry Wrapper) that can do it for you.</p>
<p>DeleteValue is similar to DeleteKey, except that you delete just a value. Yes, it&#8217;s pretty obvious. If you&#8217;d wanted to write your own recursive key deleting code, then you&#8217;d probably want to take a look at EnumKey and EnumValue as they enumerate keys and values respectively. Let&#8217;s take a quick look at how to use EnumKey:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> EnumKey, HKEY_USERS
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    i = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        subkey = EnumKey<span style="color: black;">&#40;</span>HKEY_USERS, i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> subkey
        i += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
    <span style="color: #808080; font-style: italic;"># WindowsError: [Errno 259] No more data is available    </span>
    <span style="color: #ff7700;font-weight:bold;">pass</span></pre>
<p>The code above will loop through the HKEY_USERS hive printing the subkey to stdout until we reach the end of the hive and a WindowsError is raised. Of course, this does not descend into the subkeys, but I&#8217;ll leave that as an exercise for the reader to figure out.</p>
<p>The last method that we&#8217;ll talk about here is ConnectRegistry. This is helpful if we need to edit the Registry of a remote machine. It only accepts two arguments: the computer name and the key to connect to (i.e. HKEY_LOCAL_MACHINE or some such). Note that when connecting to remote machines, you can only edit certain keys while the others are unavailable. </p>
<h2>Wrapping Up</h2>
<p>I hope this was helpful for you and that it gave you lots of good ideas for your future projects. I have many login scripts that use this wonderful library and a couple that use YARW. It&#8217;s been quite useful so far and I hope that it will be the same for you.</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://docs.python.org/library/_winreg.html">Official documentation for _winreg</a></li>
<li><a href="http://effbot.org/librarybook/winreg.htm">Effbot tutorial on _winreg</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: Finding the Commit Charge Values in Windows</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 14:24:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=656</guid>
		<description><![CDATA[This week I was tasked with trying to find a way to find out what the Peak Commit value was on our virtual workstations. The reason being that we are trying to save money and were wondering if we were allocating too much memory or not. We didn&#8217;t need the Total Commit Charge or the [...]]]></description>
			<content:encoded><![CDATA[<p>This week I was tasked with trying to find a way to find out what the Peak Commit value was on our virtual workstations. The reason being that we are trying to save money and were wondering if we were allocating too much memory or not. We didn&#8217;t need the Total Commit Charge or the Limit Commit Charge values, but since I figured out how to get those during my research, I&#8217;ll show how to get those as well.<span id="more-656"></span></p>
<p>When I first started searching on this topic, I tried such search terms as &#8220;python peak commit value&#8221; and variations thereof. That got me nowhere, so I replaced &#8220;oython&#8221; with &#8220;wmi&#8221; and found the <a href="http://msdn.microsoft.com/en-us/library/aa394268%28VS.85%29.aspx">Win32_PerfFormattedData_PerfOS_Memory</a> Class on MSDN. I thought this was it, but it only gave me the Commit Charge Limit and the Total Commit Charge. Here&#8217;s how I got those values using Tim Golden&#8217;s WMI module:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> wmi
&nbsp;
c = wmi.<span style="color: black;">WMI</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> c.<span style="color: black;">Win32_PerfFormattedData_PerfOS_Memory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    commitChargeLimit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>item.<span style="color: black;">CommitLimit</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> c.<span style="color: black;">Win32_PerfFormattedData_PerfOS_Memory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    commitChargeTotal = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>item.<span style="color: black;">CommittedBytes</span><span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Commit Charge Limit: &quot;</span>, commitChargeLimit
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Commit Charge Total: &quot;</span>, commitChargeTotal</pre>
<p>This is neat stuff and shows how easy it is to take the documentation at MSDN and translate it into something usable in Python. Unfortunately, it just didn&#8217;t give me the information I needed. My next stop was the <a href="http://mail.python.org/mailman/listinfo/python-win32">PyWin32 mailing list</a> where Mark Hammond informed me of the win32pdh and win32pdhutil modules. These expose performance counters, but I couldn&#8217;t find a way to use it to get this information either. Fortunately, I found an old post on the <a href="http://forum.sysinternals.com/forum_posts.asp?TID=15540&#038;PID=75852">sysinternals forum </a> that gave me a clue. Here is what it said:</p>
<p><quote><br />
The only way I&#8217;m aware of that one can get this detail is from the uMmPeakCommitLimit member of the SYSTEM_PERFORMANCE_INFORMATION structure one passes to NtQuerySystemInformation when calling it with the SystemPerformanceInformation type.<br />
</quote></p>
<p>I asked Mr. Hammond if that meant that I would need to use <a href="http://python.net/crew/theller/ctypes/">ctypes</a> since the <a href="http://msdn.microsoft.com/en-us/library/ms724509%28VS.85%29.aspx">NtQuerySystemInformation</a> class isn&#8217;t exposed by PyWin32 and he said &#8220;probably&#8221;. The ctypes module is pretty low-level and not something I&#8217;ve used except when I&#8217;ve copied a script from ActiveState. It&#8217;s a pretty handy module though and was added to the <a href="http://docs.python.org/library/ctypes.html">standard library</a> in version 2.5. From what I can tell, it was created by Thomas Heller.</p>
<p>Anyway, ctypes has its own <a href="https://lists.sourceforge.net/lists/listinfo/ctypes-users">mailing list</a>, so I decided to try there. I received two replies, one of which was from the man himself (Heller). He gave me a script that didn&#8217;t appear to work at first, but after a little back and forth with him, he got me straightened out. Here&#8217;s the result:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
SystemBasicInformation = <span style="color: #ff4500;">0</span>
SystemPerformanceInformation = <span style="color: #ff4500;">2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SYSTEM_BASIC_INFORMATION<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved1&quot;</span>, c_long <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NumberOfProcessors&quot;</span>, c_byte<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bUnknown2&quot;</span>, c_byte<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bUnknown3&quot;</span>, c_short<span style="color: black;">&#41;</span>
                <span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SYSTEM_PERFORMANCE_INFORMATION<span style="color: black;">&#40;</span>Structure<span style="color: black;">&#41;</span>:
    _fields_ = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;IdleTime&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;OtherTransferCount&quot;</span>, c_int64<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;OtherOperationCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;AvailablePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalCommittedPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalCommitLimit&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PeakCommitment&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PageFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;WriteCopyFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TransitionFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved1&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DemandZeroFaults&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagesRead&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PageReadIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved2&quot;</span>, c_ulong <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagefilePagesWritten&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagefilePageWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MappedFilePagesWritten&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MappedFilePageWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolUsage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolUsage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolAllocs&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolFrees&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolAllocs&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NonPagedPoolFrees&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalFreeSystemPtes&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemCodePage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalSystemDriverPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;TotalSystemCodePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SmallNonPagedLookasideListAllocateHits&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SmallPagedLookasideListAllocateHits&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Reserved3&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MmSystemCachePage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PagedPoolPage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemDriverPage&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadResourceMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastReadNotPossible&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadResourceMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FastMdlReadNotPossible&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MapDataWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinMappedDataCount&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PinReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CopyReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadNoWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadWait&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadNoWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;MdlReadWaitMiss&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ReadAheadIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;LazyWriteIos&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;LazyWritePages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DataFlushes&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DataPages&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ContextSwitches&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FirstLevelTbFills&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SecondLevelTbFills&quot;</span>, c_ulong<span style="color: black;">&#41;</span>,
                <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SystemCalls&quot;</span>, c_ulong<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
sbi = SYSTEM_BASIC_INFORMATION<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
retlen = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
res = windll.<span style="color: black;">ntdll</span>.<span style="color: black;">NtQuerySystemInformation</span><span style="color: black;">&#40;</span>SystemBasicInformation,
                                            byref<span style="color: black;">&#40;</span>sbi<span style="color: black;">&#41;</span>,
                                            sizeof<span style="color: black;">&#40;</span>sbi<span style="color: black;">&#41;</span>,
                                            byref<span style="color: black;">&#40;</span>retlen<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> res, retlen
<span style="color: #ff7700;font-weight:bold;">print</span> sbi.<span style="color: black;">NumberOfProcessors</span>
&nbsp;
&nbsp;
spi = SYSTEM_PERFORMANCE_INFORMATION<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
retlen = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
res = windll.<span style="color: black;">ntdll</span>.<span style="color: black;">NtQuerySystemInformation</span><span style="color: black;">&#40;</span>SystemPerformanceInformation,
                                            byref<span style="color: black;">&#40;</span>spi<span style="color: black;">&#41;</span>,
                                            sizeof<span style="color: black;">&#40;</span>spi<span style="color: black;">&#41;</span>,
                                            byref<span style="color: black;">&#40;</span>retlen<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> res, retlen
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Peak commit: &quot;</span>,
<span style="color: #ff7700;font-weight:bold;">print</span> spi.<span style="color: black;">PeakCommitment</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">4096</span> / <span style="color: #ff4500;">1024</span></pre>
<p>I don&#8217;t really understand everything that&#8217;s going on here, but I&#8217;m glad it works. Well, I should say that it works on Windows XP Professional, 32-bit with Python 2.5. I tried it on Windows 7 64-bit as well and while the script ran, it returned &#8220;0L&#8221;. I&#8217;m guessing that 64-bit operating systems need a slightly different script, but since all our workstations currently use 32-bit, it doesn&#8217;t really matter at this point. Once again, the Python community came through for me and showed me just how amazing they are!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating Windows Shortcuts with Python (Part II)</title>
		<link>http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 00:57:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[PyWin32]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[System Admin]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=640</guid>
		<description><![CDATA[Back when I first wrote about creating shortcuts with Python last month, I kept thinking to myself that I had a 3rd way of doing it. Today, I had to maintain some of my shortcut code and I stumbled upon it once more. I also noticed that my post had received a comment from Tim [...]]]></description>
			<content:encoded><![CDATA[<p>Back when I first wrote about <a href="http://www.blog.pythonlibrary.org/2010/01/23/using-python-to-create-shortcuts/">creating shortcuts</a> with Python last month, I kept thinking to myself that I had a 3rd way of doing it. Today, I had to maintain some of my shortcut code and I stumbled upon it once more. I also noticed that my post had received a comment from Tim Golden on yet another way to create shortcuts, so I&#8217;ll include that in this post as well.<span id="more-640"></span></p>
<p>Oddly enough, my other method happened to use something of Tim Golden&#8217;s; namely, his <a href="http://timgolden.me.uk/python/winshell.html">winshell module</a>. Check it out below:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> winshell
&nbsp;
program_files = winshell.<span style="color: black;">programs</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
winshell.<span style="color: black;">CreateShortcut</span> <span style="color: black;">&#40;</span>
               Path=<span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span> <span style="color: black;">&#40;</span>program_files, <span style="color: #483d8b;">'My Shortcut.lnk'</span><span style="color: black;">&#41;</span>,
               Target=r<span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\P</span>rogram Files<span style="color: #000099; font-weight: bold;">\S</span>omeCoolProgram<span style="color: #000099; font-weight: bold;">\p</span>rog.exe'</span><span style="color: black;">&#41;</span></pre>
<p>Let&#8217;s unpack this so you know what&#8217;s going on. First, we use winshell to find the user&#8217;s &#8220;Programs&#8221; folder, which is a sub-folder of the user&#8217;s &#8220;Startup&#8221; folder. Then we use winshell&#8217;s <em>CreateShortcut </em> method to actually create the shortcut. If you look at the CreateShortcut&#8217;s docstring, you&#8217;ll discover that it can also accept the following keyword arguments: Arguments, StartIn, Icon, and Description.</p>
<p>Oddly enough, this wasn&#8217;t the method that Golden was suggesting in his comment. He had another <a href="http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html">idea </a>that is quite a bit more complicated. Let&#8217;s take a quick look:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pythoncom
<span style="color: #ff7700;font-weight:bold;">from</span> win32com.<span style="color: black;">shell</span> <span style="color: #ff7700;font-weight:bold;">import</span> shell, shellcon
&nbsp;
shortcut = pythoncom.<span style="color: black;">CoCreateInstance</span> <span style="color: black;">&#40;</span>
  shell.<span style="color: black;">CLSID_ShellLink</span>,
  <span style="color: #008000;">None</span>,
  pythoncom.<span style="color: black;">CLSCTX_INPROC_SERVER</span>,
  shell.<span style="color: black;">IID_IShellLink</span>
<span style="color: black;">&#41;</span>
program_location = r<span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\P</span>rogram Files<span style="color: #000099; font-weight: bold;">\S</span>omeCoolProgram<span style="color: #000099; font-weight: bold;">\p</span>rog.exe'</span>
shortcut.<span style="color: black;">SetPath</span> <span style="color: black;">&#40;</span>program_location<span style="color: black;">&#41;</span>
shortcut.<span style="color: black;">SetDescription</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My Program Does Something Really Cool!&quot;</span><span style="color: black;">&#41;</span>
shortcut.<span style="color: black;">SetIconLocation</span> <span style="color: black;">&#40;</span>program_location, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
desktop_path = shell.<span style="color: black;">SHGetFolderPath</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, shellcon.<span style="color: black;">CSIDL_DESKTOP</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
persist_file = shortcut.<span style="color: black;">QueryInterface</span> <span style="color: black;">&#40;</span>pythoncom.<span style="color: black;">IID_IPersistFile</span><span style="color: black;">&#41;</span>
persist_file.<span style="color: black;">Save</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span> <span style="color: black;">&#40;</span>desktop_path, <span style="color: #483d8b;">&quot;My Shortcut.lnk&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span></pre>
<p>In my opinion, this is getting a little too low-level for my taste. However, it is also really cool that you can do it this way. If you look at Tim Golden&#8217;s <a href="http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html">linked page</a>, you&#8217;ll see that this example is practically just a copy and paste job. I think winshell&#8217;s <em>desktop</em> method does the same thing as &#8220;shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)&#8221; and it&#8217;s easier to read. Anyway, the code above does the same thing that the previous code does, only it puts the shortcut on the user&#8217;s desktop. I don&#8217;t really understand what Golden is doing in the &#8220;pythoncom.CoCreateInstance&#8221; part other than creating an object that we can use to create our shortcut.</p>
<p>Also note that Tim Golden talks about creating a URL shortcut using a method that is quite similar to the one detailed above. You can go to his <a href="http://timgolden.me.uk/python/win32_how_do_i/create-a-url-shortcut.html">website</a> and learn all the juicy details.</p>
<p>Anyway, I felt that to be complete, I&#8217;d better write up these other two methods of creating shortcuts in Windows. Hopefully you found this interesting and maybe even educational.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python, Windows and Printers</title>
		<link>http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 03:26:40 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=591</guid>
		<description><![CDATA[I do a fair amount of technical support in addition to my software development. In our small shop, we get to troubleshoot anything that is related to technology, from networks to software to printers. I think one of the most annoying aspects is trying to get printers to work the way the user wants. Another [...]]]></description>
			<content:encoded><![CDATA[<p>I do a fair amount of technical support in addition to my software development. In our small shop, we get to troubleshoot anything that is related to technology, from networks to software to printers. I think one of the most annoying aspects is trying to get printers to work the way the user wants. Another issue is setting up printers for users that have to roam from PC to PC as a part of their job. These users usually only need the printers that are in their specific location at any given time. It&#8217;s very difficult to accommodate this type of user, especially if the PCs are being used 24/7, which is true in my case. This is where Python comes in.<span id="more-591"></span></p>
<p>In this article, I&#8217;ll show you how to access the currently installed printers on a machine, change which one is the default and install another printer. I&#8217;ll also show you how to access various bits and pieces about the printers that are installed as that information can be helpful in coding up other administrative scripts.</p>
<p>To follow along, you&#8217;ll need Python 2.4 &#8211; 3.x and the <a href="http://sourceforge.net/projects/pywin32/files/">PyWin32 package</a>.</p>
<p>For our first trick of the day, let&#8217;s find out what printers are currently installed on our PC:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> win32print
printers = win32print.<span style="color: black;">EnumPrinters</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> printers</pre>
<p>You can use different integers in the EnumPrinters call to get more or less information. See the <a href="http://docs.activestate.com/activepython/2.5/pywin32/win32print__EnumPrinters_meth.html">documentation </a>for more information (you may need to look at MSDN as well). Anyway, here&#8217;s is a sample output:</p>
<p><code><br />
((8388608, 'SnagIt 9,SnagIt 9 Printer,', 'SnagIt 9', ''), (8388608, 'Samsung ML-2250 Series PCL 6,Samsung ML-2250 Series PCL 6,', 'Samsung ML-2250 Series PCL 6', ''), (8388608, 'PDFCreator,PDFCreator,', 'PDFCreator', 'eDoc Printer'), (8388608, 'Microsoft XPS Document Writer,Microsoft XPS Document Writer,', 'Microsoft XPS Document Writer', ''))<br />
</code></p>
<p>As you can see, the EnumPrinters call returns a tuple with nested tuples. If I recall correctly, the last parameter will be a UNC path if the printer is a network printer. At my work place, we&#8217;ve had to decommission some servers that had printers on them and needed a way to change the user&#8217;s printer settings so that they pointed to the new path. Using the information gathered above made this much easier.</p>
<p>For example, if my script iterated over that list and found a printer that was using an obsolete UNC path, I could do something like this to fix it:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> win32print
win32print.<span style="color: black;">DeletePrinterConnection</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>oldUNC<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\t</span>o<span style="color: #000099; font-weight: bold;">\p</span>rinter'</span><span style="color: black;">&#41;</span>
win32print.<span style="color: black;">AddPrinterConnection</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>newUNC<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\t</span>o<span style="color: #000099; font-weight: bold;">\p</span>rinter'</span><span style="color: black;">&#41;</span></pre>
<p>An alternative way to install a printer is to use a low level command line call with the subprocess module:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">call</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'rundll32 printui.dll PrintUIEntry /in /q /n <span style="color: #000099; font-weight: bold;">\\</span>UNC<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\t</span>o<span style="color: #000099; font-weight: bold;">\p</span>rinter'</span><span style="color: black;">&#41;</span></pre>
<p>For the situation I mentioned above with roaming users, I also usually need to set the default printer so the user doesn&#8217;t accidentally print to a different department. There are two ways that I&#8217;ve found that work quite well. If you know the name of the printer, you can use the following:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> win32print
win32print.<span style="color: black;">SetDefaultPrinter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'EPSON Stylus C86 Series'</span><span style="color: black;">&#41;</span></pre>
<p>In the code above, I set the default to an Epson. The name should be exactly the same as the name displayed in the &#8220;Printers and Faxes&#8221; dialog in Windows (go to Start, Settings, Printers and Faxes on Windows XP). The other way to do this is with another subprocess call:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #dc143c;">subprocess</span>.<span style="color: black;">call</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'rundll32 printui.dll PrintUIEntry /y /n <span style="color: #000099; font-weight: bold;">\\</span>UNC<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\t</span>o<span style="color: #000099; font-weight: bold;">\p</span>rinter'</span><span style="color: black;">&#41;</span></pre>
<p>There are many additional functions that win32print supports. You can start and stop print jobs, set priority levels on the print jobs, get the printer&#8217;s configuration, schedule jobs and much more. I hope you found this helpful.</p>
<p><b>Further Reading</b></p>
<ul>
<li><a href="http://docs.activestate.com/activepython/2.5/pywin32/win32print.html">win32print documentation</a></li>
<li><a href="http://timgolden.me.uk/python/wmi/cookbook.html#watch-for-new-print-jobs">Watch for new print jobs with WMI</a></li>
<li><a href="http://timgolden.me.uk/python/wmi/cookbook.html#show-print-jobs">Show Print Jobs with WMI</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Python to Reduce the Roaming Profile</title>
		<link>http://www.blog.pythonlibrary.org/2010/02/09/using-python-to-reduce-the-roaming-profile/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/02/09/using-python-to-reduce-the-roaming-profile/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 00:35:31 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=575</guid>
		<description><![CDATA[Roaming Profiles are a blessing and a curse. If the user uses the internet, their browser&#8217;s cached files will grow like mad. If the user downloads programs to their desktop or creates large Powerpoint files anywhere in their profile, then they have to be managed whenever the user logs in or out. There are several [...]]]></description>
			<content:encoded><![CDATA[<p>Roaming Profiles are a blessing and a curse. If the user uses the internet, their browser&#8217;s cached files will grow like mad. If the user downloads programs to their desktop or creates large Powerpoint files anywhere in their profile, then they have to be managed whenever the user logs in or out. There are several solutions to this problem: disk quotas, blocking the ability to download or put stuff in one&#8217;s profile, etc. In this article, I will show you how to exclude specific directories from the user&#8217;s profile using Python.<span id="more-575"></span></p>
<p>This is basically just a Windows Registry hack. As always, be sure to back up your Registry before applying any changes to it in case something goes horribly awry and you make your machine unbootable. </p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    key = OpenKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, r<span style="color: #483d8b;">&quot;SOFTWARE<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows NT<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\W</span>inlogon&quot;</span>,
                  <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
    key = CreateKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, r<span style="color: #483d8b;">&quot;SOFTWARE<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows NT<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\W</span>inlogon&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Exclude directories from roaming profile </span>
prof_dirs = <span style="color: #483d8b;">&quot;Local Settings;Temporary Internet Files;History;Temp;My Documents;Recent&quot;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;ExcludeProfileDirs&quot;</span>, <span style="color: #ff4500;">0</span>, REG_SZ, prof_dirs<span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>This code is pretty simple. First we imported the various modules and constants from _winreg. Then we tried to open the appropriate Registry key and created it if the key didn&#8217;t already exist. Next we created a string of semi-colon delimited directories to exclude from the roaming profile. Finally, we set the appropriate value and closed the key.</p>
<p>And that&#8217;s all there is to this simple script!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/02/09/using-python-to-reduce-the-roaming-profile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Enabling Screen Locking with Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/02/09/enabling-screen-locking-with-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/02/09/enabling-screen-locking-with-python/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 15:11:38 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=569</guid>
		<description><![CDATA[A few months ago, my employer needed to lock down some of our workstations to be compliant with some new software we were installing from another government organization. We needed to force those machines to lock after so many minutes elapsed and we needed to make it such that the user could not change those [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago, my employer needed to lock down some of our workstations to be compliant with some new software we were installing from another government organization. We needed to force those machines to lock after so many minutes elapsed and we needed to make it such that the user could not change those settings.  In this article, you&#8217;ll find out how do this and as a bonus, I&#8217;ll also show you how to lock your Windows machine on demand with Python.<span id="more-569"></span></p>
<h2>Hacking the Registry to Lock the Machine</h2>
<p>To start, we&#8217;ll take a look at my original script and then we&#8217;ll refactor it a bit to make the code better:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> CreateKey, SetValueEx
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> HKEY_CURRENT_USER, HKEY_USERS
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> REG_DWORD, REG_SZ
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    i = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        subkey = EnumKey<span style="color: black;">&#40;</span>HKEY_USERS, i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>subkey<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">30</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
        i += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
    <span style="color: #808080; font-style: italic;"># WindowsError: [Errno 259] No more data is available</span>
    <span style="color: #808080; font-style: italic;"># looped through all the subkeys without finding the right one</span>
    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">WindowsError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Could not apply workstation lock settings!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
keyOne = CreateKey<span style="color: black;">&#40;</span>HKEY_USERS, r<span style="color: #483d8b;">'%s<span style="color: #000099; font-weight: bold;">\C</span>ontrol Panel<span style="color: #000099; font-weight: bold;">\D</span>esktop'</span> <span style="color: #66cc66;">%</span> subkey<span style="color: black;">&#41;</span>
keyTwo = CreateKey<span style="color: black;">&#40;</span>HKEY_CURRENT_USER, r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\P</span>olicies<span style="color: #000099; font-weight: bold;">\S</span>ystem'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># enable screen saver security</span>
SetValueEx<span style="color: black;">&#40;</span>keyOne, <span style="color: #483d8b;">'ScreenSaverIsSecure'</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># set screen saver timeout</span>
SetValueEx<span style="color: black;">&#40;</span>keyOne, <span style="color: #483d8b;">'ScreenSaveTimeOut'</span>, <span style="color: #ff4500;">0</span>, REG_SZ, <span style="color: #483d8b;">'420'</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># set screen saver</span>
SetValueEx<span style="color: black;">&#40;</span>keyOne, <span style="color: #483d8b;">'SCRNSAVE.EXE'</span>, <span style="color: #ff4500;">0</span>, REG_SZ, <span style="color: #483d8b;">'logon.scr'</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;"># disable screen saver tab</span>
SetValueEx<span style="color: black;">&#40;</span>keyTwo, <span style="color: #483d8b;">'NoDispScrSavPage'</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
CloseKey<span style="color: black;">&#40;</span>keyOne<span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>keyTwo<span style="color: black;">&#41;</span></pre>
<p>It took a while to discover this, but to set the right key, we need to find the first sub-key that is larger than 30 characters in length under the HKEY_USERS hive. I&#8217;m sure there&#8217;s probably a better way to do this, but I haven&#8217;t found it yet. Anyway, once we&#8217;ve found the long key, we break out of the loop and open the keys we need or create them if they don&#8217;t already exist. This is the reason that we use CreateKey since it will do just that.  Next, we set four values and then we close the keys to apply the new settings. You can read the comments to see what each key does. Now let&#8217;s refine the code a bit to make it into a function:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> modifyRegistry<span style="color: black;">&#40;</span>key, sub_key, valueName, valueType, value<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
    A simple function used to change values in
    the Windows Registry.
    &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        key_handle = OpenKey<span style="color: black;">&#40;</span>key, sub_key, <span style="color: #ff4500;">0</span>, KEY_ALL_ACCESS<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
        key_handle = CreateKey<span style="color: black;">&#40;</span>key, sub_key<span style="color: black;">&#41;</span>
&nbsp;
    SetValueEx<span style="color: black;">&#40;</span>key_handle, valueName, <span style="color: #ff4500;">0</span>, valueType, value<span style="color: black;">&#41;</span>
    CloseKey<span style="color: black;">&#40;</span>key_handle<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    i = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        subkey = EnumKey<span style="color: black;">&#40;</span>HKEY_USERS, i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>subkey<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">30</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
        i += <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">WindowsError</span>:
    <span style="color: #808080; font-style: italic;"># WindowsError: [Errno 259] No more data is available</span>
    <span style="color: #808080; font-style: italic;"># looped through all the subkeys without finding the right one</span>
    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">WindowsError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Could not apply workstation lock settings!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
subkey = r<span style="color: #483d8b;">'%s<span style="color: #000099; font-weight: bold;">\C</span>ontrol Panel<span style="color: #000099; font-weight: bold;">\D</span>esktop'</span> <span style="color: #66cc66;">%</span> subkey
data= <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ScreenSaverIsSecure'</span>, REG_DWORD, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,
              <span style="color: black;">&#40;</span><span style="color: #483d8b;">'ScreenSaveTimeOut'</span>, REG_SZ, <span style="color: #483d8b;">'420'</span><span style="color: black;">&#41;</span>,
              <span style="color: black;">&#40;</span><span style="color: #483d8b;">'SCRNSAVE.EXE'</span>, REG_SZ, <span style="color: #483d8b;">'logon.scr'</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> valueName, valueType, value <span style="color: #ff7700;font-weight:bold;">in</span> data:
    modifyRegistry<span style="color: black;">&#40;</span>HKEY_USERS, subkey, valueName,
                   valueType, value<span style="color: black;">&#41;</span>
&nbsp;
modifyRegistry<span style="color: black;">&#40;</span>HKEY_CURRENT_USER,
               r<span style="color: #483d8b;">'Software<span style="color: #000099; font-weight: bold;">\M</span>icrosoft<span style="color: #000099; font-weight: bold;">\W</span>indows<span style="color: #000099; font-weight: bold;">\C</span>urrentVersion<span style="color: #000099; font-weight: bold;">\P</span>olicies<span style="color: #000099; font-weight: bold;">\S</span>ystem'</span>,
               <span style="color: #483d8b;">'NoDispScrSavPage'</span>, REG_DWORD, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre>
<p>As you can see, first we import everything in the <em>_winreg</em> module. This isn&#8217;t really recommended as you can accidentally overwrite functions that you&#8217;ve imported, which is why this is sometimes called &#8220;poisoning the namespace&#8221;. However, almost every example I&#8217;ve ever seen that uses the _winreg modules does it that way. See the first example for the correct way to import from it.</p>
<p>Next, we create a general purpose function that can open the key, or create the key if it&#8217;s not already there. The function will also set the value and close the key for us. After that, we do basically the same thing that we did in the previous example: we loop over the HKEY_USERS hive and break appropriately. To mix things up a bit, we create a <em>data</em> variable that holds a list of tuples. We loop over that and call our function with the appropriate parameters and for good measure, we demonstrate how to call it outside of a loop.</p>
<h2>Locking the Machine Programmatically</h2>
<p>Now you may be thinking that we already covered how to lock the machine programmatically. Well, we did in a sense; but what we really did was set up a timer to lock the machine sometime in the future when the machine has been idle. What if we want to lock the machine now? Some of you are probably thinking we should just hit the Windows key plus &#8220;L&#8221; and that is a good idea. However, the reason I created this script is because I have to remotely connect to my machine with VNC from time-to-time and I need to go through multiple steps to lock the machine when using VNC whereas if you have Python set up correctly, you can just double-click a script file and have it do the locking for you. That&#8217;s what this little script does:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
&nbsp;
winpath = <span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;windir&quot;</span><span style="color: black;">&#93;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span>winpath + r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\s</span>ystem32<span style="color: #000099; font-weight: bold;">\r</span>undll32 user32.dll, LockWorkStation'</span><span style="color: black;">&#41;</span></pre>
<p>This three line script imports the <em>os</em> module, grabs the Windows directory using its <em>environ </em>method and then calls <em>os.system</em> to lock the machine. If you were to open a DOS window on your machine and type the following into it, you would have the exact same effect:</p>
<p><code><br />
C:\windows\system32\rundll32 user32.dll, LockWorkStation<br />
</code></p>
<h2>Wrapping Up</h2>
<p>Now you know how to lock your machine with Python. If you put the first example in a login script, then you can use it lock down some or all the machines on your network. This is very handy if you have users that like to wander off or go to lots of meetings, but leave their machines logged in. It protects them from snooping and can protect your company from espionage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/02/09/enabling-screen-locking-with-python/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Lock Down Windows with Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 21:40:34 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=560</guid>
		<description><![CDATA[Almost four years ago, I was tasked with converting a Kixtart script to Python. This particular script was used to lock down Windows XP machines so they could be used as kiosks. Obviously, you don&#8217;t need Python to do this. Any programming language that can access the Windows Registry will be able to do this [...]]]></description>
			<content:encoded><![CDATA[<p>Almost four years ago, I was tasked with converting a <a href="http://www.kixtart.org/">Kixtart</a> script to Python. This particular script was used to lock down Windows XP machines so they could be used as kiosks. Obviously, you don&#8217;t need Python to do this. Any programming language that can access the Windows Registry will be able to do this or you could just use Group Policies. But this is a Python blog, so that&#8217;s what you&#8217;re going to get in this article!<span id="more-560"></span></p>
<h2>Getting Started</h2>
<p>All you need for this tutorial is the standard Python distribution and the <a href="http://sourceforge.net/projects/pywin32/files/">PyWin32 package</a>. The modules we&#8217;ll be using are <em>_winreg</em> and <em>subprocess</em>, which are built into the standard distribution and <em>win32api </em>and <em>win32con</em> from PyWin32. </p>
<p>We&#8217;ll look at the code in two halves. The first half will use the <em>_winreg</em> module:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>, win32con
<span style="color: #ff7700;font-weight:bold;">from</span> win32api <span style="color: #ff7700;font-weight:bold;">import</span> SetFileAttributes
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Note: 1 locks the machine, 0 opens the machine.      </span>
UserPolicySetting = <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Connect to the correct Windows Registry key and path, then open the key</span>
reg = ConnectRegistry<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, HKEY_CURRENT_USER<span style="color: black;">&#41;</span>
regpath = r<span style="color: #483d8b;">&quot;Software<span style="color: #000099; font-weight: bold;">\\</span>Microsoft<span style="color: #000099; font-weight: bold;">\\</span>Windows<span style="color: #000099; font-weight: bold;">\\</span>CurrentVersion<span style="color: #000099; font-weight: bold;">\\</span>Policies<span style="color: #000099; font-weight: bold;">\\</span>Explorer&quot;</span>
key = OpenKey<span style="color: black;">&#40;</span>reg, regpath, <span style="color: #ff4500;">0</span>, KEY_WRITE<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Edit registry key by adding new values (Lock-down the PC)</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoRecentDocsMenu&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoRun&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoFavoritesMenu&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoFind&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoSetFolders&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoSetTaskbar&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoSetActiveDesktop&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoWindowsUpdate&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoSMHelp&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoCloseDragDropBands&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoActiveDesktopChanges&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoMovingBands&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoViewContextMenu&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoChangeStartMenu&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;NoTrayContextMenu&quot;</span>, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>To start, we import the modules we need. Then we connect to the Windows Registry and open the following key for writing:</p>
<p>HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer</p>
<p><strong>WARNING: Before continuing, it should be noted that messing with the Windows Registry can cause your PC harm if you do it incorrectly. If you do not know what you&#8217;re doing, back up your registry before doing anything or use a virtual machine that you can restore (like VirtualBox or VMWare). The script above will make your PC mostly unusable for anything other than a kiosk.</strong></p>
<p>Anyway, after opening the key, we set over a dozen settings that hide the Run option, Favorites, Recent, and Find from the Start Menu. We also disable right-clicking on the desktop, Windows Update and all changes to various menus (among other things). This is all controlled by our <em>UserPolicySetting</em> variable. If it&#8217;s set to one (i.e. boolean True), it will lock down all these values; if it&#8217;s zero, then it enables all the settings again. At the end, we call <em>CloseKey</em> to apply the settings.</p>
<p>We should stop here a moment and consider that this is some truly ugly code (as pointed out in the comments below). I wrote this when I had only a month of so of programming experience and I was porting from a Kixtart script that looked as bad or worse than this one. Let&#8217;s try to make it cleaner:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">_winreg</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
UserPolicySetting = <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Connect to the correct Windows Registry key and path, then open the key</span>
reg = ConnectRegistry<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, HKEY_CURRENT_USER<span style="color: black;">&#41;</span>
regpath = r<span style="color: #483d8b;">&quot;Software<span style="color: #000099; font-weight: bold;">\\</span>Microsoft<span style="color: #000099; font-weight: bold;">\\</span>Windows<span style="color: #000099; font-weight: bold;">\\</span>CurrentVersion<span style="color: #000099; font-weight: bold;">\\</span>Policies<span style="color: #000099; font-weight: bold;">\\</span>Explorer&quot;</span>
key = OpenKey<span style="color: black;">&#40;</span>reg, regpath, <span style="color: #ff4500;">0</span>, KEY_WRITE<span style="color: black;">&#41;</span>
&nbsp;
sub_keys = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;NoRecentDocsMenu&quot;</span>, <span style="color: #483d8b;">&quot;NoRun&quot;</span>, <span style="color: #483d8b;">&quot;NoFavoritesMenu&quot;</span>,
            <span style="color: #483d8b;">&quot;NoFind&quot;</span>, <span style="color: #483d8b;">&quot;NoSetFolders&quot;</span>, <span style="color: #483d8b;">&quot;NoSetTaskbar&quot;</span>,
            <span style="color: #483d8b;">&quot;NoSetActiveDesktop&quot;</span>, <span style="color: #483d8b;">&quot;NoWindowsUpdate&quot;</span>,
            <span style="color: #483d8b;">&quot;NoSMHelp&quot;</span>, <span style="color: #483d8b;">&quot;NoCloseDragDropBands&quot;</span>,
            <span style="color: #483d8b;">&quot;NoActiveDesktopChanges&quot;</span>, <span style="color: #483d8b;">&quot;NoMovingBands&quot;</span>,
            <span style="color: #483d8b;">&quot;NoViewContextMenu&quot;</span>, <span style="color: #483d8b;">&quot;NoChangeStartMenu&quot;</span>,
            <span style="color: #483d8b;">&quot;NoTrayContextMenu&quot;</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Edit registry key by adding new values (Lock-down the PC)</span>
<span style="color: #ff7700;font-weight:bold;">for</span> sub_key <span style="color: #ff7700;font-weight:bold;">in</span> sub_keys:
    SetValueEx<span style="color: black;">&#40;</span>key, sub_key, <span style="color: #ff4500;">0</span>, REG_DWORD, UserPolicySetting<span style="color: black;">&#41;</span>
&nbsp;
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>The main difference here is that I took all the sub_key names and put them into a Python list that we can then iterate over, setting each to the right value. Now, if we had multiple differences in these lines of code, such as a mixture of REG_DWORD and REG_SZ, then we would need to do something different. For example, we&#8217;d need to iterate over a list of tuples instead or maybe create a function to pass the information to. If you want the most flexibility, you could create a class that does this for you in addition to error handing and the opening and closing of the keys. I&#8217;ll leave that as an exercise for the reader though.</p>
<p>The other half of our script will hide various icons and folders:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># Sets the &quot;Hidden&quot; attribute for specified files/folders.</span>
<span style="color: #ff7700;font-weight:bold;">if</span> UserPolicySetting == <span style="color: #ff4500;">1</span>:
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\S</span>et Program Access and Defaults.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\W</span>indows Catalog.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\O</span>pen Office Document.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\N</span>ew Office Document.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># NOTE: Two backslashes are required before the last directory only</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib +h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>vnc&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib +h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Outlook Express&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib +h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Java Web Start&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib +h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Microsoft Office&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib +h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Microsoft SQL Server&quot;'</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\A</span>dobe Reader 6.0.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\W</span>indows Media Player.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\W</span>indows Movie Maker.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\S</span>et Program Access and Defaults.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\W</span>indows Catalog.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\O</span>pen Office Document.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\N</span>ew Office Document.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib -h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>vnc&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib -h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Outlook Express&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib -h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Java Web Start&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib -h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Microsoft Office&quot;'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib -h &quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\\</span>Microsoft SQL Server&quot;'</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\A</span>dobe Reader 6.0.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\W</span>indows Media Player.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span>
    SetFileAttributes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu<span style="color: #000099; font-weight: bold;">\P</span>rograms<span style="color: #000099; font-weight: bold;">\W</span>indows Movie Maker.lnk&quot;</span>, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span></pre>
<p>This snippet hides the following</p>
<ul>
<li>Various links to Microsoft Office documents that usually appear in the Start Menu</li>
<li>Windows Catalog and the Set Program Access and Defaults shortcuts</li>
<li>Various folders in the Start Menu&#8217;s Programs sub-menu that we don&#8217;t want user&#8217;s to access, such as VNC, Office, the Microsoft SQL Connector, etc.</li>
<li>Other links in the Programs sub-menu</li>
</ul>
<p>This is accomplished through a combination of win32api&#8217;s SetFileAttributes and win32con&#8217;s file attributes for the shortcuts. The folder&#8217;s hidden status are toggled using a subprocess call to Windows <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/attrib.mspx?mfr=true">attrib command</a>. </p>
<p>Unfortunately, this is another example of repetitious code. Let&#8217;s take a moment and try to refactor this. It looks like everything that we&#8217;re setting is in the Start Menu or its Programs subfolder. We can probably take those paths and put them into a loop like we did with the previous example. We can also use the flag to tell us when to hide or show the folders. We&#8217;ll put the whole thing into a function to make it easier to reuse too. Let&#8217;s see how that looks:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32con
<span style="color: #ff7700;font-weight:bold;">from</span> win32api <span style="color: #ff7700;font-weight:bold;">import</span> SetFileAttributes
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> toggleStartItems<span style="color: black;">&#40;</span>flag=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>:
    items = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;Set Program Access and Defaults.lnk&quot;</span>, <span style="color: #483d8b;">&quot;Windows Catalog.lnk&quot;</span>,
             <span style="color: #483d8b;">&quot;Open Office Document.lnk&quot;</span>, <span style="color: #483d8b;">&quot;New Office Document.lnk&quot;</span>,
             <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\A</span>dobe Reader 6.0.lnk&quot;</span>, <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\W</span>indows Media Player.lnk&quot;</span>,
             <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\W</span>indows Movie Maker.lnk&quot;</span>, <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\\</span>vnc&quot;</span>,
             <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\\</span>Outlook Express&quot;</span>, <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\\</span>Java Web Start&quot;</span>,
             <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\\</span>Microsoft Office&quot;</span>, <span style="color: #483d8b;">&quot;Programs<span style="color: #000099; font-weight: bold;">\\</span>Microsoft SQL Server&quot;</span><span style="color: black;">&#93;</span>
    path = r<span style="color: #483d8b;">'C:<span style="color: #000099; font-weight: bold;">\D</span>ocuments and Settings<span style="color: #000099; font-weight: bold;">\A</span>ll Users<span style="color: #000099; font-weight: bold;">\S</span>tart Menu'</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> flag:
        toggle = <span style="color: #483d8b;">&quot;+h&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        toggle = <span style="color: #483d8b;">&quot;-h&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> items:
        p = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>path, item<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'attrib %s &quot;%s&quot;'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>toggle, p<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> flag:
            SetFileAttributes<span style="color: black;">&#40;</span>p, win32con.<span style="color: black;">FILE_ATTRIBUTE_HIDDEN</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            SetFileAttributes<span style="color: black;">&#40;</span>p, win32con.<span style="color: black;">FILE_ATTRIBUTE_NORMAL</span><span style="color: black;">&#41;</span></pre>
<p>Now, doesn&#8217;t that look much nicer? It also makes adding and removing items easier. This leads for simpler maintenance and less headaches in the future.</p>
<h2>Wrapping Up</h2>
<p>Now you know how to create a kiosk of your own with Windows and Python. I hope you found this article helpful.</p>
<p><em>Note: These scripts are tested using Python 2.4+ on Windows XP</em></p>
<p><strong>Further Reading</strong></p>
<ul>
<li><a href="http://docs.python.org/library/_winreg.html">The Official _winreg documentation</a></li>
<li><a href="http://docs.activestate.com/activepython/2.5/pywin32/PyWin32.html">The Official PyWin32 Documentation</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/02/06/lock-down-windows-with-python/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 4.695 seconds -->
