<?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>No Znx! &#187; Scripts</title>
	<atom:link href="http://znx.no/category/scripts/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Tue, 20 Dec 2011 22:42:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Arch / Yum</title>
		<link>http://znx.no/2011/12/arch-yum/</link>
		<comments>http://znx.no/2011/12/arch-yum/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 23:52:29 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[archlinux]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=364</guid>
		<description><![CDATA[I recently switched to Archlinux and I have to say I love it a great deal. I really like the simplicity of it, unfortunately due to my long standing usage or yum I have been spoilt. It means that I find myself wishing to have commands like yum install package, so switching pacman&#8217;s -S was [...]]]></description>
			<content:encoded><![CDATA[<p>I recently switched to Archlinux and I have to say I love it a great deal. I really like the simplicity of it, unfortunately due to my long standing usage or <code>yum</code> I have been spoilt. It means that I find myself wishing to have commands like <code>yum install package</code>, so switching pacman&#8217;s <code>-S</code> was a little too much. I therefore wrote a wrapper to make it easier for me. Maybe it can help others just starting out in the path to using Archlinux.</p>
<p>In preparation for this, you will need to install <code>yaourt</code> and enable it with sudo access. Either that or replace all the references to <code>yaourt</code> below with <code>pacman</code>.</p>
<p>Drop this into your <i>~/.bashrc</i> (or zshrc):</p>
<pre>function arch() {
        if [ -z "$1" ]; then
                echo "arch <command> <blah...>" >&#038;2
                return -1
        fi

        case $1 in
                upgrade)
                        # Synchronize, upgrade
                        shift
                        sudo yaourt -Syu $@
                        ;;
                install)
                        # Install stuff
                        shift
                        sudo yaourt -S $@
                        ;;
                localinstall)
                        # Install file
                        shift
                        sudo yaourt -U $@
                        ;;
                remove)
                        # Remove
                        shift
                        sudo yaourt -R $@
                        ;;
                fullremove)
                        # Remove file
                        shift
                        sudo yaourt -Rns $@
                        ;;
                info)
                        # Display information
                        shift
                        yaourt -Si $@
                        ;;
                search)
                        # Search
                        shift
                        yaourt -Ss $@
                        ;;
        esac
}</pre>
<p>Now you can do things like:</p>
<pre>$ arch install git
$ arch remove git
$ arch info git
$ arch search git</pre>
<p>Hope its handy!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2011/12/arch-yum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Renaming Files In A Directory</title>
		<link>http://znx.no/2009/09/renaming-files/</link>
		<comments>http://znx.no/2009/09/renaming-files/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:58:32 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ms]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=159</guid>
		<description><![CDATA[So I had a problem on Windows in that I have lots of directories that are named correctly but I wish the files under them to be uniformly named. The solution was to use PowerShell, which is an extremely effective scripting tool for Windows, especially if you come from a UNIX shell background. There is [...]]]></description>
			<content:encoded><![CDATA[<p>So I had a problem on Windows in that I have lots of directories that are named correctly but I wish the files under them to be uniformly named.</p>
<p>The solution was to use <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">PowerShell</a>, which is an extremely effective scripting tool for Windows, especially if you come from a UNIX shell background.</p>
<p>There is actually quite a lot of documentation available for it and rather than go into detail I will simply show you the script:</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;">#</span>
<span style="color: #008000;"># RenameDirectoryFiles.ps1</span>
<span style="color: #008000;">#</span>
&nbsp;
<span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$paths</span> <span style="color: #000000;">&#41;</span>
<span style="color: #008080; font-weight: bold;">Set-PSDebug</span> <span style="color: #008080; font-style: italic;">-Strict</span>
&nbsp;
<span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#40;</span> <span style="color: #800080;">$path</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$paths</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: pink;">!</span><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Test-Path</span> <span style="color: #800080;">$path</span> <span style="color: #008080; font-style: italic;">-PathType</span> Container<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-weight: bold;">Write-Error</span> <span style="color: #800000;">&quot;'$path' doesn't exist or isn't a directory&quot;</span>
        exit <span style="color: #804000;">1</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>
    <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800080;">$path</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">sort</span> FullName <span style="color: pink;">|</span> <span style="color: #0000FF;">foreach</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #800080;">$tmp</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;{0:000}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$i</span>
        <span style="color: #800080;">$ext</span> <span style="color: pink;">=</span> <span style="color: #000080;">$_</span>.Extension
        <span style="color: #800080;">$newname</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$path - $tmp$ext&quot;</span>
        <span style="color: #008080; font-weight: bold;">Rename-Item</span> <span style="color: #000080;">$_</span>.fullname <span style="color: #800080;">$newname</span>
        <span style="color: #800080;">$newname</span>
        <span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #800080;">$i</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So the usage is extremely simple:</p>
<pre>RenameDirectoryFiles.ps1 'My Directory'</pre>
<p>After which all the files under &#8216;My Directory&#8217; will be renamed to &#8216;My Directory &#8211; 000.fileextension&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/09/renaming-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Skinning A Cat</title>
		<link>http://znx.no/2009/05/skinning-a-cat/</link>
		<comments>http://znx.no/2009/05/skinning-a-cat/#comments</comments>
		<pubDate>Sun, 17 May 2009 02:02:36 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://znx.no/?p=116</guid>
		<description><![CDATA[I was recently discussing with someone about sed usage. They were having difficultly creating an appropriate regex to handle their problem: (Orlando) sed regex kicks my ass. I like to remove the second : in the line. So that in 123:456:6789 it will only returns 123:456. I can find the first :, but I have [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently discussing with someone about <code>sed</code> usage. They were having difficultly creating an appropriate regex to handle their problem:</p>
<pre>(Orlando) sed regex kicks my ass.  I like to remove the second : in the
line. So that in 123:456:6789 it will only returns 123:456.  I can find the
first :, but I have not been able to use s/:{2}// to find the second
one, and remove the rest.</pre>
<p>I enjoy regex (I know I&#8217;m weird, leave me alone), so I was able to provide an answer to this problem:</p>
<pre>(@znx) Orlando: the trick is to use [^:] and \1 ..
(@znx) like:   s/\(:[^:]*\):.*/\1/</pre>
<p>Now obviously at first glance this regex could be a bit intimating to someone who is still picking up the skills but as with most things if we break it down it becomes easier.</p>
<p>Working out to in, <code>\(    \):.*</code>, that says match something with <strong>:</strong> at the end and all the character after it. The <strong>.</strong> is a special meaning &#8220;any character&#8221; and <strong>*</strong> to match multiple characters. The first match will be stored by <code>sed</code> and assigned into the <strong>\1</strong> for the replacement (that is what the brackets do). Inside the brackets we have <code>:[^:]*</code>. The sequence <strong>[^ ]</strong> is a negated list, that means that we are asking it to match everything that is NOT inside the list, in this case <strong>:</strong>.</p>
<p>Putting it altogether we are saying: Match a leading : and a trailing : with any characters after it. Placing the contents between the two <strong>:</strong> in memory. Then finally we replace the contents.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s/\(:[^:]*\):.*/\1/'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>Sucess, however as with most things, there is more than one way to skin a cat and regex is rarely the prettiest method. So what other ways can we solve this problem?</p>
<p>With AWK:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F: <span style="color: #ff0000;">'{print $1&quot;:&quot;$2}'</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>With <code>cut</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">123</span>:<span style="color: #000000;">456</span>:<span style="color: #000000;">6789</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d: -f1,<span style="color: #000000;">2</span>
<span style="color: #000000;">123</span>:<span style="color: #000000;">456</span></pre></div></div>

<p>As always, experimentation with the mass of GNU tools you can find on your system will bring a greater deal of power to your tool chest. Mind you, then I wouldn&#8217;t get complements for helping would I?</p>
<pre>(Orlando) Wow, When I grow up, I like to remember this thing like you do. <img src='http://znx.no/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </pre>
<p>Haha, till next time!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/05/skinning-a-cat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BigBrother Scripting</title>
		<link>http://znx.no/2009/03/bigbrother-scripting/</link>
		<comments>http://znx.no/2009/03/bigbrother-scripting/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 22:40:13 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[ms]]></category>
		<category><![CDATA[vbs]]></category>

		<guid isPermaLink="false">http://znx.no/?p=108</guid>
		<description><![CDATA[I was recently asked if it was possible to monitor the Event Log for a single event and ensure that it was occurring regularly. It is rare that I handle Windows scripting and when I do I normally find myself cursing it, haha! In this case we want to ensure that a print server is [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked if it was possible to monitor the Event Log for a single event and ensure that it was occurring regularly. It is rare that I handle Windows scripting and when I do I normally find myself cursing it, haha! In this case we want to ensure that a print server is constantly printing through the day, we expect that at least 1 print job will occur every 15 minutes, if not then we&#8217;d like a warning. Obviously this check should only run during work hours.</p>
<p>So the first step is relatively simple, access the Event Log and look for a single event by its event code.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> objWMIService = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\cimv2&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and EventCode = 10&quot;</span>)</pre></div></div>

<p>What we did here was grab all the events with event code of 10 (a print job!). So if we count the number of events within a range then we will have basically completed a huge part of the work.</p>
<p>So next step is to make an interval that will be 15 minutes back from whatever time is current.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> dtmStartDate = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WbemScripting.SWbemDateTime&quot;</span>)
dtmStartDate.SetVarDate DateAdd(<span style="color: #800000;">&quot;n&quot;</span>,-15,Now()),<span style="color: #00C2FF; font-weight: bold;">True</span></pre></div></div>

<p>And applying that into our statement:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and EventCode = 10&quot;</span> _
   &amp;amp; <span style="color: #800000;">&quot; and TimeWritten &amp;gt;= '&quot;</span> &amp;amp; dtmStartDate &amp;amp; <span style="color: #800000;">&quot;'&quot;</span>)</pre></div></div>

<p>This means that we are now collecting the events that only occurred within the last fifteen minutes. So what next, well we need to have a statement to pass to BigBrother to indicate success or failure. Fortunately enough I have another script which monitors the cluster (thanks to the awesome DeadCat repository) and it has some code to help place the file that BigBrother collects.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">const HKLM = &amp;amp;H80000002
strBBExtPathNew = <span style="color: #800000;">&quot;SOFTWARE\Quest Software\BigBrother\bbnt\ExternalPath&quot;</span>
strBBExtPathOld = <span style="color: #800000;">&quot;SOFTWARE\BigBrother\bbnt\ExternalPath&quot;</span>
<span style="color: #151B8D; font-weight: bold;">set</span> oReg = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\default:StdRegProv&quot;</span>)
&nbsp;
oReg.GetStringValue HKLM,strBBExtPathNew,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  oReg.GetStringValue HKLM,strBBExtPathOld,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>

<p><span id="more-108"></span>So now we have the path to put our file in, all we need to do now is write the file and add two simple checks to see that the time is within work ours (9-5).</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span>
&nbsp;
strState = <span style="color: #800000;">&quot;green Printing was active within the last 15 minutes&quot;</span>
strBBFileName = <span style="color: #800000;">&quot;print&quot;</span>
intEventCode = 10
&nbsp;
<span style="color: #008000;">' Find out where to put the BB log files
</span>const HKLM = &amp;amp;H80000002
strBBExtPathNew = <span style="color: #800000;">&quot;SOFTWARE\Quest Software\BigBrother\bbnt\ExternalPath&quot;</span>
strBBExtPathOld = <span style="color: #800000;">&quot;SOFTWARE\BigBrother\bbnt\ExternalPath&quot;</span>
<span style="color: #151B8D; font-weight: bold;">set</span> oReg = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\default:StdRegProv&quot;</span>)
&nbsp;
<span style="color: #008000;">' Check for the BB External Path, new then old otherwise quit
</span>oReg.GetStringValue HKLM,strBBExtPathNew,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  oReg.GetStringValue HKLM,strBBExtPathOld,,strExtPath
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> isNull(strExtPath) <span style="color: #8D38C9; font-weight: bold;">then</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Get Computer name
</span><span style="color: #151B8D; font-weight: bold;">set</span> WshNetwork = WScript.<span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WScript.Network&quot;</span>)
strComputerName = WshNetwork
&nbsp;
<span style="color: #008000;">' Prepare file
</span><span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;Scripting.FileSystemObject&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">set</span> f = fso.OpenTextFile(strExtPath &amp;amp; <span style="color: #800000;">&quot;\&quot;</span> &amp;amp; strBBFileName, 8, <span style="color: #00C2FF; font-weight: bold;">True</span>)
&nbsp;
<span style="color: #008000;">' Run between 9 - 5
</span><span style="color: #8D38C9; font-weight: bold;">if</span> Hour(Now()) &amp;lt; 9 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;green Not checking as we are outwith work hours&quot;</span>
  f.Write strState
  <span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  <span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> Hour(Now()) &amp;gt; 17 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;green Not checking as we are outwith work hours&quot;</span>
  f.Write strState
  <span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  <span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
  WScript.Quit
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Get a date 15 minutes in the past.
</span><span style="color: #151B8D; font-weight: bold;">set</span> dtmStartDate = <span style="color: #E56717; font-weight: bold;">CreateObject</span>(<span style="color: #800000;">&quot;WbemScripting.SWbemDateTime&quot;</span>)
dtmStartDate.SetVarDate DateAdd(<span style="color: #800000;">&quot;n&quot;</span>,-15,Now()),<span style="color: #00C2FF; font-weight: bold;">True</span>
&nbsp;
<span style="color: #008000;">' Collect the events from eventlog within the last 15 minutes
</span><span style="color: #151B8D; font-weight: bold;">set</span> objWMIService = <span style="color: #E56717; font-weight: bold;">GetObject</span>(<span style="color: #800000;">&quot;winmgmts:\root\cimv2&quot;</span>)
<span style="color: #151B8D; font-weight: bold;">Set</span> colEvents = objWMIService.ExecQuery _
   (<span style="color: #800000;">&quot;Select * from Win32_NTLogEvent Where Logfile = 'System' and &quot;</span> _
   &amp;amp; <span style="color: #800000;">&quot;EventCode = '&quot;</span> &amp;amp; intEventCode _
   &amp;amp; <span style="color: #800000;">&quot;' and TimeWritten &amp;gt;= '&quot;</span> &amp;amp; dtmStartDate &amp;amp; <span style="color: #800000;">&quot;'&quot;</span>)
&nbsp;
<span style="color: #008000;">' If no events then alert.
</span><span style="color: #8D38C9; font-weight: bold;">if</span> colEvents.Count = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;red No print job within the last 15 minutes&quot;</span>
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span>
&nbsp;
<span style="color: #008000;">' Output
</span>f.Write strState
&nbsp;
<span style="color: #151B8D; font-weight: bold;">set</span> fso = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
<span style="color: #151B8D; font-weight: bold;">set</span> f = <span style="color: #00C2FF; font-weight: bold;">Nothing</span>
WScript.Quit</pre></div></div>

<p>Obviously you can adjust this script to monitor for any regular event and it works well on Windows 2003 and XP machines. It is at this point I thought that I was finished. Testing this script on a Windows XP machine, no issues it was doing exactly what it was meant to. However the server I was intending on running it under was a Windows 2000 server. So I was forced to make some more changes to the script to allow it to run. The changes I made unfortunately make the script slightly harder to adjust to make it work for any event.</p>
<p>The first change was to the statement to collect the print job.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #151B8D; font-weight: bold;">Set</span> colEvents = objWMIService.ExecQuery _
  (<span style="color: #800000;">&quot;Select * From Win32_NTLogEvent Where Type = 'information' &quot;</span> _
  &amp; <span style="color: #800000;">&quot;and EventCode = &quot;</span> &amp; intEventCode _
  &amp; <span style="color: #800000;">&quot;TimeWritten &gt;= '&quot;</span> &amp; dtmStartDate &amp; <span style="color: #800000;">&quot;'&quot;</span>)</pre></div></div>

<p>The next change I had to make was very strange, the Count method didn&#8217;t seem to exist (strange!?) so I was forced to loop to count:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #008000;">' UGLY
</span><span style="color: #151B8D; font-weight: bold;">set</span> intCount = 0
<span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> objEvent <span style="color: #8D38C9; font-weight: bold;">in</span> colEvents
  intCount = intCount + 1
<span style="color: #8D38C9; font-weight: bold;">Next</span>
<span style="color: #8D38C9; font-weight: bold;">if</span> intCount = 0 <span style="color: #8D38C9; font-weight: bold;">then</span>
  strState = <span style="color: #800000;">&quot;red No print job within the last 15 minutes&quot;</span>
<span style="color: #8D38C9; font-weight: bold;">end</span> <span style="color: #8D38C9; font-weight: bold;">if</span></pre></div></div>

<p>Those two changes allowed me to successfully monitor a single event. Same script in Linux would have taken me seconds! Anyway at least its a working model, let&#8217;s hope not a lot more requests come through for Windows issues, especially W2K ones!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/03/bigbrother-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Oddity</title>
		<link>http://znx.no/2009/02/file-oddity/</link>
		<comments>http://znx.no/2009/02/file-oddity/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 18:10:38 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=102</guid>
		<description><![CDATA[Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this: &#60;html&#62;&#60;head&#62;&#60;title&#62;Status&#60;/title&#62;&#60;/head&#62; &#60;table&#62; &#60;tr&#62;&#60;td&#62;Failed&#60;/td&#62;&#60;td&#62;Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;tr&#62;&#60;td&#62;Success&#60;/td&#62;&#60;td&#62;Another Backup Group&#60;/td&#62;&#60;/tr&#62; &#60;/table&#62; &#60;/body&#62; &#60;/html&#62; Nothing odd there, the file is normal but when I tried this command: $ grep -i failed status.html [...]]]></description>
			<content:encoded><![CDATA[<p>Today at work I was attempting to parse a file and discovered something odd happening. When I simply viewed the file with cat, I could see this:</p>
<pre>&lt;html&gt;&lt;head&gt;&lt;title&gt;Status&lt;/title&gt;&lt;/head&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Success&lt;/td&gt;&lt;td&gt;Another Backup Group&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>Nothing odd there, the file is normal but when I tried this command:</p>
<pre>$ grep -i failed status.html
$</pre>
<p>Huh? No output, suggesting that there is no lines with the words failed on them. The same occurs with awk and sed, indeed I could not find any tool to be able to grep out the status. So the next step was to check what was odd about the file:</p>
<pre>$ file status.html
status.html: HTML document text</pre>
<p>Still, nothing unusual. So now I am in full head scratching mode, I open up the file with vim to see if I can discover anything strange about the file but nothing. At this point I happened to switch to more rather than cat and the result was the start of how I solved it.</p>
<pre>$ more status.html
��&lt;

$</pre>
<p>Ah, so now I can see why grep and the others cannot view anything in the file. So this time I switch to vim again and check what file encoding we have:</p>
<pre>:set fileencoding
fileencoding=ucs-2le</pre>
<p>For those that are unaware, this is UCS-2 (little endian), also know as <a href="http://en.wikipedia.org/wiki/UTF-16">UTF-16</a>. So the issue was simply that we had UTF-16 characters, now for the trick to get around it:</p>
<pre>$ iconv -f UTF-16 -t UTF-8 status.html | grep -i failed
&lt;tr&gt;&lt;td&gt;Failed&lt;/td&gt;&lt;td&gt;Backup Group&lt;/td&gt;&lt;/tr&gt;
$</pre>
<p>Tada. Once more a solution!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/02/file-oddity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun With Bmotion</title>
		<link>http://znx.no/2009/02/fun-with-bmotion/</link>
		<comments>http://znx.no/2009/02/fun-with-bmotion/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 20:45:21 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[IRC]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[bmotion]]></category>
		<category><![CDATA[funny]]></category>

		<guid isPermaLink="false">http://znx.no/?p=97</guid>
		<description><![CDATA[I help out now and then with little bits of code for BMotion, the main coder is JamesOff. It is a wonderful set of scripts for AI on eggdrops. Sometimes bmotion can just spark. Check this conversation I had, both NoTopic and MonicaOff are running bmotion. (@ NoTopic) Uh oh, it's mark. (+ znx) listen [...]]]></description>
			<content:encoded><![CDATA[<p>I help out now and then with little bits of code for <a href="http://www.bmotion.net/">BMotion</a>, the main coder is <a href="http://jamesoff.net/">JamesOff</a>. It is a wonderful set of scripts for AI on eggdrops. Sometimes bmotion can just spark. Check this conversation I had, both NoTopic and MonicaOff are running bmotion.</p>
<pre> (@  NoTopic) Uh oh, it's mark.
 (+      znx) listen nt .. im tired of your shit
 (         *) znx kicks the crap out of nt
 (         *) NoTopic passes it on to james
 (+      znx) um
 (+      znx) :[
 (@MonicaOff) cheer up mak
 (@MonicaOff) *hugs*
 (+      znx) aww
 (+      znx) i love you mon
 (         *) znx hugs
 (@MonicaOff) Hehe, want to go out on a date someplace? <img src='http://znx.no/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />
 (+      znx) sure!
 (+      znx) where would you like to go mon?
 (@MonicaOff) Amsterdam.
 (+      znx) ooo
 (         *) znx books the flights
 (+ JamesOff) sounds nice <img src='http://znx.no/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />
 (@  NoTopic) oh categorically abyss what do you think, talking ass?
 (+      znx) your just annoyed im not taking you nt
 (         *) znx shows nt the flight tickets
 (@  NoTopic) ooooh, impressive!
 (+      znx) <img src='http://znx.no/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> !!
 (         *) NoTopic steals the flight tickets and runs off
 (@  NoTopic) ALL MINE NOW^@~#%~£~$$~*£$$$
 (+      znx) W T F
 (         *) NoTopic sells the flight tickets on ebay
 (+      znx) NOOOOOOOOOOooooooooooooooooooo
 (         *) znx sulks at nt
 (@  NoTopic) what</pre>
<p>Of course sometimes bmotion is just plain evil!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/02/fun-with-bmotion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backing Up MySQL Databases</title>
		<link>http://znx.no/2009/01/backing-up-mysql-databases/</link>
		<comments>http://znx.no/2009/01/backing-up-mysql-databases/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 22:47:49 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://znx.no/?p=73</guid>
		<description><![CDATA[This is a really simple script that can be used to backup all the databases in a MySQL server. Thought I&#8217;d put it here as it shows some basic scripting techniques that maybe will help people see how to write scripts. Basically all this does is save me from having to type out the date. [...]]]></description>
			<content:encoded><![CDATA[<p>This is a really simple script that can be used to backup all the databases in a MySQL server. Thought I&#8217;d put it here as it shows some basic scripting techniques that maybe will help people see how to write scripts. Basically all this does is save me from having to type out the date. It could be automated but that would require storing the user name and password somewhere which normally isn&#8217;t too smart an idea.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #666666; font-style: italic;"># GNU GPL version 3.0 or above</span>
<span style="color: #666666; font-style: italic;"># Copyright (c) 2009 Mark Sangster</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;$3&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'mybackup &lt;user&gt; &lt;password&gt; &lt;host&gt;'</span> <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #007800;">MYUSER</span>=<span style="color: #007800;">$1</span>; <span style="color: #007800;">MYPASS</span>=<span style="color: #007800;">$2</span>; <span style="color: #007800;">MYHOST</span>=<span style="color: #007800;">$3</span>
&nbsp;
mysqldump <span style="color: #660033;">-u</span> <span style="color: #007800;">$MYUSER</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$MYPASS</span> <span style="color: #660033;">-h</span> <span style="color: #007800;">$MYHOST</span> <span style="color: #660033;">-A</span> <span style="color: #000000; font-weight: bold;">|</span> \
  <span style="color: #c20cb9; font-weight: bold;">bzip2</span> <span style="color: #660033;">-9</span> <span style="color: #000000; font-weight: bold;">&gt;</span> mysql-<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>Y-<span style="color: #000000; font-weight: bold;">%</span>m-<span style="color: #000000; font-weight: bold;">%</span>d<span style="color: #000000; font-weight: bold;">`</span>.bz2</pre></div></div>

<p>The script usage is very striaght-forward:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># mybackup user pass host</span></pre></div></div>

<p>At which point you will find a file called &#8220;mysql-2009-01-27.bz2&#8243; or similar.  Nothing to difficult!</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/01/backing-up-mysql-databases/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Better E-Mail Checking!</title>
		<link>http://znx.no/2008/12/better-e-mail-checking/</link>
		<comments>http://znx.no/2008/12/better-e-mail-checking/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 23:12:18 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[ms]]></category>

		<guid isPermaLink="false">http://znx.no/?p=60</guid>
		<description><![CDATA[A mate of mine was reading this and made some great comments about it! First up there is a Bash version of expect (THANK GOD!). Secondly fetchmail maybe a better solution to grabbing the mail to see if it was received, another good comment! Unfortunately for me I was working on a system with the [...]]]></description>
			<content:encoded><![CDATA[<p>A <a title="Dustybin" href="http://www.thinkdebian.org/">mate of mine</a> was reading this and made some great comments about it! First up there is a Bash version of <a title="BASH Version of Expect" href="http://empty.sourceforge.net">expect</a> (THANK GOD!). Secondly fetchmail maybe a better solution to grabbing the mail to see if it was received, another good comment! Unfortunately for me I was working on a system with the tools that were available to me, that means no fetchmail and no empty installed.</p>
<p>Interestingly it didn&#8217;t initially work 100% of the time, after a little bit of testing it looked like Microsoft were temporarily throwing away some of the test e-mails. Interesting as it must mean that they look for repetitious e-mails in a bid to fight SPAM. We of course simply contacted MS and got it sorted and now the e-mails come in every hour.</p>
<p>What this does show us though, is that under UNIX/Linux there is a lot of different solutions that can be made, but sometimes you have to work with the tools that are there.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2008/12/better-e-mail-checking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>E-Mail Checking</title>
		<link>http://znx.no/2008/12/e-mail-checking/</link>
		<comments>http://znx.no/2008/12/e-mail-checking/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 22:11:31 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[Scripts]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[ms]]></category>

		<guid isPermaLink="false">http://znx.no/?p=55</guid>
		<description><![CDATA[At my work we have two domains and recently one of those domains was blacklisted, thanks to some phishing e-mails hitting their marks. So I was tasked today to setup an automated e-mail send and check. To ensure that we can send between our domains. First up I checked to see that I was able [...]]]></description>
			<content:encoded><![CDATA[<p>At my work we have two domains and recently one of those domains was blacklisted, thanks to some phishing e-mails hitting their marks. So I was tasked today to setup an automated e-mail send and check. To ensure that we can send between our domains.</p>
<p>First up I checked to see that I was able to get a POP3 connection to the other domain, as it is hosted by Microsoft for us. Whilst I was able to connect via POP3 I was unable to login. A quick check revealed that only POP3S was being accepted. This meant that I was going to need to utilize the simple s_client tool available with OpenSSL.</p>
<p>So to testing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ openssl s_client <span style="color: #660033;">-connect</span> SERVER:<span style="color: #000000;">995</span>
.... blah blah ....
+OK The POP3 Service is ready.
USER test.user<span style="color: #000000; font-weight: bold;">@</span>example.com
^C
$</pre></div></div>

<p>Hrmm, what&#8217;s going on here, the server isn&#8217;t responding to my USER. A quick brain check and I spotted my issue, line endings!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ openssl s_client <span style="color: #660033;">-connect</span> SERVER:<span style="color: #000000;">995</span> <span style="color: #660033;">-crlf</span>
.... blah blah ....
+OK The POP3 Service is ready.
USER test.user<span style="color: #000000; font-weight: bold;">@</span>example.com
+OK
PASS mypassword
+OK User successfully logged in.
STAT
+OK <span style="color: #000000;">49</span> <span style="color: #000000;">2758446</span>
QUIT
DONE</pre></div></div>

<p>That&#8217;s more like it. Now I know that I can connect, logged and run arbitrary commands. It may be perfectly OK to manually check like this but that isn&#8217;t the reason we started this, we want an automated solution.</p>
<p>The most obvious solution here is <strong>expect</strong>, ugh TCL! With the above information it is easy to write a simple expect script:</p>

<div class="wp_syntax"><div class="code"><pre class="tcl" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env expect</span>
spawn openssl s_client -connect SERVER:<span style="color: #ff4500;">995</span> -crlf
expect <span style="color: #483d8b;">&quot;+OK The POP3 service is ready.&quot;</span>
send <span style="color: #483d8b;">&quot;USER test.user@example.com<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
send <span style="color: #483d8b;">&quot;PASS mypassword<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK User successfully logged on.&quot;</span>
send <span style="color: #483d8b;">&quot;QUIT<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;DONE&quot;</span>
expect <span style="color: #008000;">eof</span></pre></div></div>

<p>Simple enough, send a command and expect a response. Obviously it helps if you understand the protocol that you are dealing with. Running that script happily logs in and then quits. Sweet, we are halfway there already!</p>
<p>So to recap, I plan on sending an e-mail from the first domain to my test account on the second domain and then check that the e-mail actually arrived. Thus proving that the e-mail was getting through without issue. So when I do this I will wish to get the newest e-mail in my INBOX, which may not the be the first. So the script needs some modifying.</p>

<div class="wp_syntax"><div class="code"><pre class="tcl" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env expect</span>
spawn openssl s_client -connect SERVER:<span style="color: #ff4500;">995</span> -crlf
expect <span style="color: #483d8b;">&quot;+OK The POP3 service is ready.&quot;</span>
send <span style="color: #483d8b;">&quot;USER test.user@example.com<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
send <span style="color: #483d8b;">&quot;PASS mypassword<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK User successfully logged on.&quot;</span>
send <span style="color: #483d8b;">&quot;STAT<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect -re <span style="color: #483d8b;">&quot;OK (.*) .*&quot;</span> <span style="color: black;">&#123;</span>
  <span style="color: #ff7700;font-weight:bold;">set</span> number <span style="color: #ff3333;">$expect_out</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #008000;">string</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#125;</span>
send <span style="color: #483d8b;">&quot;TOP $number 1<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
send <span style="color: #483d8b;">&quot;QUIT<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;DONE&quot;</span>
expect <span style="color: #008000;">eof</span></pre></div></div>

<p>Now expect will grab the number of e-mails in the INBOX and request the headers of the newest one. So the final step is integrating this with a simple check. Lets just use a shell script to check with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">TS</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #000000; font-weight: bold;">%</span>s<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Subject: TESTING <span style="color: #007800;">$TS</span>
&nbsp;
Hi,
&nbsp;
This is a test please do not delete me!
&nbsp;
Thanks&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> mail test.user<span style="color: #000000; font-weight: bold;">@</span>example.com
<span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">60</span>
expect collect.exp <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #007800;">$TS</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> \
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Success The E-Mail Arrived!&quot;</span> <span style="color: #000000; font-weight: bold;">||</span>\
 <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Failure The E-Mail Was Not Found!&quot;</span></pre></div></div>

<p>That works, a test and we have a success! We find the TS in the subject line of the e-mail. There is a minor problem here, what happens if I e-mail the test and in-between I receive some other e-mails, hrmm! OK so why don&#8217;t we dump the headers of the first few e-mails, we certainly don&#8217;t expect a rush of e-mails on a test account within a minute. Let&#8217;s change the expect script just a bit more:</p>

<div class="wp_syntax"><div class="code"><pre class="tcl" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env expect</span>
&nbsp;
spawn openssl s_client -connect SERVER:<span style="color: #ff4500;">995</span> -crlf
expect <span style="color: #483d8b;">&quot;+OK The POP3 service is ready.&quot;</span>
send <span style="color: #483d8b;">&quot;USER test.user@example.com<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
send <span style="color: #483d8b;">&quot;PASS mypassword<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK User successfully logged on.&quot;</span>
send <span style="color: #483d8b;">&quot;STAT<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect -re <span style="color: #483d8b;">&quot;OK (.*) .*&quot;</span> <span style="color: black;">&#123;</span>
  <span style="color: #ff7700;font-weight:bold;">set</span> number <span style="color: #ff3333;">$expect_out</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #008000;">string</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#125;</span>
send <span style="color: #483d8b;">&quot;TOP $number 1<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
<span style="color: #ff7700;font-weight:bold;">set</span> number <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$number</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
send <span style="color: #483d8b;">&quot;TOP $number 1<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
<span style="color: #ff7700;font-weight:bold;">set</span> number <span style="color: black;">&#91;</span><span style="color: #ff7700;font-weight:bold;">expr</span> <span style="color: #ff3333;">$number</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
send <span style="color: #483d8b;">&quot;TOP $number 1<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;+OK&quot;</span>
send <span style="color: #483d8b;">&quot;QUIT<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>
expect <span style="color: #483d8b;">&quot;DONE&quot;</span>
expect <span style="color: #008000;">eof</span></pre></div></div>

<p>Placing the shell script into a crontab and we are done. Yet another example of how simple scripting can achieve anything.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2008/12/e-mail-checking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

