<?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, 08 Jan 2012 12:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python: Changing Microsoft Office User Initials</title>
		<link>http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 23:37:36 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Microsoft Office]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[System Admin]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1350</guid>
		<description><![CDATA[A couple of months ago at work, we received a report that a file was locked. The dialog that appeared showed the initials of a user who wasn&#8217;t even working for us any more. Thus we discovered an annoying bug that can crop up with Office. Basically, a user is asked by Word or Excel [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/" data-url="http://bit.ly/sLXXCS" data-text="Python: Changing Microsoft Office User Initials" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/";
			reddit_title = "Python: Changing Microsoft Office User Initials";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/"></g:plusone></div></div><p>A couple of months ago at work, we received a report that a file was locked. The dialog that appeared showed the initials of a user who wasn&#8217;t even working for us any more. Thus we discovered an annoying bug that can crop up with Office. Basically, a user is asked by Word or Excel to input their name and initials during the first run of that respective application and it will keep that data no matter who logs into the machine later on. This can lead to some serious confusion when we get error messages of this sort. Anyway, let&#8217;s take a quick look at how to get this done.<span id="more-1350"></span></p>
<p>We will be using Python&#8217;s <strong>_winreg</strong> module for this hack. You can see said hack below:</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;
key = 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;">\O</span>ffice<span style="color: #000099; font-weight: bold;">\1</span>1.0<span style="color: #000099; font-weight: bold;">\C</span>ommon<span style="color: #000099; font-weight: bold;">\U</span>serInfo'</span><span style="color: black;">&#41;</span>
res = QueryValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;UserInitials&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">repr</span> <span style="color: black;">&#40;</span>res<span style="color: black;">&#41;</span>
&nbsp;
username = u<span style="color: #483d8b;">&quot;mldr<span style="color: #000099; font-weight: bold;">\0</span>&quot;</span>
SetValueEx<span style="color: black;">&#40;</span>key, <span style="color: #483d8b;">&quot;UserInitials&quot;</span>, <span style="color: #ff4500;">0</span>, REG_BINARY, username<span style="color: black;">&#41;</span>
CloseKey<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span></pre>
<p>Here we use the <strong>CreateKey</strong> method just in case the key doesn&#8217;t already exist. If the key does exist, then CreateKey will just open it. The first half of the script was used for checking to see if the key had the correct value in it. The last three lines overwrite the value with my initials. I can&#8217;t remember why I had to make a unicode string, but the guys on PyWin32 told me that was the way to do it. I can tell you that I wasn&#8217;t ever able to get a plain string to work. Once the value is set, we clean up after ourselves by closing the key.</p>
<p>That&#8217;s it! Easy, eh? Have fun with Python!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/10/27/python-changing-microsoft-office-user-initials/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Find and List All Running Processes with Python</title>
		<link>http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/#comments</comments>
		<pubDate>Sun, 03 Oct 2010 18:34:46 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<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=1271</guid>
		<description><![CDATA[The other day, I was tasked with finding a way to get a list of all running processes on a Windows XP virtual machine. I was also supposed to include information about how much CPU and memory each process used. Fortunately, this didn&#8217;t have to be a remote script, but one that could be run [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/" data-url="http://bit.ly/sJkCrz" data-text="How to Find and List All Running Processes with Python" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/";
			reddit_title = "How to Find and List All Running Processes with Python";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/"></g:plusone></div></div><p>The other day, I was tasked with finding a way to get a list of all running processes on a Windows XP virtual machine. I was also supposed to include information about how much CPU and memory each process used. Fortunately, this didn&#8217;t have to be a remote script, but one that could be run on the client. After a fair bit of Googling here and there, I finally found a solution. In this article, we&#8217;ll look at some of the rejects as well as the eventual solution, which happens to work cross-platform.<span id="more-1271"></span></p>
<p>One of the first scripts I found was <a href="http://mail.python.org/pipermail/python-win32/2006-March/004340.html">this one</a> back from March of 2006:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># http://mail.python.org/pipermail/python-win32/2006-March/004340.html</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span>
wmi=win32com.<span style="color: black;">client</span>.<span style="color: black;">GetObject</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'winmgmts:'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> wmi.<span style="color: black;">InstancesOf</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'win32_process'</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> p.<span style="color: black;">Name</span>, p.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ProcessId'</span><span style="color: black;">&#41;</span>, \
        <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'UserModeTime'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span><span style="color: black;">&#41;</span>+<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>p.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'KernelModeTime'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span><span style="color: black;">&#41;</span>
    children=wmi.<span style="color: black;">ExecQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Select * from win32_process where ParentProcessId=%s'</span> <span style="color: #66cc66;">%</span>p.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ProcessId'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> child <span style="color: #ff7700;font-weight:bold;">in</span> children:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\t</span>'</span>,child.<span style="color: black;">Name</span>,child.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ProcessId'</span><span style="color: black;">&#41;</span>, \
            <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>child.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'UserModeTime'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span><span style="color: black;">&#41;</span>+<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>child.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'KernelModeTime'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span><span style="color: black;">&#41;</span></pre>
<p>This script requires the <a href="http://sourceforge.net/projects/pywin32/">PyWin32 package</a> to work. However, while it&#8217;s a handy little script, it doesn&#8217;t show anything that I want except the ProcessId. I don&#8217;t really care about the user or kernel mode times (i.e. the total CPU time by user or kernel). Also I don&#8217;t really like working with the black magic of COM, so I ended up rejecting this one out of hand.</p>
<p>Next up was an <a href="http://code.activestate.com/recipes/303339-getting-process-information-on-windows/">ActiveState recipe</a>. It looked promising:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># http://code.activestate.com/recipes/303339-getting-process-information-on-windows/</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32pdh, <span style="color: #dc143c;">string</span>, win32api
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> procids<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;">#each instance is a process, you can have multiple processes w/same name</span>
    junk, instances = win32pdh.<span style="color: black;">EnumObjectItems</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>,<span style="color: #008000;">None</span>,<span style="color: #483d8b;">'process'</span>, win32pdh.<span style="color: black;">PERF_DETAIL_WIZARD</span><span style="color: black;">&#41;</span>
    proc_ids=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    proc_dict=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> instance <span style="color: #ff7700;font-weight:bold;">in</span> instances:
        <span style="color: #ff7700;font-weight:bold;">if</span> instance <span style="color: #ff7700;font-weight:bold;">in</span> proc_dict:
            proc_dict<span style="color: black;">&#91;</span>instance<span style="color: black;">&#93;</span> = proc_dict<span style="color: black;">&#91;</span>instance<span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            proc_dict<span style="color: black;">&#91;</span>instance<span style="color: black;">&#93;</span>=<span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> instance, max_instances <span style="color: #ff7700;font-weight:bold;">in</span> proc_dict.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> inum <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>max_instances<span style="color: #ff4500;">+1</span><span style="color: black;">&#41;</span>:
            hq = win32pdh.<span style="color: black;">OpenQuery</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># initializes the query handle </span>
            path = win32pdh.<span style="color: black;">MakeCounterPath</span><span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span><span style="color: #008000;">None</span>,<span style="color: #483d8b;">'process'</span>,instance, <span style="color: #008000;">None</span>, inum,<span style="color: #483d8b;">'ID Process'</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
            counter_handle=win32pdh.<span style="color: black;">AddCounter</span><span style="color: black;">&#40;</span>hq, path<span style="color: black;">&#41;</span>
            win32pdh.<span style="color: black;">CollectQueryData</span><span style="color: black;">&#40;</span>hq<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#collects data for the counter </span>
            <span style="color: #008000;">type</span>, val = win32pdh.<span style="color: black;">GetFormattedCounterValue</span><span style="color: black;">&#40;</span>counter_handle, win32pdh.<span style="color: black;">PDH_FMT_LONG</span><span style="color: black;">&#41;</span>
            proc_ids.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>instance,<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            win32pdh.<span style="color: black;">CloseQuery</span><span style="color: black;">&#40;</span>hq<span style="color: black;">&#41;</span>
&nbsp;
    proc_ids.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> proc_ids
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> procids<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Alas, while this also got me a list of processes from my Windows box (along with the PID), it didn&#8217;t give me any information on the CPU and memory utilization. I think this one could work if I used different counter names. I&#8217;m guessing if you wanted, you could figure out that information using MSDN. I didn&#8217;t want to mess with that, so I continued digging.</p>
<p>That recipe led me to the following one based on ctypes:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># http://code.activestate.com/recipes/305279/</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
Enumerates active processes as seen under windows Task Manager on Win NT/2k/XP using PSAPI.dll
(new api for processes) and using ctypes.Use it as you please.
&nbsp;
Based on information from http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q175030&amp;ID=KB;EN-US;Q175030
&nbsp;
By Eric Koome
email ekoome@yahoo.com
license GPL
&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
<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;
<span style="color: #808080; font-style: italic;">#PSAPI.DLL</span>
psapi = windll.<span style="color: black;">psapi</span>
<span style="color: #808080; font-style: italic;">#Kernel32.DLL</span>
kernel = windll.<span style="color: black;">kernel32</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> EnumProcesses<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    arr = c_ulong <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">256</span>
    lpidProcess= arr<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cb = sizeof<span style="color: black;">&#40;</span>lpidProcess<span style="color: black;">&#41;</span>
    cbNeeded = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    hModule = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    count = c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    modname = c_buffer<span style="color: black;">&#40;</span><span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
    PROCESS_QUERY_INFORMATION = 0x0400
    PROCESS_VM_READ = 0x0010
&nbsp;
    <span style="color: #808080; font-style: italic;">#Call Enumprocesses to get hold of process id's</span>
    psapi.<span style="color: black;">EnumProcesses</span><span style="color: black;">&#40;</span>byref<span style="color: black;">&#40;</span>lpidProcess<span style="color: black;">&#41;</span>,
                        cb,
                        byref<span style="color: black;">&#40;</span>cbNeeded<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#Number of processes returned</span>
    nReturned = cbNeeded.<span style="color: black;">value</span>/sizeof<span style="color: black;">&#40;</span>c_ulong<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    pidProcess = <span style="color: black;">&#91;</span>i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> lpidProcess<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>:nReturned<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> pid <span style="color: #ff7700;font-weight:bold;">in</span> pidProcess:
&nbsp;
        <span style="color: #808080; font-style: italic;">#Get handle to the process based on PID</span>
        hProcess = kernel.<span style="color: black;">OpenProcess</span><span style="color: black;">&#40;</span>PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
                                      <span style="color: #008000;">False</span>, pid<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> hProcess:
            psapi.<span style="color: black;">EnumProcessModules</span><span style="color: black;">&#40;</span>hProcess, byref<span style="color: black;">&#40;</span>hModule<span style="color: black;">&#41;</span>, sizeof<span style="color: black;">&#40;</span>hModule<span style="color: black;">&#41;</span>, byref<span style="color: black;">&#40;</span>count<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            psapi.<span style="color: black;">GetModuleBaseNameA</span><span style="color: black;">&#40;</span>hProcess, hModule.<span style="color: black;">value</span>, modname, sizeof<span style="color: black;">&#40;</span>modname<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;&quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span> i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> modname <span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">#-- Clean up</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>modname._length_<span style="color: black;">&#41;</span>:
                modname<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>=<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00'</span>
&nbsp;
            kernel.<span style="color: black;">CloseHandle</span><span style="color: black;">&#40;</span>hProcess<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    EnumProcesses<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>This is pretty clever looking, but I&#8217;m pretty bad at parsing ctypes. It&#8217;s something I want to learn, but I had a deadline, doggone it! Plus this one only showed a list of running processes but no information about them. Fortunately, the author included a reference, but I decided to keep looking.</p>
<p>Next I found a thread about using Tim Golden&#8217;s handy <a href="http://tgolden.sc.sabren.com/python/wmi/index.html">WMI module</a> to do this sort of thing (below is copied right from the thread):</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># http://mail.python.org/pipermail/python-win32/2003-December/001482.html</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> processes = WMI.<span style="color: black;">InstancesOf</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Win32_Process'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>processes<span style="color: black;">&#41;</span>
<span style="color: #ff4500;">41</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: black;">&#91;</span>process.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Name'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span> <span style="color: #ff7700;font-weight:bold;">for</span> process <span style="color: #ff7700;font-weight:bold;">in</span> processes<span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># get</span>
the process names
<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'System Idle Process'</span>, u<span style="color: #483d8b;">'System'</span>, u<span style="color: #483d8b;">'SMSS.EXE'</span>, u<span style="color: #483d8b;">'CSRSS.EXE'</span>,
u<span style="color: #483d8b;">'WINLOGON.EXE'</span>, u<span style="color: #483d8b;">'SERVICES.EXE'</span>, u<span style="color: #483d8b;">'LSASS.EXE'</span>, u<span style="color: #483d8b;">'SVCHOST.EXE'</span>,
u<span style="color: #483d8b;">'SVCHOST.EXE'</span>, u<span style="color: #483d8b;">'SVCHOST.EXE'</span>, u<span style="color: #483d8b;">'SVCHOST.EXE'</span>, u<span style="color: #483d8b;">'SPOOLSV.EXE'</span>,
u<span style="color: #483d8b;">'ati2evxx.exe'</span>, u<span style="color: #483d8b;">'BAsfIpM.exe'</span>, u<span style="color: #483d8b;">'defwatch.exe'</span>, u<span style="color: #483d8b;">'inetinfo.exe'</span>,
u<span style="color: #483d8b;">'mdm.exe'</span>, u<span style="color: #483d8b;">'rtvscan.exe'</span>, u<span style="color: #483d8b;">'SCARDSVR.EXE'</span>, u<span style="color: #483d8b;">'WLTRYSVC.EXE'</span>,
u<span style="color: #483d8b;">'BCMWLTRY.EXE'</span>, u<span style="color: #483d8b;">'EXPLORER.EXE'</span>, u<span style="color: #483d8b;">'Apoint.exe'</span>, u<span style="color: #483d8b;">'carpserv.exe'</span>,
u<span style="color: #483d8b;">'atiptaxx.exe'</span>, u<span style="color: #483d8b;">'quickset.exe'</span>, u<span style="color: #483d8b;">'DSentry.exe'</span>, u<span style="color: #483d8b;">'Directcd.exe'</span>,
u<span style="color: #483d8b;">'vptray.exe'</span>, u<span style="color: #483d8b;">'ApntEx.exe'</span>, u<span style="color: #483d8b;">'FaxCtrl.exe'</span>, u<span style="color: #483d8b;">'digstream.exe'</span>,
u<span style="color: #483d8b;">'CTFMON.EXE'</span>, u<span style="color: #483d8b;">'wuauclt.exe'</span>, u<span style="color: #483d8b;">'IEXPLORE.EXE'</span>, u<span style="color: #483d8b;">'Pythonwin.exe'</span>,
u<span style="color: #483d8b;">'MMC.EXE'</span>, u<span style="color: #483d8b;">'OUTLOOK.EXE'</span>, u<span style="color: #483d8b;">'LineMgr.exe'</span>, u<span style="color: #483d8b;">'SAPISVR.EXE'</span>,
u<span style="color: #483d8b;">'WMIPRVSE.EXE'</span><span style="color: black;">&#93;</span>
&nbsp;
Here <span style="color: #ff7700;font-weight:bold;">is</span> how to get a single process <span style="color: #ff7700;font-weight:bold;">and</span> get its PID.
&nbsp;
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p = WMI.<span style="color: black;">ExecQuery</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'select * from Win32_Process where
Name=&quot;Pythonwin.exe&quot;'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: black;">&#91;</span>prop.<span style="color: black;">Name</span> <span style="color: #ff7700;font-weight:bold;">for</span> prop <span style="color: #ff7700;font-weight:bold;">in</span> p<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">Properties_</span><span style="color: black;">&#93;</span> <span style="color: #808080; font-style: italic;"># let's look at all the</span>
process <span style="color: #008000;">property</span> names
<span style="color: black;">&#91;</span>u<span style="color: #483d8b;">'Caption'</span>, u<span style="color: #483d8b;">'CommandLine'</span>, u<span style="color: #483d8b;">'CreationClassName'</span>, u<span style="color: #483d8b;">'CreationDate'</span>,
u<span style="color: #483d8b;">'CSCreationClassName'</span>, u<span style="color: #483d8b;">'CSName'</span>, u<span style="color: #483d8b;">'Description'</span>, u<span style="color: #483d8b;">'ExecutablePath'</span>,
u<span style="color: #483d8b;">'ExecutionState'</span>, u<span style="color: #483d8b;">'Handle'</span>, u<span style="color: #483d8b;">'HandleCount'</span>, u<span style="color: #483d8b;">'InstallDate'</span>,
u<span style="color: #483d8b;">'KernelModeTime'</span>, u<span style="color: #483d8b;">'MaximumWorkingSetSize'</span>, u<span style="color: #483d8b;">'MinimumWorkingSetSize'</span>,
u<span style="color: #483d8b;">'Name'</span>, u<span style="color: #483d8b;">'OSCreationClassName'</span>, u<span style="color: #483d8b;">'OSName'</span>, u<span style="color: #483d8b;">'OtherOperationCount'</span>,
u<span style="color: #483d8b;">'OtherTransferCount'</span>, u<span style="color: #483d8b;">'PageFaults'</span>, u<span style="color: #483d8b;">'PageFileUsage'</span>,
u<span style="color: #483d8b;">'ParentProcessId'</span>, u<span style="color: #483d8b;">'PeakPageFileUsage'</span>, u<span style="color: #483d8b;">'PeakVirtualSize'</span>,
u<span style="color: #483d8b;">'PeakWorkingSetSize'</span>, u<span style="color: #483d8b;">'Priority'</span>, u<span style="color: #483d8b;">'PrivatePageCount'</span>, u<span style="color: #483d8b;">'ProcessId'</span>,
u<span style="color: #483d8b;">'QuotaNonPagedPoolUsage'</span>, u<span style="color: #483d8b;">'QuotaPagedPoolUsage'</span>,
u<span style="color: #483d8b;">'QuotaPeakNonPagedPoolUsage'</span>, u<span style="color: #483d8b;">'QuotaPeakPagedPoolUsage'</span>,
u<span style="color: #483d8b;">'ReadOperationCount'</span>, u<span style="color: #483d8b;">'ReadTransferCount'</span>, u<span style="color: #483d8b;">'SessionId'</span>, u<span style="color: #483d8b;">'Status'</span>,
u<span style="color: #483d8b;">'TerminationDate'</span>, u<span style="color: #483d8b;">'ThreadCount'</span>, u<span style="color: #483d8b;">'UserModeTime'</span>, u<span style="color: #483d8b;">'VirtualSize'</span>,
u<span style="color: #483d8b;">'WindowsVersion'</span>, u<span style="color: #483d8b;">'WorkingSetSize'</span>, u<span style="color: #483d8b;">'WriteOperationCount'</span>,
u<span style="color: #483d8b;">'WriteTransferCount'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">Properties_</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ProcessId'</span><span style="color: black;">&#41;</span>.<span style="color: black;">Value</span> <span style="color: #808080; font-style: italic;"># get our ProcessId</span>
<span style="color: #ff4500;">928</span></pre>
<p>This is some cool stuff and I use Golden&#8217;s modules in some of my other code. However, I was still uncertain as to which counters to use to get to my information. I thought most of this stuff would just be coded for me or something! Well, it turned out that there is a package out there that does exactly what I needed AND it works on all the three of the major platforms! Amazing!</p>
<h2>The Cross-Platform Solution!</h2>
<p>The package&#8217;s name is <a href="http://code.google.com/p/psutil/">psutil</a> and it was what I decided to use. Here&#8217;s what I ended up with:</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> psutil
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
logPath = r<span style="color: #483d8b;">'some<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\p</span>roclogs'</span>
<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>logPath<span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">mkdir</span><span style="color: black;">&#40;</span>logPath<span style="color: black;">&#41;</span>
&nbsp;
separator = <span style="color: #483d8b;">&quot;-&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">80</span>
format = <span style="color: #483d8b;">&quot;%7s %7s %12s %12s %30s, %s&quot;</span>
format2 = <span style="color: #483d8b;">&quot;%7.4f %7.2f %12s %12s %30s, %s&quot;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
    procs = psutil.<span style="color: black;">get_process_list</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    procs = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>procs, key=<span style="color: #ff7700;font-weight:bold;">lambda</span> proc: proc.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
    logPath = r<span style="color: #483d8b;">'some<span style="color: #000099; font-weight: bold;">\p</span>ath<span style="color: #000099; font-weight: bold;">\p</span>roclogs<span style="color: #000099; font-weight: bold;">\p</span>rocLog%i.log'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>logPath, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>separator + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</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: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
    f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>format <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%CPU&quot;</span>, <span style="color: #483d8b;">&quot;%MEM&quot;</span>, <span style="color: #483d8b;">&quot;VMS&quot;</span>, <span style="color: #483d8b;">&quot;RSS&quot;</span>, <span style="color: #483d8b;">&quot;NAME&quot;</span>, <span style="color: #483d8b;">&quot;PATH&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    f.<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><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> proc <span style="color: #ff7700;font-weight:bold;">in</span> procs:
        cpu_percent = proc.<span style="color: black;">get_cpu_percent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        mem_percent = proc.<span style="color: black;">get_memory_percent</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        rss, vms = proc.<span style="color: black;">get_memory_info</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        rss = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>rss<span style="color: black;">&#41;</span>
        vms = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>vms<span style="color: black;">&#41;</span>
        name = proc.<span style="color: black;">name</span>
        path = proc.<span style="color: black;">path</span>
        f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>format2 <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>cpu_percent, mem_percent, vms, rss, name, path<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        f.<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>
    f.<span style="color: black;">close</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;Finished log update!&quot;</span>
    <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">300</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;writing new log data!&quot;</span></pre>
<p>Yes, it&#8217;s an infinite loop and yes, that&#8217;s usually a very bad thing to do (except in GUI programming). However, for my purpose, I needed a way to check the user&#8217;s processes every 5 minutes or so to see what was causing the machine to act so weird. Thus, the script needs to run forever and log the results to uniquely named files. That&#8217;s all this script does, along with a little formatting magic. Feel free to use it or not as you see fit.</p>
<p>I hope you found this collection of material helpful. Hopefully it will save you all the digging I went through!</p>
<p><em>Note: While this last script appears to work just fine on Windows XP, on Windows 7 32 and 64-bit, you will get an &#8220;Access Denied&#8221; traceback, I suspect this is caused by Window 7&#8242;s increased security, but I will try to find a workaround.</p>
<p>UPDATE (10/09/2010) &#8211; The psutil folks don&#8217;t know why it doesn&#8217;t work, but one of their developers has confirmed the issue. You can follow along on their <a href="http://groups.google.com/group/psutil/browse_frm/thread/ec8bf72fa18f79a2">Google Groups list</a>.<br />
</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/10/03/how-to-find-and-list-all-running-processes-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Remote Drive Space on Windows</title>
		<link>http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 22:50:00 +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=1196</guid>
		<description><![CDATA[After about a year or so at my current job, as we were still working on upgrading the last few Windows 98 machines to Windows XP, we had a need to check which machines on our network were getting low on disk space. The issue was cropping up because we had Windows XP loaded on [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/" data-url="http://bit.ly/vX4W5I" data-text="Getting Remote Drive Space on Windows" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/";
			reddit_title = "Getting Remote Drive Space on Windows";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/"></g:plusone></div></div><p>After about a year or so at my current job, as we were still working on upgrading the last few Windows 98 machines to Windows XP, we had a need to check which machines on our network were getting low on disk space. The issue was cropping up because we had Windows XP loaded on several machines that had 10 GB hard drives and a few with 20 GB and one or two with just 4 GB. Anyway, after some digging online, I discovered that the PyWin32 package could accomplish what I needed.<span id="more-1196"></span></p>
<p>Here is the code that I used:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> win32com.<span style="color: black;">client</span> as com
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> TotalSize<span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Return the TotalSize of a shared drive [GB]&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        fso = com.<span style="color: black;">Dispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: black;">&#41;</span>
        drv = fso.<span style="color: black;">GetDrive</span><span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> drv.<span style="color: black;">TotalSize</span>/<span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">30</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> FreeSpace<span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; Return the FreeSpace of a shared drive [GB]&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        fso = com.<span style="color: black;">Dispatch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: black;">&#41;</span>
        drv = fso.<span style="color: black;">GetDrive</span><span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> drv.<span style="color: black;">FreeSpace</span>/<span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">30</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
&nbsp;
workstations = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'computeNameOne'</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Hard drive sizes:'</span>
<span style="color: #ff7700;font-weight:bold;">for</span> compName <span style="color: #ff7700;font-weight:bold;">in</span> workstations:
    drive = <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>'</span> + compName + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>c$'</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'*************************************************<span style="color: #000099; font-weight: bold;">\n</span>'</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> compName
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'TotalSize of %s = %f GB'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>drive, TotalSize<span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'FreeSpace on %s = %f GB'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>drive, FreeSpace<span style="color: black;">&#40;</span>drive<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'*************************************************<span style="color: #000099; font-weight: bold;">\n</span>'</span></pre>
<p>Note at the bottom that I used a list. Normally, I would list the computer names of every computer that I wanted to check. Then I would iterate over those names and put together the right path that I needed from a machine with full domain admin rights.</p>
<p>To get this to work, we needed to import win32com.client and call the following: <strong>com.Dispatch(&#8220;Scripting.FileSystemObject&#8221;)</strong>. This would get us a COM object that we could query to get a disk object. Once we had that, we can ask the disk how much total space it had and how much free space. Looking at this code today, I would combine the two functions into one and return a tuple. As you can see, I do a little bit of math on the result to get it to return the size in gigabytes. </p>
<p>That&#8217;s all there is to it. Simple stuff. I suspect that I didn&#8217;t write this code completely since the variable names suck. It is probably from an ActiveState recipe or a forum and I forgot to attribute it. If you recognize the code, let me know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/09/09/getting-remote-drive-space-on-windows/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/" data-url="http://bit.ly/tv7oEF" data-text="PyWin32: Getting Windows Event Logs" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/";
			reddit_title = "PyWin32: Getting Windows Event Logs";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/07/27/pywin32-getting-windows-event-logs/"></g:plusone></div></div><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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/" data-url="http://bit.ly/rv8aSK" data-text="Python: Running Ping, Traceroute and More" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/";
			reddit_title = "Python: Running Ping, Traceroute and More";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/"></g:plusone></div></div><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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/" data-url="http://bit.ly/rWyHLZ" data-text="Restarting PCs with Python" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/";
			reddit_title = "Restarting PCs with Python";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/03/27/restarting-pcs-with-python/"></g:plusone></div></div><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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/" data-url="http://bit.ly/vHisL5" data-text="Python&#8217;s _winreg: Editing the Windows Registry" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/03/20/pythons-_winreg-editing-the-windows-registry/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div></div><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. <strong>IMPORTANT: Be sure to back up your registry before attempting to edit it.</strong> 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>
<p><strong>UPDATE (10/14/2011):</strong> We now have a Brazilian Portuguese translation of this post up on Carlisson Galdino&#8217;s <a href="http://www.carlissongaldino.com.br/post/editando-o-registro-do-windows-em-python-com-o-winreg" target="_blank">blog </a></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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/" data-url="http://bit.ly/uzbNAy" data-text="Python: Finding the Commit Charge Values in Windows" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/";
			reddit_title = "Python: Finding the Commit Charge Values in Windows";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/03/05/python-finding-the-commit-charge-values-in-windows/"></g:plusone></div></div><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[Python]]></category>
		<category><![CDATA[PyWin32]]></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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/" data-url="http://bit.ly/vy8YNT" data-text="Creating Windows Shortcuts with Python (Part II)" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/";
			reddit_title = "Creating Windows Shortcuts with Python (Part II)";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/02/25/creating-windows-shortcuts-with-python-part-ii/"></g:plusone></div></div><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[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/" data-url="http://bit.ly/sgpNrN" data-text="Python, Windows and Printers" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/";
			reddit_title = "Python, Windows and Printers";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/02/14/python-windows-and-printers/"></g:plusone></div></div><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>
	</channel>
</rss>

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

