<?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>Andrew McFague</title>
	<atom:link href="http://www.andrew-mcfague.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andrew-mcfague.com</link>
	<description>Programming in Brooklyn</description>
	<lastBuildDate>Wed, 23 Mar 2011 04:49:53 +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>Repoze.who: Part 1 &#8211; Using authentication in your site</title>
		<link>http://www.andrew-mcfague.com/python/repoze-who-part-1-using-authentication-in-your-site/</link>
		<comments>http://www.andrew-mcfague.com/python/repoze-who-part-1-using-authentication-in-your-site/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 03:57:35 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[pylons]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[repoze.who]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[basicauth]]></category>
		<category><![CDATA[htpasswd]]></category>

		<guid isPermaLink="false">http://www.andrew-mcfague.com/?p=86</guid>
		<description><![CDATA[repoze.who is great for using authentication within your site, but the documentation is woefully incomplete as to how to actually implement the plugin. And although the Pylons book and wiki contain sparse examples of how to set up your project, they&#8217;re still lacking in several key features. Since I&#8217;ve spent quite some time working on <a href='http://www.andrew-mcfague.com/python/repoze-who-part-1-using-authentication-in-your-site/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p><cite>repoze.who</cite> is great for using authentication within your site, but the documentation is woefully incomplete as to how to actually implement the plugin. And although the Pylons book and wiki contain sparse examples of how to set up your project, they&#8217;re still lacking in several key features. Since I&#8217;ve spent quite some time working on company-related projects that use repoze.who, I want to share my knowledge.</p>
<p><span id="more-86"></span></p>
<p>There are four &#8220;roles&#8221; that plug-ins can fulfill, which will be configured in our <cite>who.ini</cite>:</p>
<ul class="simple">
<li><strong>IIdentifier</strong>: After a user submits their credentials, this plug-in is responsible for stripping the information and retrieving the info&#8211;such as a username/password from a log in page, or retrieving a single sign-on (SSO) key. This is then used by the&#8230;</li>
<li><strong>IAuthenticator</strong>: After a user&#8217;s information has been retrieved, the authenticator will determine whether or not the presented information is correct. For example, if it was a SSO key, this plug-in would be used to verify that it is still valid. If it were a username and password, this would verify that the information is correct.</li>
<li><strong>IChallenger</strong>: When a user is either not logged in&#8211;or has failed to login&#8211;the challenger will present the user with a page allowing them ao authenticate.</li>
<li><strong>IMetadataProvider</strong>: When logged in, this plug-in retrieves metadata based on the IIdentifier. (optional)</li>
</ul>
<p>For this first part, I&#8217;ll focus primarily on getting two of the built-in plug-ins configured to fulfill these.  First off, lets create a blank project to get things started.</p>
<div id="creating-a-pylons-plugin" class="section">
<span id="Creating_a_Pylons_plugin"><h1>Creating a Pylons plugin</h1></span>
<p>If you&#8217;ve never touched Pylons, make sure you follow the guide on the <a class="reference external" href="http://pylonshq.com/docs/en/1.0/gettingstarted/">official Pylons Getting Started page</a>.</p>
<p>Lets create a basic server and controller using <cite>paster</cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster create <span style="color: #660033;">-t</span> pylons authapp <span style="color: #007800;">template_engine</span>=mako <span style="color: #007800;">sqlalchemy</span>=False
Selected and implied templates:
Pylons<span style="color: #666666; font-style: italic;">#pylons  Pylons application template</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>...<span style="color: #7a0874; font-weight: bold;">&#93;</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> authapp
$ paster controller secret
Creating <span style="color: #7a0874; font-weight: bold;">&#91;</span>...<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>At this point, we want to add <cite>repoze.who</cite> to our project.  Open <tt class="docutils literal">authapp/config/middleware.py</tt> and add the following.  Take special note that the middleware setup is put AFTER the <tt class="docutils literal">if asbool(full_stack):</tt> line.  This is because the <cite>StatusCodeRedirect</cite> middleware will take the 401 error that we throw, and display an error page.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Add to imports</span>
<span style="color: #ff7700;font-weight:bold;">from</span> repoze.<span style="color: black;">who</span>.<span style="color: black;">config</span> <span style="color: #ff7700;font-weight:bold;">import</span> make_middleware_with_config <span style="color: #ff7700;font-weight:bold;">as</span> make_who_with_config
<span style="color: black;">&#91;</span>...<span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> make_app<span style="color: black;">&#40;</span>global_conf, full_stack=<span style="color: #008000;">True</span>, static_files=<span style="color: #008000;">True</span>, <span style="color: #66cc66;">**</span>app_conf<span style="color: black;">&#41;</span>:
<span style="color: black;">&#91;</span>...<span style="color: black;">&#93;</span>
    <span style="color: #808080; font-style: italic;"># CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> asbool<span style="color: black;">&#40;</span>full_stack<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># Handle Python exceptions</span>
        app = ErrorHandler<span style="color: black;">&#40;</span>app, global_conf, <span style="color: #66cc66;">**</span>config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'pylons.errorware'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># Display error documents for 401, 403, 404 status codes (and</span>
        <span style="color: #808080; font-style: italic;"># 500 when debug is disabled)</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> asbool<span style="color: black;">&#40;</span>config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'debug'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
            app = StatusCodeRedirect<span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            app = StatusCodeRedirect<span style="color: black;">&#40;</span>app, <span style="color: black;">&#91;</span><span style="color: #ff4500;">400</span>, <span style="color: #ff4500;">401</span>, <span style="color: #ff4500;">403</span>, <span style="color: #ff4500;">404</span>, <span style="color: #ff4500;">500</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># BEGIN REPOZE.WHO CODE</span>
    app = make_who_with_config<span style="color: black;">&#40;</span>app,
                               global_conf,
                               app_conf<span style="color: black;">&#91;</span><span style="color: #483d8b;">'repoze.who.ini'</span><span style="color: black;">&#93;</span>,
                               app_conf<span style="color: black;">&#91;</span><span style="color: #483d8b;">'repoze.who.log.file'</span><span style="color: black;">&#93;</span>,
                               app_conf<span style="color: black;">&#91;</span><span style="color: #483d8b;">'repoze.who.log.level'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># END REPOZE.WHO CODE</span></pre></div></div>

<p>Then, add the following lines in <cite>development.ini</cite> under the <tt class="docutils literal">[app:main]</tt> heading:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>app:main<span style="">&#93;</span></span>
repoze.who.ini <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> %<span style="">&#40;</span>here<span style="">&#41;</span>s/who.ini</span>
repoze.who.log.file <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> stdout</span>
repoze.who.log.level <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> debug</span></pre></div></div>

<p>Now, we&#8217;ll add a sample <cite>who.ini</cite>, which will fill in as this article progresses.  This should be in the same directory as your <cite>development.ini</cite> config.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>general<span style="">&#93;</span></span>
<span style="color: #000099;">request_classifier</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> repoze.who.classifiers:default_request_classifier</span>
<span style="color: #000099;">challenge_decider</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> repoze.who.classifiers:default_challenge_decider</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>identifiers<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>authenticators<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>challengers<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span></pre></div></div>

<p>At this point, we should have a functional Pylons stack setup.  But just to be sure, lets test it by running <cite>paster</cite>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster serve development.ini
Starting server <span style="color: #000000; font-weight: bold;">in</span> PID <span style="color: #000000;">10186</span>.
serving on http:<span style="color: #000000; font-weight: bold;">//</span>127.0.0.1:<span style="color: #000000;">5000</span></pre></div></div>

<p>At this point, you should be able to open a browser and navigate to <a class="reference external" href="http://127.0.0.1:5000">http://127.0.0.1:5000</a> and/or <a class="reference external" href="http://127.0.0.1:5000/secret/index">http://127.0.0.1:5000/secret/index</a>, and see either a &#8220;Pylons Page&#8221; or a &#8220;Hello World&#8221; page, respectively.  If you don&#8217;t see this, retrace your steps and try to debug the issue.</p>
<div id="basicauth" class="section">
<span id="BasicAuth"><h1>BasicAuth</h1></span>
<p>The first plug-in we&#8217;ll look at is BasicAuth, which fulfills the IIdentifier and IChallenger roles. Basic authentication is commonly used to protect pages, and although it may not be the most user-friendly, its a great way to get started. Because its included with <cite>repoze.who</cite>, it requires zero additional setup, except for the following additions to your <cite>who.ini</cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>identifiers<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> basicauth</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>challengers<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> basicauth</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>plugin:basicauth<span style="">&#93;</span></span>
<span style="color: #000099;">use</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> repoze.who.plugins.basicauth:make_plugin</span>
<span style="color: #000099;">realm</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> some_unique_security_realm</span></pre></div></div>

<p><cite>realm</cite> should be some quasi-descriptive name of your site or project.</p>
<p>Of course, we could never really login, since we have nothing to check the usernames/passwords against.</p>
<div id="htpasswd" class="section">
<span id="htpasswd"><h1>htpasswd</h1></span>
<p><cite>htpasswd</cite> is responsible for taking the information that the <cite>IIdentifier</cite> plugin (<cite>BasicAuth</cite>) has parsed out, and determine whether or not the user is actually allowed to access the page.  Because <cite>htpasswd</cite> needs to do some authentication, lets set up some users first.  An easy way to create hashes is using the variety of sites that provide interactive ways to do so, such as <a class="reference external" href="http://home.flash.net/cgi-bin/pw.pl">this one</a>.  Lets generate a few sample ones, and put them into a file called <cite>passwd</cite> in the same directory as our <cite>who.ini</cite>:</p>
<div class="highlight">

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;">andrew:2kRr2LXOu9cp6
robert:2ML8KpK./SeQI</pre></div></div>

<p>Now that we have users <cite>andrew</cite> and <cite>robert</cite> setup (their passwords are both the text <cite>secret</cite>), we can setup the <cite>htpasswd</cite> plug-in in <cite>who.ini</cite>:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>authenticators<span style="">&#93;</span></span>
<span style="color: #000099;">plugins</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> htpasswd</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>plugin:htpasswd<span style="">&#93;</span></span>
<span style="color: #000099;">use</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> repoze.who.plugins.htpasswd:make_plugin</span>
<span style="color: #000099;">filename</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> %<span style="">&#40;</span>here<span style="">&#41;</span>s/passwd</span></pre></div></div>

<p>At this point, we now have everything we need to authenticate.  But what happens when you start your server?  Its like we haven&#8217;t done <em>anything</em>, right?  That&#8217;s because we need some way to tell the <cite>repoze.who</cite> middleware when we want someone to have to authenticate to access that area of the site.</p>
<div id="protecting-actions" class="section">
<span id="Protecting_Actions"><h1>Protecting Actions</h1></span>
<p>For this, we&#8217;re going to create a new plug-in.  In all reality, this plug-in can exist anywhere you want.  For this tutorial, it will exist in <tt class="docutils literal">authapp.lib.authenticated</tt>.  Open up <cite>authapp/lib/authenticated.py</cite> and insert the following:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> decorator <span style="color: #ff7700;font-weight:bold;">import</span> decorator
<span style="color: #ff7700;font-weight:bold;">from</span> pylons <span style="color: #ff7700;font-weight:bold;">import</span>  request
<span style="color: #ff7700;font-weight:bold;">from</span> pylons.<span style="color: black;">controllers</span>.<span style="color: black;">util</span> <span style="color: #ff7700;font-weight:bold;">import</span> abort
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> user_is_authenticated<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Returns True if a user is authenticated, False otherwise &quot;&quot;&quot;</span>
    identity = request.<span style="color: black;">environ</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'repoze.who.identity'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> identity <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>
&nbsp;
@decorator
<span style="color: #ff7700;font-weight:bold;">def</span> authenticated<span style="color: black;">&#40;</span>func, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Decorator that will use Pylons' abort method to trigger the repoze.who
        middleware that this request needs to be authenticated. &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> user_is_authenticated<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        abort<span style="color: black;">&#40;</span><span style="color: #ff4500;">401</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> func<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span></pre></div></div>

<p>In a nut shell, this creates a decorator that will inform <cite>repoze.who</cite> if it needs to be authenticated.</p>
<p>At this point, you should now be able to start paster and access the secret page at <a class="reference external" href="http://127.0.0.1:5000/secret/index">http://127.0.0.1:5000/secret/index</a>, and wallah!  You should now be prompted with a <tt class="docutils literal">BasicAuth</tt> challenge!</p>
<div id="conclusion" class="section">
<span id="Conclusion"><h2>Conclusion</h2></span>
<p>In my next post, I hope to provide a little more flexible login situation by walking through writing a custom plug-in, and all that it entails.</p>
</div>
</div>
</div>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.andrew-mcfague.com/python/repoze-who-part-1-using-authentication-in-your-site/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Commands every serious *nix user should know</title>
		<link>http://www.andrew-mcfague.com/linux/utilities-linux/commands-every-serious-nix-user-should-know/</link>
		<comments>http://www.andrew-mcfague.com/linux/utilities-linux/commands-every-serious-nix-user-should-know/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 01:33:23 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[nc]]></category>
		<category><![CDATA[netcat]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[pgrep]]></category>
		<category><![CDATA[pkill]]></category>
		<category><![CDATA[tee]]></category>
		<category><![CDATA[watch]]></category>

		<guid isPermaLink="false">http://redmumba.wordpress.com/?p=61</guid>
		<description><![CDATA[Most blogs focus on some common commands--such as uptime, top, etc.--but this list is aimed at more hardcore users, featuring commands that are simply invaluable for system administration--be it personal or professional.]]></description>
			<content:encoded><![CDATA[<p>This isn&#8217;t your mother&#8217;s &#8220;10 Things you didn&#8217;t know Ubuntu could do&#8221; kind of list; these are low-level system commands that <em>will</em> prove to be invaluable as you begin to administer your system.  What follows are commands and their examples, as well as suggestions on when they should be used.</p>
<p><span id="more-61"></span><div class='toc wptoc'>
<h2>Contents</h2>
<ul class='toc-odd level-1'>
	<li>
		<a href="#Introduction">Introduction</a>
	</li>
	<li>
		<a href="#Commands">Commands</a>
		<ul class='toc-even level-2'>
<ul class='toc-odd level-3'>
			<li>
				<a href="#nc_netcat">nc (netcat)</a>
			</li>
			<li>
				<a href="#watch">watch</a>
			</li>
			<li>
				<a href="#parallel">parallel</a>
			</li>
			<li>
				<a href="#pgreppkill">pgrep/pkill</a>
			</li>
			<li>
				<a href="#tee">tee</a>
			</li>
		</ul>
</ul>
	<li>
		<a href="#Conclusion">Conclusion</a>
	</li>
</ul>
</div>
<div class='wptoc-end'>&nbsp;</div></p>
<span id="Introduction"><h1>Introduction</h1></span>
<p>As I use Linux and BSD on a day-to-day basis, there are some commands that are simply a godsend once you learn them.  Some are common but little is known about how they work; others are usually unheard of until the day someone demonstrates its use to you, and you wonder how you&#8217;ve lived so long without using that little gold nugget of knowledge.</p>
<span id="Commands"><h1>Commands</h1></span>
<span id="nc_netcat"><h3>nc (netcat)</h3></span>
<p>This is a great tool for several reasons.  First off, most people use it as a way of sending information; that is, you can use it to literally cat information across the network, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">cat</span> data.txt <span style="color: #000000; font-weight: bold;">|</span> nc host.example.com</pre></div></div>

<p>This will take the contents of http_data.txt and send them over the network to the specified host.  But did you know that nc can also <em>receive</em> data as well?  You can launch a listen-only server listening on port 8080 on localhost using the following:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Output any incoming requests on port 8080!</span>
nc <span style="color: #660033;">-l</span> <span style="color: #000000;">8080</span></pre></div></div>

<p>If I were to telnet to port 8080, I would be able to send information over the network.  This is especially useful if you have a program sending data, but need a quick and easy way of picking up the data.  On top of all that, you can have the netcat &#8220;server&#8221; send responses from stdin&#8211;that is, if your application sends a HELO, you can respond right back.  Pretty nifty, eh?</p>
<span id="watch"><h3>watch</h3></span>
<p>I remember when I was younger, every time I wanted to repeatedly check something (such as, while waiting for someone to upload a file), I&#8217;d either setup a loop or repeatedly run the command.  Little did I know, there was a command that already repeated a command for you; &#8220;watch&#8221; and learn.  Say I wanted to watch a directory for new files from an interactive terminal; perhaps I&#8217;m waiting for a client to upload something, or maybe I&#8217;m waiting for a process to finished.  I could periodically type ls to see if the file is there; or I could use watch.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Repeatedly run the ls command on /tmp</span>
<span style="color: #c20cb9; font-weight: bold;">watch</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>tmp</pre></div></div>

<p>This will, by default, run the command every two seconds.  Using -n, I can configure this time however I want.  But what if I&#8217;m watching something like memory usage, and I want to be able to look and see what&#8217;s changed between command runs?  Well, watch has that too!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Highlight differences between runs</span>
<span style="color: #c20cb9; font-weight: bold;">watch</span> <span style="color: #660033;">-d</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>tmp
&nbsp;
<span style="color: #666666; font-style: italic;"># Ditto, only this time, keep the changes across ALL runs;</span>
<span style="color: #666666; font-style: italic;"># So if I step away for an hour, it will show ALL new files</span>
<span style="color: #c20cb9; font-weight: bold;">watch</span> <span style="color: #660033;">-d</span>  --cumulative <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">/</span>tmp</pre></div></div>

<p>Isn&#8217;t that just the best little command you never knew about?  The <a href="http://linux.die.net/man/1/watch">man page</a> gives even <em>more</em> examples.</p>
<span id="parallel"><h3>parallel</h3></span>
<p>Every Linux users follows the same process.  First, they discover back ticks and use it to pipe the output of one command to another:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># If you're on 2.6.23, better hope there are less than 32k results</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #660033;">-type</span> f<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>Then, you learn about xargs, and realize the folly of your ways:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Uhoh, better hope there aren't any spaces, quotes,</span>
<span style="color: #666666; font-style: italic;">#   or other escape characters!</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span></pre></div></div>

<p>Soon, you learn to use null bytes to filter results to avoid those nasty line breaks:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Good luck trying to debug this without altering the command</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #660033;">-type</span> f <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span></pre></div></div>

<p>And what if you try to read the output?  What followed was the result of many years of frustration with the default state of the whitespace delimiter from the command line: <a href="http://www.gnu.org/software/parallel/">parallel</a>.  As a GNU utility, its available on pretty much any *nix platform worth its salt.  It operates identically to xargs, only it uses a line-by-line algorithm to pass arguments.  That is, spaces, escape characters, etc., no longer break up your output.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #000000; font-weight: bold;">/</span>tmp <span style="color: #660033;">-type</span> f <span style="color: #000000; font-weight: bold;">|</span> parallel <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-f</span></pre></div></div>

<p>Got a quote in your filename?  No problem!  Got spaces?  Its been handled.  parallel is pretty much what most people expect when using xargs.  And, because its part of the GNU tool set (and distributed under the GPL), you have no reason to not start using it right this very moment.</p>
<span id="pgreppkill"><h3>pgrep/pkill</h3></span>
<p>Like parallel, the step-by-step learning process of killing misbehaving processes usually goes like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ps</span> aux <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> java
<span style="color: #c20cb9; font-weight: bold;">kill</span></pre></div></div>

<p>Then, you learn about killall, which means you can kill any and all processes based on their process name:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Kill all `java' processes</span>
<span style="color: #c20cb9; font-weight: bold;">killall</span> java</pre></div></div>

<p>But lets say you&#8217;re running several instances of Java, but you only want to kill the ones associated with tomcat6?  killall won&#8217;t work, as you&#8217;re no longer killing based on the process name.  You can go back to the first method, but this is a lot of time and overheard for something that SHOULD be automated&#8211;and easy, considering how common of a task it is.  This is where pkill comes into play; instead of a process name or a pid, pkill takes a pattern&#8211;it can be a simple line of text, or an extended regular expression&#8211;and kills any processes matching it.  That is, I can now say:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Match anything where tomcat is in the command name;</span>
<span style="color: #666666; font-style: italic;"># `java-tomcat', but NOT `java -jar tomcat6.jar'</span>
pkill tomcat
&nbsp;
<span style="color: #666666; font-style: italic;"># Match anything where tomcat is in the FULL command line</span>
<span style="color: #666666; font-style: italic;"># `java-tomcat' &lt;em&gt;and&lt;/em&gt; `java -jar tomcat6.jar'</span>
pkill <span style="color: #660033;">-f</span> tomcat</pre></div></div>

<p>pgrep serves much the same purpose, only it will return matching pids instead of sending the processes signals.</p>
<span id="tee"><h3>tee</h3></span>
<p><span style="font-weight: normal; font-size: 13px;">A lot of people have probably seen this command at least once in their life, but may not have given it any thought.  I know that in the times that I have had a need for it, however, I would have had no alternatives without it.  tee is a tool that allows you to copy stdout to a file or files, while not disrupting stdout itself.  That is, I can now log output while display it to the screen.  For example, lets say I want to run a find command that not only stores the logs, but allows me to look at it.  I could pipe all output to the file and then view the file, but this runs into issues where its a continuous process that may not finish immediately&#8211;and I may not want to worry about backgrounding the process.</span> <span style="font-weight: normal; font-size: 13px;">For example, lets say I have a job to restart some servers; I want to be able to check the process as its running (and maybe even give it input as it runs), but I also want the entire session to be logged.</span> <span style="font-weight: normal; font-size: 13px;"> </span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>run_some_long_task.sh <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">tee</span> output.log</pre></div></div>

<p><span style="font-weight: normal; font-size: 13px;">Now, anytime there&#8217;s output, it will be recorded to stdout AND output.log.  Amazing, right?  I find this especially useful in <a href="https://hudson.dev.java.net/">Hudson</a> jobs where I want the output to be recorded in the build log, but I also want to be able to parse the output after the command has run.  It also lends itself to cron jobs, where the output can be simultaneously mailed to a user and written to disk.</span></p>
<span id="Conclusion"><h1>Conclusion</h1></span>
<p>There you have it, a list of five commands you&#8217;ve probably never heard of, or never took the time to investigate!  I know I would have been at a major loss for some of these commands, and I feel that these are generally not in most users&#8217; arsenals of commands.  If you&#8217;re a hardcore console user, there&#8217;s absolutely no reason you shouldn&#8217;t always be using these.  But for those of you who are not Linux gurus, I can promise you one thing&#8211;one day, in some random situation, you WILL be thankful for having learned these commands!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrew-mcfague.com/linux/utilities-linux/commands-every-serious-nix-user-should-know/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>tmux: a screen alternative</title>
		<link>http://www.andrew-mcfague.com/linux/utilities-linux/tmux-a-screen-alternative/</link>
		<comments>http://www.andrew-mcfague.com/linux/utilities-linux/tmux-a-screen-alternative/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 22:16:08 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[multiplexer]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[tmux]]></category>

		<guid isPermaLink="false">http://redmumba.wordpress.com/?p=16</guid>
		<description><![CDATA[tmux is a BSD-licensed program that offers up more features--and new approaches--to multiplexers.]]></description>
			<content:encoded><![CDATA[<p>One of the FreeBSD fans in my company stumbled upon a GNU screen alternative, that he insists renders it defunct.  Since my muscle memory will never allow a &#8220;smooth&#8221; transition to another multiplexer, I decided to make my first post an attempt to get myself (and others) introduced to this new stallion of multiplexer goodness.</p>
<p><span id="more-16"></span><br />
<div class='toc wptoc'>
<h2>Contents</h2>
<ul class='toc-odd level-1'>
	<li>
		<a href="#Improvements_over_screen">Improvements over screen</a>
	</li>
	<li>
		<a href="#Getting_Started">Getting Started</a>
	</li>
	<li>
		<a href="#Common_Tasks">Common Tasks</a>
		<ul class='toc-even level-2'>
			<li>
				<a href="#Conclusion">Conclusion</a>
			</li>
</ul>
</ul>
</ul>
</div>
<div class='wptoc-end'>&nbsp;</div></p>
<span id="Improvements_over_screen"><h2>Improvements over screen</h2></span>
<p>You can <a href="http://tmux.sourceforge.net/">grab a copy of the source code here</a>, but before you do, check with your package manager of choice.  Macports, Debian (and Ubuntu flavors), OpenSUSE, etc., all have native versions for your enjoyment&#8211;most likely under the obvious package name, <em>tmux</em>.</p>
<p>Both screen and tmux provide multiple buffers, the ability to detach/reattach sessions, locking, etc..  Where tmux really shines is the ability to split itself vertically and horizontally&#8211;something the screen team has <a href="http://fungi.yuggoth.org/vsp4s/">lagged behind on implementing themselves</a>.  Fans of windows managers, however, will really appreciate the master/stacking approach to screens, allowing you to have a selection of stacked terminal windows on the side, with a primary work window in the center.</p>
<span id="Getting_Started"><h2>Getting Started</h2></span>
<p>Its important that we learn the terminology in tmux, most importantly servers, clients, sessions, windows, panes, and pseudo-terminals.  It breaks down like so:</p>
<ul>
<li>The single <strong>server</strong> manages all <strong>sessions</strong>.</li>
<li><strong>Sessions</strong> contains one or more <strong>windows</strong>.</li>
<li><strong>Windows</strong> are a collection of <strong>panes</strong>.</li>
<li><strong>Panes</strong> are simply ways of laying out <strong>pseudo terminals</strong>.</li>
<li><strong>Clients</strong> allow the user to connect to a <strong>session</strong>.</li>
</ul>
<p>So the hierarchy isn&#8217;t hard to follow&#8211;in fact, I personally find it very easy to grasp due to the nature of the terms (i.e., window panes).  So lets gets this party started!</p>
<p>First and foremost, start up your client, tmux.  Since we haven&#8217;t configured or run this at all, the client would start-up, start a server, and create a session with a single window in it.  This can be accomplished by simply typing `tmux&#8217; in the command prompt.  Before you start mashing <em>C-a</em> (the default screen command key), here&#8217;s one of the quirks&#8211;<strong>in tmux, the default command key is </strong><em><strong>C-b</strong></em>, a choice I found to be extremely awkward on the fingers.  And, there&#8217;s the muscle memory.  Fortunately, <a href="http://tmux.cvs.sourceforge.net/viewvc/tmux/tmux/FAQ">the tmux FAQ</a> shows us how to set it to whatever we want; simply type <strong>C-b :</strong> from within tmux following in the terminal to disable C-b and use C-a (or a key of your choosing):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-g</span> prefix C-a
unbind C-b
<span style="color: #7a0874; font-weight: bold;">bind</span> C-a send-prefix</pre></div></div>

<p>From here on, assume that all bolded commands listed are preceded by the command key you set above.</p>
<span id="Common_Tasks"><h2>Common Tasks</h2></span>
<p>One of the most helpful things I&#8217;ve found about tmux is the ability to show the list of keybindings from anywhere, using <strong>?</strong>.</p>
<p>There are a few common keybindings you&#8217;ll want to keep in mind.  First, lets look at the ones with similar functions in GNU screen:</p>
<p><span style="line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; font-size: small;">
<table id="wp-table-reloaded-id-1-no-1" class="wp-table-reloaded wp-table-reloaded-id-1">
<thead>
	<tr class="row-1 odd">
		<th class="column-1">Action</th><th class="column-2">GNU Screen</th><th class="column-3">tmux</th>
	</tr>
</thead>
<tbody class="row-hover">
	<tr class="row-2 even">
		<td class="column-1">Create new window</td><td class="column-2">c</td><td class="column-3">c</td>
	</tr>
	<tr class="row-3 odd">
		<td class="column-1">Split window (horizontal)</td><td class="column-2">S</td><td class="column-3">"</td>
	</tr>
	<tr class="row-4 even">
		<td class="column-1">Rename window</td><td class="column-2">A</td><td class="column-3">,</td>
	</tr>
	<tr class="row-5 odd">
		<td class="column-1">Detach</td><td class="column-2">d</td><td class="column-3">d</td>
	</tr>
	<tr class="row-6 even">
		<td class="column-1">Kill window</td><td class="column-2">k, K</td><td class="column-3">&amp;</td>
	</tr>
	<tr class="row-7 odd">
		<td class="column-1">Make window fullscreen</td><td class="column-2">Q</td><td class="column-3">!</td>
	</tr>
	<tr class="row-8 even">
		<td class="column-1">Access program command prompt</td><td class="column-2">:</td><td class="column-3">:</td>
	</tr>
</tbody>
</table>
</span></p>
<p><strong>Layouts!</strong></p>
<p>With tmux, you can take a single window (literally, one pane) and split it into several panes, forming a rudimentary layout.  Lets start by splitting the screen vertically by pressing <strong><em>%</em><span style="font-weight: normal;"> a few times</span></strong>, which should give us several panes.  Now, we can cycle through the predefined layouts by using <strong><em>Space</em></strong>.  Once we have a layout selected, lets learn how to navigate within it.</p>
<ul>
<li><strong><em>q</em><span style="font-weight: normal;"> displays the open panes in your current window</span></strong></li>
<li><strong><em>Up</em></strong>/<em><strong>o</strong></em> selects the next pane, counter-clockwise</li>
<li><strong><em>Down </em><span style="font-weight: normal;">selects the Next/Previous pane</span></strong></li>
<li><strong> </strong><strong><em>C-o</em></strong> rotates the panes counter-clockwise</li>
</ul>
<span id="Conclusion"><h3>Conclusion</h3></span>
<p>That&#8217;s all there is to it!  90% of tmux is learning to mentally unbind the screen shortcuts, and rebind them to tmux&#8211;which pays off.  I hope this has given GNU Linux users a new alternative to screen&#8211;or at the very least, a new toy to play around with.  Feel free to ask questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.andrew-mcfague.com/linux/utilities-linux/tmux-a-screen-alternative/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/20 queries in 0.574 seconds using disk: basic
Object Caching 678/736 objects using disk: basic

Served from: www.andrew-mcfague.com @ 2012-02-22 20:49:32 -->
