<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title><![CDATA[ASCII Thoughts]]></title>
        <description><![CDATA[ASCII Thoughts]]></description>
        <link>http://asciithoughts.com</link>
        <generator>RSS for Node</generator>
        <lastBuildDate>Tue, 11 Oct 2016 03:36:29 GMT</lastBuildDate>
        <atom:link href="http://asciithoughts.com/feed.rss" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[Using Pry in production (part 3)]]></title>
            <description><![CDATA[<h1>Using Pry in production (part 3)</h1>
<p>This last trick is not specific to Pry, but is very useful nonetheless.</p>
<h2>Stuck request</h2>
<p>I had a problem recently where one particular discussion in my application wouldn&apos;t load. It looked like the error was a timeout, and as result Nginx was returning a 500. Since this was a timeout and not an exception, the error did not show in New Relic, which made it difficult to debug.</p>
<h2>To the console!</h2>
<p>My first check was to verify whether the same request would eventually run in the console, and if it was just really slow and needed optimization:</p> <pre><code>&gt; app.get &quot;/discussions/problems/1234&quot;
................ 10min later, still nothing ..............
</code></pre>
<p>So the request was stuck somewhere, but where? <code>set_trace_func</code> to the rescue!</p> <pre><code class="language-ruby">set_trace_func proc { <span class="hljs-params">|event, file, line, id, binding, klass|</span>
    <span class="hljs-keyword" style="color: #333; font-weight: bold;">if</span> event == <span class="hljs-string" style="color: #d14;">&quot;line&quot;</span> &amp;&amp; file[RAILS_ROOT]
      puts <span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-subst" style="color: #333; font-weight: normal;">#{file}</span>:<span class="hljs-subst" style="color: #333; font-weight: normal;">#{line}</span>&quot;</span>
    <span class="hljs-keyword" style="color: #333; font-weight: bold;">end</span>
}
</code></pre>
<p>Run the request again:</p> <pre><code>&gt; app.get &quot;/foo/bar/baz
/data/APP/current/config/initializers/action_controller.rb:8
/data/APP/current/config/initializers/action_controller.rb:11
/data/APP/current/config/initializers/action_controller.rb:11
[...]
/data/APP/current/config/app/models/comment/concerns/admin.rb:34
</code></pre>
<p>And there I had it. The exact line where the request was stuck. It turned out to be a bad string/regex combination, but I&apos;m not sure how else I could have figured it out. <code>set_trace_func</code> definitely was handy.</p>
<h2>Logging SQL requests</h2>
<p>Also very useful when debugging requests in the console:</p> <pre><code class="language-ruby"><span class="hljs-comment" style="color: #998; font-style: italic;"># Rails 2</span>
ActiveRecord::Base.connection.instance_variable_set <span class="hljs-symbol" style="color: #990073;">:</span>@logger, Logger.new(STDOUT)

<span class="hljs-comment" style="color: #998; font-style: italic;"># Rails 3 &amp; 4</span>
ActiveRecord::Base.logger = Logger.new(STDOUT)
</code></pre>
<h2>Conclusion</h2>
<p>So that concludes our short serie on Pry. I hope you enjoyed it :)</p>
<p>That&apos;s it for today, cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/05/25/using-pry-in-production-part-3/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/05/25/using-pry-in-production-part-3/</guid>
            <pubDate>Sun, 25 May 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Using Pry in production (part 2)]]></title>
            <description><![CDATA[<h1>Using Pry in production (part 2)</h1>
<p>Last week, we took a look at the basics of using
  <a href="http://pryrepl.org/">Pry</a> in production as a replacement for the standard IRB console. Today, let&apos;s look at a practical scenario and see how Pry can help us debug a misbehaving request.</p>
<h2>Why use Pry?</h2>
<p>After all, we already have New Relic which gives us a stack trace of every error in our application.</p>
<p>Well, many errors do not result in an exception. Oftentimes the application responds, but with the wrong data. And oftentimes as well, the error only occurs in rare cases dependent on production data: it&apos;s hard to reproduce an error locally if you don&apos;t even know what makes the error occur in the first place.</p>
<p>There are 2 ways developers deal with this problem:</p>
<ul>
  <li>
    <p>Have a full copy of the production environment (with up-to-date production data). While this may be feasible for small datasets, it becomes harder and harder as the data grows. Customers will also often complain about an error that just appeared, making it more likely that the important data does not yet exist in the test environment.</p>
  </li>
  <li>
    <p>Connect a local development environment to a remote database. I find this method too dangerous: it&apos;s a disaster waiting to happen. You should not trust yourself with a direct connection to the production database on your machine. Ever.</p>
  </li>
</ul>
<p>In these situations, I find that Pry offers just enough functionality to do some actual debugging in production, while keeping you relatively safe.</p>
<h2>PryNav</h2>
<p>I like
  <a href="https://github.com/nixme/pry-nav">pry-nav</a> better than
  <a href="https://github.com/nixme/pry-debugger">pry-debugger</a> or
  <a href="https://github.com/deivid-rodriguez/pry-byebug">pry-byebug</a> because it works with every version of Ruby, and being pure ruby, doesn&apos;t require me to include the <code>debugger</code> gem in production. I can thus safely include <code>pry</code> and <code>pry-nav</code> in my <code>Gemfile</code>, keeping them unloaded, and only require them when I start the Rails console manually with my <code>pryrc</code>. You get the best of both worlds: no impact on running production servers, but the flexibility of a basic debugger when you need one. And it works everywhere.</p> <pre><code class="language-ruby"><span class="hljs-comment" style="color: #998; font-style: italic;"># Gemfile</span>
gem <span class="hljs-string" style="color: #d14;">&quot;pry&quot;</span>, <span class="hljs-symbol" style="color: #990073;">:require</span> =&gt; <span class="hljs-literal" style="color: #008080;">false</span>
gem <span class="hljs-string" style="color: #d14;">&quot;pry-nav&quot;</span>, <span class="hljs-symbol" style="color: #990073;">:require</span> =&gt; <span class="hljs-literal" style="color: #008080;">false</span>
</code></pre>
<h2>Scenario</h2>
<p>One of my apps is a customer support app, and so it deals a lot with discussions, comments, support staff, etc. I had a case recently where a customer could not assign a particular discussion to a particular user. It worked (and had been working) for every other discussion and every other user, so it was fairly obvious that there was something peculiar about the data that triggered a bug. A first exploration of the code involved did not reveal any obvious clue, so this was thus a perfect use case for Pry.</p>
<h4>Step 1: Authenticate</h4>
<p>Rails provides you with a few methods to interact with your app directly from the console (see this
  <a href="http://pragmaticstudio.com/blog/2006/4/4/running-your-rails-app-headless">blog post</a> for a good introduction). The first step of every debugging session is often to authenticate as the user with something like that:</p> <pre><code class="language-ruby">[<span class="hljs-number" style="color: #008080;">1</span>] pry(main)&gt; app.post <span class="hljs-string" style="color: #d14;">&quot;/login&quot;</span>,  <span class="hljs-symbol" style="color: #990073;">:user</span> =&gt; {<span class="hljs-symbol" style="color: #990073;">:login</span> =&gt; <span class="hljs-string" style="color: #d14;">&apos;joe&apos;</span>, <span class="hljs-symbol" style="color: #990073;">:password</span> =&gt; <span class="hljs-string" style="color: #d14;">&apos;password&apos;</span>}
</code></pre>
<p>For real applications however, this is unpractical as passwords are not stored in clear text in the database. We still need to login as our user though... While we can&apos;t use a login/password, most apps will have a <code>current_user</code> method or similar, that we can
  <strong>patch</strong>.</p>
<p>First, let&apos;s edit our <code>current_user</code> method:</p> <pre><code>[1] pry(main)&gt; edit Authenticated#current_user -p
</code></pre>
<p>And overwrite it:</p> <pre><code class="language-ruby"><span class="hljs-function"><span class="hljs-keyword" style="color: #333; font-weight: bold;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold;">current_user</span></span>
  <span class="hljs-keyword" style="color: #333; font-weight: bold;">return</span> User.find(<span class="hljs-number" style="color: #008080;">1234</span>)
<span class="hljs-keyword" style="color: #333; font-weight: bold;">end</span>
</code></pre>
<p>From now on, every request will be made as user 1234.</p>
<h4>Step 2: Set our breakpoint</h4>
<p>We know our entry point: the controller action. So let&apos;s start there:</p> <pre><code>[2] pry(main)&gt; edit DiscussionsController#assign -p
</code></pre>
<p>and add a call to Pry:</p> <pre><code class="language-ruby"><span class="hljs-function"><span class="hljs-keyword" style="color: #333; font-weight: bold;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold;">assign</span></span>
  binding.pry
  [...]
<span class="hljs-keyword" style="color: #333; font-weight: bold;">end</span>
</code></pre>
<p>Now, call our action:</p> <pre><code>[3] pry(main)&gt; app.post &quot;/discussions/problems/1234/assign&quot;, :user_id =&gt; 768

From: /data/APP/current/app/controllers/discussions_controller.rb @ line 72 DiscussionsController#assign:

    71: def assign
 =&gt; 72:   binding.pry
    73:   @discussion.assign(user)
          [...]
    89: end

[1] pry(#&lt;DiscussionsController&gt;)&gt;
</code></pre>
<p>So let&apos;s try the basics: run the line and verify if it works or not.</p> <pre><code>[1] pry(#&lt;DiscussionsController&gt;)&gt; @discussion.assign(user)
=&gt; nil

[2] pry(#&lt;DiscussionsController&gt;)&gt; @discussion.assignments
=&gt; []
</code></pre>
<p>That doesn&apos;t fair well.. Let&apos;s step inside the method instead:</p> <pre><code>[3] pry(#&lt;DiscussionsController&gt;)&gt; step

From: /data/APP/current/app/models/discussion/concerns/assignments.rb @ line 87 Discussion#assign:

    86: def assign(user)
 =&gt; 87:   unless assigned?(user)
    88:     assignment = Assignment.create(:discussion =&gt; self, :user =&gt; user)
    89:   end
    90: end
    
[4] pry(#&lt;DiscussionsController&gt;)&gt;
</code></pre>
<p>Something looks fishy:</p> <pre><code>[4] pry(#&lt;DiscussionsController&gt;)&gt; assigned?(user)
=&gt; true

[5] pry(#&lt;DiscussionsController&gt;)&gt; assignments
[]
</code></pre>
<p>Let&apos;s look inside <code>assigned?</code></p> <pre><code>[6] pry(#&lt;DiscussionsController&gt;)&gt; step

From: /data/APP/current/app/models/discussion/concerns/assignments.rb @ line 123 Discussion#assigned?:

    122: def assigned?(user)
 =&gt; 123:   REDIS.sismember(RedisKey.assignments(self.id), user.id)
    124: end
    
[7] pry(#&lt;DiscussionsController&gt;)&gt;
</code></pre>
<p>And there is our bug! The <code>assigned?</code> method looks in our Redis cache to see if the discussion is already assigned to this user, but <code>assignments</code> looks in the database. Somehow the data in Redis has been corrupted, and is no more in sync with the database.</p>
<h2>Step 3: Fix it</h2>
<p>Now that we understand the bug, we can go back to our development environment and start writing tests, a fix, etc.. We can also start looking at logs to figure out how the Redis data got corrupted in the first place: we will have to fix that as well.</p>
<h2>Conclusion</h2>
<p>While Pry should not be used as the &quot;be-all and end-all&quot; for production problems, it does come in handy to debug issues related to data, that are hard or impossible to reproduce in other environments. By selectively patching code in the session, and stepping into requests, we can quickly figure out the source of problems, and then go back to our normal workflow to write the fix.</p>
<p>I hope I was able to convince you that Pry is totally awesome. But don&apos;t take my word for it, try it for yourself: you&apos;ll be hooked!</p>
<p>That&apos;s it for today, cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/05/18/using-pry-in-production-part-2/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/05/18/using-pry-in-production-part-2/</guid>
            <pubDate>Sun, 18 May 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Using Pry in production (part 1)]]></title>
            <description><![CDATA[<h1>Using Pry in production (part 1)</h1>
<p>If you don&apos;t know about
  <a href="http://pryrepl.org/">Pry</a> yet, you&apos;re missing out. It&apos;s a replacement REPL for Ruby with tons of features and many plugins, which makes it a perfect replacement for IRB, and as a result, for the standard Rails console.</p>
<p>A little while ago, the fine folks at
  <a href="https://bugsnag.com">Bugsnag</a> wrote
  <a href="https://bugsnag.com/blog/production-pry">an article</a> about their usage of
  <a href="http://pryrepl.org/">Pry</a> in production. I&apos;ve been a Pry convert for quite some time as well, so today I&apos;d like to share my own tips and tricks.</p>
<h2>Running Pry</h2>
<p>I think the
  <a href="https://bugsnag.com/blog/production-pry">Bugsnag article</a> nails it pretty well, though I tweak some of the steps:</p>
<ul>
  <li>
    <p>
      <strong>Use a dedicated machine</strong>: it&apos;s pretty easy for a console session to start eating up memory and resources, and you don&apos;t want that to disrupt your running application. The best machine is usually a job runner machine: all configured with the app, but not running any webserver.</p>
  </li>
  <li>
    <p>
      <strong>Setup your Gemfile</strong>: instead of using <code>pry-rails</code>, I prefer to use the <code>pryrc</code> technique which I&apos;ll get back to in a minute. So my <code>Gemfile</code> looks like this:</p> <pre><code class="language-ruby"><span class="hljs-comment" style="color: #998; font-style: italic;"># Gemfile</span>
gem <span class="hljs-string" style="color: #d14;">&apos;pry&apos;</span>, <span class="hljs-symbol" style="color: #990073;">:require</span> =&gt; <span class="hljs-literal" style="color: #008080;">false</span>
gem <span class="hljs-string" style="color: #d14;">&apos;pry-nav&apos;</span>, <span class="hljs-symbol" style="color: #990073;">:require</span> =&gt; <span class="hljs-literal" style="color: #008080;">false</span>
</code></pre></li>
  <li>
    <p>
      <strong>Use a shortcut</strong>: mine is called <code>t1</code>. This is only for quick edits though, not for long running tasks. If you are going to leave a session running for a while, processing data, you definitely want to run it inside a <code>screen</code> or <code>tmux</code> session.</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># ~/.bashrc</span>
<span class="hljs-built_in" style="color: #0086b3;">alias</span> t1=<span class="hljs-string" style="color: #d14;">&apos;ssh -t X.X.X.X -C &quot;cd /data/APP/current &amp;&amp; RAILS_ENV=production bundle exec pry&quot;&apos;</span>
</code></pre></li>
</ul>
<h2>Configure Pry</h2>
<p>Since we are using a dedicated machine, we can have a shared <code>pryrc</code> for everyone that offers some Rails goodies. We are not using <code>pry-rails</code>, so we also need some of the code in <code>~/.pryrc</code> to do the Rails initialization:</p> <pre><code class="language-ruby"><span class="hljs-comment" style="color: #998; font-style: italic;"># ~/.pryrc</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># MISSING</span>
</code></pre>
<h2>Play</h2>
<p>The first benefit of using Pry is that you get an actual editor for your code. I tend to do a lot of support related tasks, and so I often need to run one off snippets of code. They are slightly more complicated than a one-liner, but at the same time quite straight forward to write and modify. So my usual workflow is this:</p> <pre><code>PRODUCTION [1] pry(main)&gt; def find_ips; end
PRODUCTION [1] pry(main)&gt; edit find_ips
</code></pre>
<p>Since Pry is configured with vim as the default editor, this drops me in vim where I can start writing my code:</p> <pre><code class="language-ruby"><span class="hljs-keyword" style="color: #333; font-weight: bold;">require</span> <span class="hljs-string" style="color: #d14;">&quot;set&quot;</span>

<span class="hljs-comment" style="color: #998; font-style: italic;"># Check all the IP addresses used by one user</span>
<span class="hljs-function"><span class="hljs-keyword" style="color: #333; font-weight: bold;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold;">find_ips</span></span>
  list = Set.new
  user = User.find <span class="hljs-number" style="color: #008080;">1234</span>
  user.comments.each {<span class="hljs-params">|comment|</span> list &lt;&lt; comment.ip}
  list.each {<span class="hljs-params">|ip|</span> puts ip}
<span class="hljs-keyword" style="color: #333; font-weight: bold;">end</span>
</code></pre>
<p>The code is simple, but being able to work with it in a real editor is a big improvement. We can now run it, make adjustments, and iterate until we get it just right:</p> <pre><code>PRODUCTION [1] pry(main)&gt; find_ips
X.X.X.X
X.X.X.X
X.X.X.X

PRODUCTION [1] pry(main)&gt; edit find_ips
# do some changes

PRODUCTION [1] pry(main)&gt; find_ips
X.X.X.X on Feb 1st, 2014 at 8:14:45 AM
X.X.X.X on Mar 2nd, 2014 at 10:01:32 PM
[...]
</code></pre>
<p>The other nice thing is that it makes it a lot easier to extract useful code. As I try to solve a particular problem, or extract data, I will often end up with a method 10 to 20 lines long that solves a particular support use case. If I want to save it for later, or integrate it into the main codebase, I can simply edit it again, and save it to a safe location from vim, or copy/paste it somewhere else. This would be a lot harder if all I had was a bunch of one-liners that only existed in the history of my session.</p>
<h2>Patch</h2>
<p>One of the most useful feature of Pry is it&apos;s ability to easily monkeypatch existing code. Let&apos;s say that we have a job that generates a CSV export of some reporting data. The export is great and all customers love it, but this one customer asked you to add another column to it. They have a somewhat narrow use case, and including the column for all customers would not necessarily be an improvement, so you decide to simply run the report once with this extra column, just for them. With Pry, this is super easy!</p>
<p>First, we take a look at the CSV export itself:</p> <pre><code>PRODUCTION [1] pry(main)&gt; show-source Report#generate_csv

def generate_csv
  [...]
  documents.each {|doc| csv &lt;&lt; doc.csv}
  [...]
end
</code></pre>
<p>So we know that we need to modify the <code>Document#csv</code> method. It&apos;s a simple method that returns an array. We are going to
  <strong>patch</strong> the method: the modification will only exist within our Pry session and won&apos;t be written on disk. To do that, we use <code>edit -p</code> instead of the standard <code>edit</code>:</p> <pre><code>PRODUCTION [1] pry(main)&gt; edit Document#csv -p
</code></pre>
<p>Add the extra data:</p> <pre><code class="language-ruby"><span class="hljs-function"><span class="hljs-keyword" style="color: #333; font-weight: bold;">def</span> <span class="hljs-title" style="color: #900; font-weight: bold;">csv</span></span>
  [ title,
    content,
    created_at,
    updated_at,
    custom_data ] <span class="hljs-comment" style="color: #998; font-style: italic;"># LINE ADDED</span>
<span class="hljs-keyword" style="color: #333; font-weight: bold;">end</span>
</code></pre>
<p>We can now generate the report for this customer, without affecting anyone else:</p> <pre><code>PRODUCTION [1] pry(main)&gt; Report.find(1234).generate_csv
</code></pre>
<p>And we&apos;re done!</p>
<h2>Conclusion</h2>
<p>Pry is a perfect replacement for IRB, and can greatly improve your life when working on production applications. Just for the ability to edit code in vim, Pry is absolutely worth it. I definitely recommend spending some time installing it, configuring it, and getting it working on your application.</p>
<p>And if you&apos;ve never used Pry before, take a look at this great talk:
  <a href="https://www.youtube.com/watch?v=D9j_Mf91M0I">Ruby Conf 2013 - REPL driven development with Pry by Conrad Irwin</a>. It will make you an instant convert.</p>
<p>That&apos;s it for today, cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/05/11/using-pry-in-production-part-1/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/05/11/using-pry-in-production-part-1/</guid>
            <pubDate>Sun, 11 May 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[SSH authentication on Github for multiple users]]></title>
            <description><![CDATA[<h1>SSH authentication on Github for multiple users</h1>
<p>I have 2 Github accounts: a personal one, and a professional one. If you have more than one account on Github, you&apos;ve probably experienced this before:</p> <pre><code>ERROR: Permission to user2/repo.git denied to user1.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
</code></pre>
<h2>Why does this happen?</h2>
<p>To better understand what&apos;s going on, let&apos;s assume we have 2 users:</p>
<ul>
  <li><code>user1</code> is the main profile, associated with <code>~/.ssh/id_rsa</code></li>
  <li><code>user2</code> is a secondary profile, associated with <code>~/.ssh/id_github_rsa</code></li>
</ul>
<p>First, let&apos;s get some debugging info. In <code>~/.ssh/config</code>, add:</p> <pre><code>Host github.com
  LogLevel DEBUG
</code></pre>
<p>Then try again a <code>git push</code> in a repository owned by <code>user2</code>:</p> <pre><code>debug1: Connecting to github.com [192.30.252.131] port 22.
debug1: Connection established.
[...]
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa
[...]
debug1: Authentication succeeded (publickey).
</code></pre>
<p>The key used for authentication is <code>id_rsa</code> attached to <code>user1</code>. This is the problem. SSH tries keys in a certain order, and <code>id_rsa</code> occurs first. The authentication succeeds at the SSH level (there IS a user attached to this key), but fails at the git level because <code>user1</code> is not a member of the repository we are trying to push to, owned by <code>user2</code>.</p>
<p>Now that we understand what the problem is, let&apos;s try to fix it.</p>
<h2>First attempt</h2>
<p>Since SSH allows for custom configuration per host, the first idea that comes to mind is to create a special host with a preferred key. In <code>~/.ssh/config</code>, let&apos;s add a section for <code>user2</code>:</p> <pre><code>Host github-user2
  User git
  Hostname github.com
  IdentityFile ~/.ssh/id_github_rsa
</code></pre>
<p>We will also need to update our github repository. We need to change the current <code>origin</code> from its default <code>git@github.com</code> to <code>github-user2</code>:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">cd</span> ~/projects/user2/project
git remote <span class="hljs-built_in" style="color: #0086b3;">set</span>-url origin github-user2:user2/project.git
</code></pre>
<p>Great! Let&apos;s try to push again:</p> <pre><code class="language-bash">ERROR: Permission to user2/project.git denied to user1.
fatal: Could not <span class="hljs-built_in" style="color: #0086b3;">read</span> from remote repository.

Please make sure you have the correct access rights
and the repository exists.
</code></pre>
<p>Mmmm.. Looks like that didn&apos;t do it. Let&apos;s increase our log level to the maximum:</p> <pre><code>Host github-user2
  LogLevel DEBUG3
  User git
  Hostname github.com
  IdentityFile ~/.ssh/id_github_rsa
</code></pre>
<p>And push again:</p> <pre><code>debug2: ssh_connect: needpriv 0
debug1: Connecting to github.com [192.30.252.128] port 22.
debug1: Connection established.
[...]
debug2: key: ~/.ssh/id_rsa (0x700100200300),
debug2: key: ~/.ssh/id_github_rsa (0x700200300400), explicit
[...]
debug1: Next authentication method: publickey
debug1: Offering RSA public key: ~/.ssh/id_rsa
[...]
debug1: Authentication succeeded (publickey).
</code></pre>
<p>And so here is where weirdness happens: <code>id_github_rsa</code> key seems to be recognized from our git config (marked as <code>explicit</code>), but somehow <code>id_rsa</code> is still used first. Why?</p>
<p>It took me a minute to figure this one out. I spent some time searching and eventually found
  <a href="http://sealedabstract.com/code/github-ssh-with-multiple-identities-the-slightly-more-definitive-guide/">this blog post</a> by Drew Crawford. The problem comes from the way <code>IdentityFile</code> works. From the man:</p>
<blockquote>
  <p>Specifies a file from which the user&apos;s DSA, ECDSA or RSA authentication identity is read. The default is <code>~/.ssh/identity</code> for protocol version 1, and <code>~/.ssh/id_dsa</code>, <code>~/.ssh/id_ecdsa</code> and <code>~/.ssh/id_rsa</code> for protocol version 2.
    <strong>Additionally, any identities represented by the authentication agent will be used for authentication.</strong> [...]</p>
</blockquote>
<p>So despite specifying a preferred key for authentication, SSH will
  <strong>first</strong> try keys already loaded in the agent. Since <code>id_rsa</code> is our main key and we use it everywhere, chances are it&apos;s loaded in the agent first, therefore overriding our configuration.</p>
<p>With this new information in hand, let&apos;s update our SSH config.</p>
<h2>Second attempt</h2>
<p>So we need to tell SSH to ignore any keys already loaded in the agent, and just use the one we specify. Thankfully, there is an option for that, <code>IdentitiesOnly</code>:</p>
<blockquote>
  <p>Specifies that ssh(1) should only use the authentication identity files configured in the ssh_config files, even if ssh-agent(1) or a PKCS11Provider offers more identities. The argument to this keyword must be <code>yes</code> or <code>no</code>. This option is intended for situations where ssh-agent offers many different identities. The default is <code>no</code>.</p>
</blockquote>
<p>Let&apos;s update our <code>~/.ssh/config</code>:</p> <pre><code>Host github-user2
  User git
  Hostname github.com
  IdentitiesOnly yes
  IdentityFile ~/.ssh/id_github_rsa
</code></pre>
<p>And try one last push:</p> <pre><code class="language-bash">$ <span class="hljs-built_in" style="color: #0086b3;">cd</span> ~/projects/user2/project
$ git push
Everything up-to-date
</code></pre>
<p>Hooray \o/</p>
<h2>Recap</h2>
<p>So to successfully authenticate on Github with a secondary user, you need to:</p>
<ol>
  <li>Create a dedicated SSH config with <code>IdentityFile</code> and <code>IdentitiesOnly</code></li>
  <li>Update the <code>origin</code> URL of every repo to use the SSH config: <code>github-user:user/repo</code></li>
</ol>
<p>Not that complicated, but not easy to figure out either. It certainly took me a minute, especially since my keys were not always loaded in the same order in the SSH agent, which confused things a bit. So I hope this will avoid you some headaches :)</p>
<p>That&apos;s it for today, cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/05/04/ssh-authentication-on-github-for-multiple-users/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/05/04/ssh-authentication-on-github-for-multiple-users/</guid>
            <pubDate>Sun, 04 May 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Improve your battery life with Activity Monitor]]></title>
            <description><![CDATA[<h1>Improve your battery life with Activity Monitor</h1>
<p>OS X Mavericks introduced a new tab in Activity Monitor:
  <strong>Energy</strong>. The nice thing about this new pane is that it includes an average. If you run on battery a lot, and are looking for ways to maximize your battery life, this gives you instant insight into the power hogs on your system:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140427-01.png">
    <img src="http://asciithoughts.com/images/20140427-01.png" alt="Activity Monitor">
  </a>
</p>
<p>I&apos;m definitely not going to kill Chrome or Propane, but it&apos;s interesting to notice that the simple Little Snitch Network Monitor is using so much battery, compared to Evernote for example (which is also running all the time). A quick change of settings and you get an instant battery win:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140427-02.png">
    <img src="http://asciithoughts.com/images/20140427-02.png" alt="Little Snitch settings">
  </a>
</p>
<p>Looking at this section regularly, I was able to tweak the settings of a few applications (I&apos;m looking at you
  <a href="http://www.code42.com/crashplan/">Crashplan</a>) and got a noticeable improvement in overall battery life. I&apos;ve also been enjoying
  <a href="http://fruitjuiceapp.com/">FruitJuice</a>, which tells you when to plug and when to use the battery.</p>
<p>So if you are running Mavericks and looking for ways to maximize your time away from an outlet, take a look at Activity Monitor: it&apos;s fast and easy.</p>
<p>That&apos;s it for today, cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/04/27/improve-your-battery-life-with-activity-monitor/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/04/27/improve-your-battery-life-with-activity-monitor/</guid>
            <pubDate>Sun, 27 Apr 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Download videos from any site with youtube-dl]]></title>
            <description><![CDATA[<h1>Download videos from (almost) any site with youtube-dl</h1>
<p>Continuing on last week theme of downloading videos,
  <a href="http://rg3.github.io/youtube-dl/">youtube-dl</a> is a nifty little utility that allows you to download videos to watch later, or for safekeeping.</p>
<p>Install with
  <a href="http://brew.sh/">Homebrew</a>:</p> <pre><code class="language-bash">brew install youtube-dl
</code></pre>
<p>You can now download videos from YouTube, BlipTV, Brightcove, Dailymotion, TED and many other
  <a href="http://rg3.github.io/youtube-dl/supportedsites.html">supported sites</a>:</p> <pre><code class="language-bash">$ youtube-dl https://www.youtube.com/watch?v=fK4mokbaaAU
<span class="hljs-comment" style="color: #998; font-style: italic;"># [youtube] Setting language</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [youtube] fK4mokbaaAU: Downloading webpage</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [youtube] fK4mokbaaAU: Downloading video info webpage</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [youtube] fK4mokbaaAU: Extracting video information</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [download] Destination: XXY (Collision Course).mp4</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [download] 100% of 29.55MiB in 01:36</span>
</code></pre>
<p>Enjoy!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/04/13/download-videos-from-any-site-with-youtube-dl/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/04/13/download-videos-from-any-site-with-youtube-dl/</guid>
            <pubDate>Sun, 13 Apr 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Subscribe to YouTube users in iTunes]]></title>
            <description><![CDATA[<h1>Subscribe to YouTube users in iTunes</h1>
<p>I love my commute: I love catching up on tech news in the morning, and indulge in silly entertainment in the evening. It is my moment of piece, my transition between work and home.</p>
<p>While Feedly has long solved the problem of text content, I&apos;ve often wanted a simple way to catch up on all my favorite youtubers on the way back home. Unfortunately, the time I want to consume videos is also the time where it is the less practical to do so.</p>
<h2>iTunes podcasts and RSS Handler</h2>
<p>The nice thing about iTunes podcasts is that iTunes downloads them for you. Just like RSS feeds, there is no work for me to do: just let the content come to you. It also solves the bandwidth problem when commuting: just sync before leaving, and you&apos;re good to go.</p>
<p>
  <a href="https://code.google.com/p/rsshandler/">RSS Handler</a> is a standalone application that allows you to subscribe to YouTube users, playlists, etc., in iTunes. Just type in the username:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-01.png">
    <img src="http://asciithoughts.com/images/20140406-01.png" alt="RSS Handler">
  </a>
</p>
<p>Copy it in iTunes</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-02.png">
    <img src="http://asciithoughts.com/images/20140406-02.png" alt="Paste in iTunes">
  </a>
</p>
<p>And iTunes will now download those videos for you:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-03.png">
    <img src="http://asciithoughts.com/images/20140406-03.png" alt="iTunes window">
  </a>
</p>
<h2>Improving the worflow</h2>
<p>While RSS Handler is great, finding the username, pasting it in the app, generating the URL and pasting it in iTunes, is just too much work. So I wrote a simple
  <a href="http://userscripts.org:8080/scripts/show/435131">user script</a> to simplify the process.</p>
<p>1st, make sure you have either
  <a href="https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/">Greasemonkey</a> (Firefox), or
  <a href="https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en">Tampermonkey</a> (Chrome), installed. Then go and install
  <a href="http://userscripts.org:8080/scripts/show/435131">this user script</a>:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-04.png">
    <img src="http://asciithoughts.com/images/20140406-04.png" alt="User script">
  </a>
</p>
<p>Once you&apos;re done, you should see a small &quot;iTunes&quot; button at the bottom of user pages:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-05.png">
    <img src="http://asciithoughts.com/images/20140406-05.png" alt="iTunes link">
  </a>
</p>
<p>Choose a title for the podcast:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-06.png">
    <img src="http://asciithoughts.com/images/20140406-06.png" alt="Choose a title">
  </a>
</p>
<p>And you will be automatically subscribed in iTunes:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140406-07.png">
    <img src="http://asciithoughts.com/images/20140406-07.png" alt="Choose a title">
  </a>
</p>
<h2>Conclusion</h2>
<p>That&apos;s it for today, enjoy!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/04/06/subscribe-to-youtube-users-in-itunes/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/04/06/subscribe-to-youtube-users-in-itunes/</guid>
            <pubDate>Sun, 06 Apr 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Analyze disk space with ncdu]]></title>
            <description><![CDATA[<h1>Analyze disk space with ncdu</h1>
<p>
  <a href="http://dev.yorhel.nl/ncdu">NCurses Disk Usage</a>, or <code>ncdu</code> for short, is a graphical disk space analyzer:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140330-ncdu.gif">
    <img src="http://asciithoughts.com/images/20140330-ncdu.gif" alt="Using ncdu">
  </a>
</p>
<h2>Why you should use it</h2>
<ul>
  <li>It&apos;s incredibly fast</li>
  <li>It&apos;s available on every environment</li>
  <li>You can delete files directly within the app</li>
  <li>You can limit your search to one filesystem with <code>-x</code> (no <code>/dev</code>, etc)</li>
</ul>
<h2>Where you should use it</h2>
<p>While I regularly use <code>ncdu</code> on my Mac, <code>ncdu</code> shines on servers. More than once I found myself with a hard drive close to full and no idea why? <code>ncdu</code> allowed me to scan the disk, find the source of the problem and fix it, all within minutes. Often times the issue was a renegade process creating huge amounts of data at a totally unexpected place, and finding the culprit without the &quot;drill-down&quot; approach of <code>ncdu</code> would have amounted to searching a needle in a haystack.</p>
<p>So now, I make sure it&apos;s installed on every server I work with. You never know when you will need it to save the day.</p>
<p>That&#x2019;s it for today. Cheers ;)</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/03/30/analyze-disk-space-with-ncdu/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/03/30/analyze-disk-space-with-ncdu/</guid>
            <pubDate>Sun, 30 Mar 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Speed up your tests and save your SSD with mrd]]></title>
            <description><![CDATA[<h1>Speed up your tests and save your SSD with mrd</h1>
<p>
  <a href="https://github.com/mezis/mrd">mrd</a> creates a virtual disk in memory, and runs an instance of MySQL on it. The main application is running tests: no matter how fast your hard drive is, it will never be as fast as RAM. Since test data is volatile anyway, why not use the fastest medium available. As an added bonus, because it prevents all these volatile writes from going to disk, it can increase the lifespan of your SSD.</p>
<p>While <code>mrd</code> is great, it&apos;s only great if you use it all the time. So in this article I&apos;ll share a few tips that have made using <code>mrd</code> a much more pleasant experience for me.</p>
<h2>Installing it</h2>
<p>Similar to our previous article on Mailcatcher, If you have a reasonably recent version of Linux or Mac OS X, you probably have Ruby installed. If not, you will have to
  <a href="https://www.ruby-lang.org/en/installation/">install Ruby first</a>. Once you have Ruby installed:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># If you are using the system&apos;s Ruby:</span>
sudo gem install mrd

<span class="hljs-comment" style="color: #998; font-style: italic;"># If you use rbenv/rvm/etc:</span>
gem install mrd
</code></pre>
<h2>Running it</h2>
<p>If you use <code>rvm</code>, <code>rbenv</code> or <code>chruby</code>, you will first need to create a wrapper script. I put mine in <code>~/.bin/launchd-mrd</code>:</p> <pre><code class="language-bash"><span class="hljs-meta" style="color: #999; font-weight: bold;">#!/bin/bash</span>
<span class="hljs-built_in" style="color: #0086b3;">export</span> PATH=<span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-variable" style="color: #008080;">$PATH</span>:/usr/local/bin&quot;</span>
<span class="hljs-built_in" style="color: #0086b3;">source</span> /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/share/chruby/chruby.sh
chruby 2.1
mrd
</code></pre>
<p>Make the script executable:</p> <pre><code class="language-bash">chmod +x ~/.bin/launchd-mrd
</code></pre>
<p>Launch it:</p> <pre><code class="language-bash">~/.bin/launchd-mrd
<span class="hljs-comment" style="color: #998; font-style: italic;"># Created Ramdisk at /dev/disk4</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Formatted Ramdisk at /dev/disk4</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Mounted Ramdisk at /Volumes/MySQLRAMDisk</span>
<span class="hljs-comment" style="color: #998; font-style: italic;">#</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># [...]</span>
<span class="hljs-comment" style="color: #998; font-style: italic;">#</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Starting MySQL server</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># MySQL is now running.</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Configure you client to use the root user, no password, and the socket at &apos;/Volumes/MySQLRAMDisk/mysql.sock&apos;.</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Just close this terminal or press ^C when you no longer need it.</span>
</code></pre>
<p>Great! It works perfectly. Let&apos;s now create a daemon to automatically start it on boot.</p>
<h2>Starting it automatically</h2>
<p>Create the startup script:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">echo</span> <span class="hljs-string" style="color: #d14;">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC \&quot;-//Apple//DTD PLIST 1.0//EN\&quot; \&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd\&quot;&gt;
&lt;plist version=\&quot;1.0\&quot;&gt;
  &lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd&lt;/string&gt;
    &lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;<span class="hljs-variable" style="color: #008080;">$HOME</span>/.bin/launchd-mrd&lt;/string&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd.err&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd.out&lt;/string&gt;
  &lt;/dict&gt;
&lt;/plist&gt;
&quot;</span> &gt; ~/Library/LaunchAgents/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd.plist
</code></pre>
<p>Launch it:</p> <pre><code>launchctl load ~/Library/LaunchAgents/com.$USER.mrd.plist
</code></pre>
<p>You should see the new shiny icon right on your desktop:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140316-mrd.png">
    <img src="http://asciithoughts.com/images/20140316-mrd.png" alt="MySQL RAM disk screenshot">
  </a>
</p>
<p>If you run into any trouble, look at the logs:</p> <pre><code class="language-bash">/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd.out
/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd.err
</code></pre>
<h2>Configuring Rails</h2>
<p>You can now open your <code>config/database.yml</code> and update the <code>test</code> section:</p> <pre><code class="language-yaml"><span class="hljs-attr">test:</span>
<span class="hljs-attr">  adapter:</span> mysql2
<span class="hljs-attr">  encoding:</span> utf8
<span class="hljs-attr">  collation:</span> utf8_unicode_ci
<span class="hljs-attr">  host:</span> localhost
<span class="hljs-attr">  database:</span> NAME
<span class="hljs-attr">  username:</span> root
<span class="hljs-attr">  password:</span> 
<span class="hljs-attr">  socket:</span> /Volumes/MySQLRAMDisk/mysql.sock
</code></pre>
<p>Run the tests:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">cd</span> ~/project
RAILS_ENV=<span class="hljs-built_in" style="color: #0086b3;">test</span> bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake db:create
bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake db:<span class="hljs-built_in" style="color: #0086b3;">test</span>:prepare
bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake <span class="hljs-built_in" style="color: #0086b3;">test</span>
..............................................................
..............................................................
...
</code></pre>
<p>Yeah \o/ This works!</p>
<h2>Automating the database creation</h2>
<p>Wouldn&apos;t it be better if we could automate the database creation/preparation as well? After all, you might have multiple projects, and you don&apos;t want to have to recreate the test database each time, load the schema, etc... So let&apos;s automate all that.</p>
<p>Create a new script <code>~/.bin/prepare-test-dbs</code>:</p> <pre><code class="language-bash"><span class="hljs-meta" style="color: #999; font-weight: bold;">#!/bin/bash
</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># Load your preferred Ruby Version Manager</span>
<span class="hljs-built_in" style="color: #0086b3;">export</span> PATH=<span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-variable" style="color: #008080;">$PATH</span>:/usr/local/bin&quot;</span>
<span class="hljs-built_in" style="color: #0086b3;">source</span> /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/share/chruby/chruby.sh
<span class="hljs-built_in" style="color: #0086b3;">source</span> /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/share/chruby/auto.sh

<span class="hljs-comment" style="color: #998; font-style: italic;"># Wait until &apos;mrd&apos; is actually loaded, and MySQL running</span>
until [[ -S /Volumes/MySQLRAMDisk/mysql.sock ]]; <span class="hljs-keyword" style="color: #333; font-weight: bold;">do</span>
  sleep 1
<span class="hljs-keyword" style="color: #333; font-weight: bold;">done</span>

<span class="hljs-comment" style="color: #998; font-style: italic;"># Go through each project, and:</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - create the database</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - load the schema</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - clear logs/*</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - clear tmp/*</span>
<span class="hljs-keyword" style="color: #333; font-weight: bold;">for</span> dir <span class="hljs-keyword" style="color: #333; font-weight: bold;">in</span> ~/projects/project1 \
           ~/projects/project2 \
           ~/projects/projectN; <span class="hljs-keyword" style="color: #333; font-weight: bold;">do</span>
  <span class="hljs-built_in" style="color: #0086b3;">cd</span> <span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-variable" style="color: #008080;">$dir</span>&quot;</span>
  RAILS_ENV=<span class="hljs-built_in" style="color: #0086b3;">test</span> bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake db:create
  bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake db:<span class="hljs-built_in" style="color: #0086b3;">test</span>:prepare
  bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake <span class="hljs-built_in" style="color: #0086b3;">log</span>:clear
  bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake tmp:clear
<span class="hljs-keyword" style="color: #333; font-weight: bold;">done</span>
</code></pre>
<p>Make it executable:</p> <pre><code class="language-bash">chmod +x ~/.bin/prepare-test-dbs
</code></pre>
<p>And create the startup script:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">echo</span> <span class="hljs-string" style="color: #d14;">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC \&quot;-//Apple//DTD PLIST 1.0//EN\&quot; \&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd\&quot;&gt;
&lt;plist version=\&quot;1.0\&quot;&gt;
  &lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd-prepare&lt;/string&gt;
    &lt;key&gt;Program&lt;/key&gt;
    &lt;string&gt;<span class="hljs-variable" style="color: #008080;">$HOME</span>/.bin/prepare-test-dbs&lt;/string&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd-prepare.err&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd-prepare.out&lt;/string&gt;
  &lt;/dict&gt;
&lt;/plist&gt;
&quot;</span> &gt; ~/Library/LaunchAgents/com.<span class="hljs-variable" style="color: #008080;">$USER</span>.mrd-prepare.plist
</code></pre>
<p>Now, each time your start your computer:</p>
<ul>
  <li>The RAM disk will be created</li>
  <li><code>mrd</code> will launch a MySQL instance</li>
  <li>The script will prepare a test DB for each of your projects</li>
</ul>
<p>Run the tests:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">cd</span> ~/projects/project1
bundle <span class="hljs-built_in" style="color: #0086b3;">exec</span> rake <span class="hljs-built_in" style="color: #0086b3;">test</span>
..............................................................
..............................................................
...
</code></pre>
<p>You&apos;re all set!</p>
<p>That&apos;s it for today. Cheers ;)</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/03/16/speedup-your-tests-and-save-your-ssd-with-mrd/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/03/16/speedup-your-tests-and-save-your-ssd-with-mrd/</guid>
            <pubDate>Sun, 16 Mar 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Preview all your development emails with Mailcatcher]]></title>
            <description><![CDATA[<h1>Preview all your development emails with Mailcatcher</h1>
<p>One of the great features coming in Rails 4.1 is
  <a href="http://edgeguides.rubyonrails.org/4_1_release_notes.html#action-mailer-previews">Action Mailer Previews</a>. This will offer a quick and simple way to preview the emails of your application as your develop them.</p>
<p>Unfortunately, not everyone will be able to jump to Rails 4.1 right away, and not everyone uses Rails. Your application might be written in PHP, Java, .Net, etc. Wouldn&apos;t it be great if you could easily preview all your emails without having to use Gmail as your SMTP?</p>
<p>Well rejoice my friend: here comes
  <a href="http://mailcatcher.me/">mailcatcher</a>!</p>
<h2>What is mailcatcher?</h2>
<p>The premise of mailcatcher is really simple:
  <strong>it runs a SMTP server on your machine</strong>, on port <code>1025</code>, and saves all the emails it receives. It also comes with a web interface where you can inspect said emails:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140309-mailcatcher.png">
    <img src="http://asciithoughts.com/images/20140309-mailcatcher.png" alt="Mailcatcher screenshot">
  </a>
</p>
<h2>Why is it awesome?</h2>
<ul>
  <li>
    <p>
      <strong>It catches all emails</strong> (but doesn&apos;t actually send any). This means that you can not only verify that each email looks ok, but also that the right number of emails was sent, to the right people. This also means that you can use real data, like an existing customer list, without fear of spamming those users.</p>
  </li>
  <li>
    <p>
      <strong>It is compatible</strong> with pretty much every language/framework. If your application sends emails, chances are it supports SMTP. So this provides you with a one-stop solution for all your email needs.</p>
  </li>
  <li>
    <p>
      <strong>It gives you the relevant information</strong>: HTML, plain text, source. Quick and easy.</p>
  </li>
</ul>
<h2>Installation</h2>
<p>The
  <a href="http://mailcatcher.me/">mailcatcher site</a> provides a lot more details but the gist of it is really simple. If you have a reasonably recent version of Linux or Mac OS X, you probably have Ruby installed. If not, you will have to
  <a href="https://www.ruby-lang.org/en/installation/">install Ruby first</a>.</p>
<p>Once you have Ruby installed:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># If you are using the system&apos;s Ruby:</span>
sudo gem install mailcatcher

<span class="hljs-comment" style="color: #998; font-style: italic;"># If you use rbenv/rvm/etc:</span>
gem install mailcatcher
</code></pre>
<p>You can then run mailcatcher with:</p> <pre><code class="language-bash">mailcatcher     <span class="hljs-comment" style="color: #998; font-style: italic;"># As a daemon</span>
mailcatcher <span class="hljs-_">-f</span>  <span class="hljs-comment" style="color: #998; font-style: italic;"># In the foreground</span>
</code></pre>
<p>If <code>mailcatcher</code> is not in your path, look at your ruby environment:</p> <pre><code class="language-bash">gem environment
<span class="hljs-comment" style="color: #998; font-style: italic;"># RubyGems Environment:</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - RUBYGEMS VERSION: 2.2.2</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - RUBY VERSION: 2.1.1 (2014-02-24 patchlevel 76) [x86_64-darwin13.0]</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - INSTALLATION DIRECTORY: /Users/Julien/.gem/ruby/2.1.1</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - RUBY EXECUTABLE: /Users/Julien/.rubies/2.1.1/bin/ruby</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - EXECUTABLE DIRECTORY: /Users/Julien/.gem/ruby/2.1.1/bin</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># - SPEC CACHE DIRECTORY: /Users/Julien/.gem/specs</span>
</code></pre>
<p>Look in the <code>EXECUTABLE DIRECTORY</code>:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># It should be here</span>
/Users/Julien/.rubies/2.1.1/bin/mailcatcher
</code></pre>
<h2>Configuring Rails</h2> <pre><code class="language-ruby"><span class="hljs-comment" style="color: #998; font-style: italic;"># In config/environments/development.rb</span>
config.action_mailer.delivery_method = <span class="hljs-symbol" style="color: #990073;">:smtp</span>
config.action_mailer.smtp_settings = { 
  <span class="hljs-symbol" style="color: #990073;">:address</span> =&gt; <span class="hljs-string" style="color: #d14;">&quot;localhost&quot;</span>, <span class="hljs-symbol" style="color: #990073;">:port</span> =&gt; <span class="hljs-number" style="color: #008080;">1025</span> 
}
</code></pre>
<p>The
  <a href="http://mailcatcher.me/">mailcatcher site</a> provides information for other platforms.</p>
<h2>Starting it automatically</h2>
<p>You will probably want to automatically start mailcatcher when you start your computer. It&apos;s pretty easy on Mac OS X:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># Path to mailcatcher. You might want to try </span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># &apos;which mailcatcher&apos; in your terminal first</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># to make sure it returns a result.</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># If it doesn&apos;t, and you know the path to the</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># executable, just set it here.</span>
MAILCATCHER_PATH=`<span class="hljs-built_in" style="color: #0086b3;">which</span> mailcatcher`

<span class="hljs-comment" style="color: #998; font-style: italic;"># Create the daemon file</span>
<span class="hljs-built_in" style="color: #0086b3;">echo</span> <span class="hljs-string" style="color: #d14;">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;
&lt;!DOCTYPE plist PUBLIC \&quot;-//Apple//DTD PLIST 1.0//EN\&quot; \&quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd\&quot;&gt;
&lt;plist version=\&quot;1.0\&quot;&gt;
  &lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.mailcatcher&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
      &lt;string&gt;<span class="hljs-variable" style="color: #008080;">$MAILCATCHER_PATH</span>&lt;/string&gt;
      &lt;string&gt;-f&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;KeepAlive&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.mailcatcher.err&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.mailcatcher.out&lt;/string&gt;
  &lt;/dict&gt;
&lt;/plist&gt;
&quot;</span> &gt; ~/Library/LaunchAgents/com.mailcatcher.plist
</code></pre>
<p>You can then start it for the current session with (make sure it&apos;s not already running):</p> <pre><code>launchctl load ~/Library/LaunchAgents/com.mailcatcher.plist
</code></pre>
<p>If you run into any trouble, look at the logs:</p> <pre><code>/tmp/com.mailcatcher.out
/tmp/com.mailcatcher.err
</code></pre>
<h2>Caveat</h2>
<p>If you use <code>rbenv</code>, <code>rvm</code>, etc., you might need a wrapper script for the environment:</p> <pre><code class="language-bash"><span class="hljs-meta" style="color: #999; font-weight: bold;">#!/bin/bash</span>
<span class="hljs-built_in" style="color: #0086b3;">export</span> PATH=<span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-variable" style="color: #008080;">$PATH</span>:/usr/local/bin&quot;</span>
<span class="hljs-built_in" style="color: #0086b3;">source</span> /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/share/chruby/chruby.sh
chruby 2.1
mailcatcher <span class="hljs-_">-f</span>
</code></pre>
<h2>Conclusion</h2>
<p>Mailcatcher is absolutely awesome and I don&apos;t know what I would do without it.</p>
<p>That&apos;s it for today. Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/03/09/preview-all-your-emails-with-mailcatcher/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/03/09/preview-all-your-emails-with-mailcatcher/</guid>
            <pubDate>Sun, 09 Mar 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Animated GIFs with LICEcap, Preview and ImageMagick]]></title>
            <description><![CDATA[<h1>Animated GIFs with LICEcap, Preview and ImageMagick</h1>
<p>Animated GIFs have seen a resurgence over the last few years. It&apos;s easy to see why...</p>
<p>
  <img src="http://media.giphy.com/media/AfbJToCgg40HS/giphy.gif" alt="Cute kitten and puppy">
</p>
<p>While I&apos;m grateful for puppies and kittens, the reason I like animated GIFs, and I think the reason they won over the web, is because GIF is a universal format. Point anyone to a URL, and no matter their platform/device, it&apos;s pretty much guaranteed they can see it.</p>
<h2>Screencasts</h2>
<p>Because GIF is such a universal format, it makes sense to use it for short screencasts.
  <a href="http://www.cockos.com/licecap/">LICEcap</a> is a tiny utility for Mac and Windows that allows you to quickly select a section of your screen and record it directly to a GIF file:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140302-licecap.gif">
    <img src="http://asciithoughts.com/images/20140302-licecap.gif" alt="LICEcap recording">
  </a>
</p>
<h2>Editing</h2>
<p>While LICEcap makes it super easy to quickly record GIFs, it lacks any editing capabilities. Sometimes, all you need to make your GIF perfect is remove a few frames, for errors, typos, dead time, etc. Keeping the smallest number of frames possible also helps with size.</p>
<p>GIF being a standard format, you can open it in Preview to edit the frames. Simply click on a frame and press delete to delete it. When you&apos;re done, you can save the file.</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140302-preview.png">
    <img src="http://asciithoughts.com/images/20140302-preview.png" alt="Editing in preview">
  </a>
</p>
<p>Another alternative, if the GIF gets really big, is to use ImageMagick:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># Generate individual frames</span>
convert -coalesce input.gif frames.png

<span class="hljs-comment" style="color: #998; font-style: italic;"># Do some editing, and then put the frames back together</span>
convert frames*.png output.gif
</code></pre>
<h2>Framerate</h2>
<p>Your GIF is almost ready. Since your removed a few frames, and sticked to the essential, it&apos;s possible it&apos;s now a bit too fast. So let&apos;s reduce the framerate with
  <a href="http://www.imagemagick.org/">ImageMagick</a>:</p> <pre><code class="language-bash">convert     \
  -loop 0   \  <span class="hljs-comment" style="color: #998; font-style: italic;"># number of loops. 0 is infinite</span>
  -delay 20 \  <span class="hljs-comment" style="color: #998; font-style: italic;"># ticks between frames. 1 tick is 10ms, so 5FPS</span>
  input.gif \
  output.gif
</code></pre>
<h2>Optimizing</h2>
<p>Finally, we can use
  <a href="http://imageoptim.com/">ImageOptim</a> to optimize the file size:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140302-imageoptim.png">
    <img src="http://asciithoughts.com/images/20140302-imageoptim.png" alt="Optimization">
  </a>
</p>
<p>And you&apos;re done! Your screencast is ready to be shared with the world.</p>
<h2>Conclusion</h2>
<p>While you won&apos;t be producing hours of videos with LICEcap, it&apos;s definitely a useful tool for one off screencasts and other small demos. In association with Preview, ImageMagick and ImageOptim, it makes for a fast and powerful workflow.</p>
<p>Other alternatives and tools:</p>
<ul>
  <li>
    <a href="http://gifbrewery.com/">GIF Brewery</a>
  </li>
  <li>
    <a href="http://recordit.co/">Recordit</a>
  </li>
  <li>
    <a href="http://www.screenr.com/">Screenr</a>
  </li>
</ul>
<p>That&apos;s it for today. Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/03/02/animated-gifs-with-licecap-preview-and-imagemagick/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/03/02/animated-gifs-with-licecap-preview-and-imagemagick/</guid>
            <pubDate>Sun, 02 Mar 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Setting up a wildcard DNS domain on Mac OS X]]></title>
            <description><![CDATA[<h1>Setting up a wildcard DNS domain on Mac OS X</h1>
<p>There are many ways to develop on a Mac, and many stack to choose from. One common and recurring need however is to access your local websites through a named domain, ie using <code>example.dev</code>. Thankfully, there is a really simple way to do this using
  <a href="http://www.thekelleys.org.uk/dnsmasq/doc.html">Dnsmasq</a> as a local resolver.</p>
<h2>Install Dnsmasq</h2>
<p>This is straightforward with
  <a href="http://brew.sh/">Homebrew</a>:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># Install it</span>
brew install dnsmasq

<span class="hljs-comment" style="color: #998; font-style: italic;"># Create the etc dir if needed</span>
mkdir -p /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/etc

<span class="hljs-comment" style="color: #998; font-style: italic;"># Create a simple configuration</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># This forces the .dev domain to respond with 127.0.0.1</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># You can find more information in the default config file:</span>
<span class="hljs-comment" style="color: #998; font-style: italic;">#   /usr/local/opt/dnsmasq/dnsmasq.conf.example</span>
<span class="hljs-built_in" style="color: #0086b3;">echo</span> <span class="hljs-string" style="color: #d14;">&quot;address=/.dev/127.0.0.1&quot;</span> &gt; /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/etc/dnsmasq.conf

<span class="hljs-comment" style="color: #998; font-style: italic;"># Install the daemon startup file</span>
sudo cp -fv /usr/<span class="hljs-built_in" style="color: #0086b3;">local</span>/opt/dnsmasq/*.plist \
  /Library/LaunchDaemons

<span class="hljs-comment" style="color: #998; font-style: italic;"># Start the daemon</span>
sudo launchctl load \
  /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
</code></pre>
<h2>Configure Mac OS X</h2>
<p>All we need to do is tell the resolver to use Dnsmasq for <code>.dev</code> domains:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># man 5 resolver</span>
sudo mkdir -p /etc/resolver
sudo sh -c <span class="hljs-string" style="color: #d14;">&apos;echo &quot;nameserver 127.0.0.1&quot; &gt; /etc/resolver/dev&apos;</span>
</code></pre>
<p>You can now use any <code>.dev</code> domain and it will always resolve to <code>127.0.0.1</code>:</p> <pre><code class="language-bash">ping google.com   <span class="hljs-comment" style="color: #998; font-style: italic;"># this still works</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># PING google.com (74.125.239.38): 56 data bytes</span>

ping foo.dev      <span class="hljs-comment" style="color: #998; font-style: italic;"># you can use any domain</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># PING foo.dev (127.0.0.1): 56 data bytes</span>

ping bar.baz.dev  <span class="hljs-comment" style="color: #998; font-style: italic;"># or subdomain</span>
<span class="hljs-comment" style="color: #998; font-style: italic;"># PING foo.dev (127.0.0.1): 56 data bytes</span>
</code></pre>
<h2>Conclusion</h2>
<p>This is very useful in particular for applications that use subdomains as account identifier: you can easily create new accounts on the fly, and never have to worry about your <code>/etc/hosts</code> file again.</p>
<p>Finally, you may also want to look at
  <a href="http://pow.cx">Pow</a> (and
  <a href="https://github.com/rodreegez/powder">powder</a>). Pow will automatically start your Rails/Rack applications, and provides a
  <a href="http://pow.cx/manual.html#section_2.1.4">port proxying feature</a> for apps written in other languages. Definitely a great alternative.</p>
<p>That&apos;s it for today.</p>
<p>Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/02/23/setting-up-a-wildcard-dns-domain-on-mac-os-x/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/02/23/setting-up-a-wildcard-dns-domain-on-mac-os-x/</guid>
            <pubDate>Sun, 23 Feb 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Using Little Snitch to save bandwidth]]></title>
            <description><![CDATA[<h1>Using Little Snitch to save bandwidth</h1>
<p>In our last article, we saw how to use Little Snitch to stay secure by forcing the use of a VPN at unsafe locations. Let&apos;s go a little further and see how we can save on bandwidth and improve the quality of our connection.</p>
<h2>Why save bandwidth?</h2>
<p>I started thinking about restricting internet access to a few applications for 2 reasons:</p>
<ul>
  <li>
    <p>Free Wifi at crowded places can be less than stellar. When you can&apos;t SSH to a remote server because iTunes is downloading your podcasts, and Mac OS X checking for updates, you have a problem.</p>
  </li>
  <li>
    <p>I have a hotspot from
      <a href="http://www.freedompop.com/">FreedomPop</a>, and I get 500MB of free data every month. This may not be enough to watch Netflix, but it&apos;s plenty when you&apos;re on call and all you need is a browser, and a terminal. Why pay extra for data when you don&apos;t need to?</p>
  </li>
</ul>
<p>Thankfully, Little Snitch offers a simple and elegant solution.</p>
<h2>Creating a profile</h2>
<p>Let&apos;s start by creating a new profile. We&apos;ll call it &quot;Low Bandwidth&quot;:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-22.png">
    <img src="http://asciithoughts.com/images/20140215-22.png" alt="Creating a new profile">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-23.png">
    <img src="http://asciithoughts.com/images/20140215-23.png" alt="Creating a new profile">
  </a>
</p>
<p>Now let&apos;s connect to our hotspot:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-24.png">
    <img src="http://asciithoughts.com/images/20140215-24.png" alt="Profile selection dialog">
  </a>
</p>
<h2>Building the rules</h2>
<p>If you followed the previous article, you should have Little Snitch in &quot;Silent Mode: Deny&quot;. This makes it really easy to quickly enable the 2 or 3 applications we care about when on the go.</p>
<p>Open the Rules screen:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-25.png">
    <img src="http://asciithoughts.com/images/20140215-25.png" alt="Profile selection dialog">
  </a>
</p>
<p>Then, go to &quot;Silent Mode Connections&quot; and allow Chrome, Safari and Terminal:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-26.png">
    <img src="http://asciithoughts.com/images/20140215-26.png" alt="Profile selection dialog">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-27.png">
    <img src="http://asciithoughts.com/images/20140215-27.png" alt="Profile selection dialog">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-28.png">
    <img src="http://asciithoughts.com/images/20140215-28.png" alt="Profile selection dialog">
  </a>
</p>
<p>Since you&apos;re doing this while on the Low Bandwidth profile, the new rules are automatically associated with it:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-29.png">
    <img src="http://asciithoughts.com/images/20140215-29.png" alt="Profile selection dialog">
  </a>
</p>
<p>And there you have it! Safari now works:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-30.png">
    <img src="http://asciithoughts.com/images/20140215-30.png" alt="Profile selection dialog">
  </a>
</p>
<p>But the App Store won&apos;t start updating all your applications behind your back:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-31.png">
    <img src="http://asciithoughts.com/images/20140215-31.png" alt="Profile selection dialog">
  </a>
</p>
<h2>Conclusion</h2>
<p>This works really well and has allowed me to enjoy my browsing experience on questionable networks. It has also allowed me to stay within my free 500MB while using my hotspot regularly.</p>
<p>Also, if you&apos;re keen on saving bandwidth, I highly recommend
  <a href="https://itunes.apple.com/us/app/bandwidth+/id490461369">Bandwidth+</a>. It&apos;s a simple little app that sits in your menu bar and monitor your bandwidth usage. Priceless.</p>
<p>I hope these 2 articles were helpful and gave you a better understanding of the power of
  <a href="http://www.obdev.at/products/littlesnitch/">Little Snitch</a>. It&apos;s a great tool, simple to use and configure, and absolutely worth the money. Definitely a must have for anyone working remotely.</p>
<p>That&apos;s it for today.</p>
<p>Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/02/16/using-little-snitch-to-save-bandwidth/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/02/16/using-little-snitch-to-save-bandwidth/</guid>
            <pubDate>Sun, 16 Feb 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Using Little Snitch to prevent internet access without VPN]]></title>
            <description><![CDATA[<h1>Using Little Snitch to prevent internet access without VPN</h1>
<p>There are a couple reasons why you would want to access the internet through a VPN: your work requires it, you regularly work from open Wifi networks, at hotels, coffee shops or libraries, and you want to prevent eavesdropping.</p>
<p>This article presents a simple technique that allows you to force internet access through a VPN when using unsafe networks. I will use
  <a href="https://www.privateinternetaccess.com/">Private Internet Access</a> as an example, but this applies to any VPN connection, on any network.</p>
<h2>TL;DR</h2>
<ul>
  <li>Modify rules in default profile to deny all connections.</li>
  <li>Create a &quot;VPN&quot; profile that allows all connections.</li>
  <li>Let &quot;Automatic Profile Switching&quot; do the rest.</li>
</ul>
<h2>Little Snitch</h2>
<p>
  <a href="http://www.obdev.at/products/littlesnitch/">Little Snitch</a> is a firewall that allows you to control connections
  <strong>from</strong> your computer
  <strong>to</strong> the internet. One of its greatest features, introduced in version 3, is &quot;Automatic Profile Switching&quot;: the ability to automatically apply different rules depending on which network you&apos;re connected to.</p>
<p>We are going to use this feature to provide unrestricted access to the internet when connected to a VPN, and automatically cut off access as soon as we are disconnected (or before we are connected).</p>
<h2>Step 1: Install Little Snitch</h2>
<p>Go ahead and
  <a href="http://www.obdev.at/products/littlesnitch/download.html">install Little Snitch</a>.</p>
<h2>Step 2: Stop the filter and switch to silent mode</h2>
<p>Little Snitch has a tendency to be a bit verbose, and will pester you with questions as soon as any application attempts a connection, which can rapidly get annoying.</p>
<p>Fortunately, there is a &quot;Silent Mode&quot;, which will automatically allow/deny any connection, and offer us some peace while we work on the configuration. So:</p>
<ul>
  <li>Stop the network filter</li>
  <li>Set &quot;Silent Mode&quot; to &quot;Deny&quot;</li>
</ul>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-01.png">
    <img src="http://asciithoughts.com/images/20140215-01.png" alt="Switching to silent mode">
  </a>
</p>
<h2>Step 3: Delete all default rules</h2>
<p>Little Snitch comes with a couple of default rules. They are mostly harmful, but if you are worried about your privacy, it can&apos;t hurt to be cautious. So let&apos;s start from an empty environment.</p>
<p>Open the &quot;Rules&quot; screen:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-02.png">
    <img src="http://asciithoughts.com/images/20140215-02.png" alt="Open the rules panel">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-03.png">
    <img src="http://asciithoughts.com/images/20140215-03.png" alt="Open the rules panel">
  </a>
</p>
<p>Delete or disable all the rules. You may get a few warnings, but just go ahead and do it anyway (you can always restore the factory defaults later).</p>
<p>I only keep 3 main rules:</p>
<ul>
  <li>DNS</li>
  <li>Outgoing connections to local network</li>
  <li>Incoming connections (though it&apos;s safe to disable those as well)</li>
</ul>
<p>When you are done, your rules should look like this:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-04.png">
    <img src="http://asciithoughts.com/images/20140215-04.png" alt="Rules panel">
  </a>
</p>
<p>Let&apos;s simplify the view a bit by hiding disabled rules:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-05.png">
    <img src="http://asciithoughts.com/images/20140215-05.png" alt="Hiding disabled rules">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-06.png">
    <img src="http://asciithoughts.com/images/20140215-06.png" alt="Hiding disabled rules">
  </a>
</p>
<h2>Step 4: Create a new profile</h2>
<p>First, we need to enable &quot;Automatic Profile Switching&quot;:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-07.png">
    <img src="http://asciithoughts.com/images/20140215-07.png" alt="Enable Automatic Profile Switching">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-08.png">
    <img src="http://asciithoughts.com/images/20140215-08.png" alt="Enable Automatic Profile Switching">
  </a>
</p>
<p>Now, let&apos;s create our
  <strong>VPN</strong> profile:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-09.png">
    <img src="http://asciithoughts.com/images/20140215-09.png" alt="Create the VPN profile">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-10.png">
    <img src="http://asciithoughts.com/images/20140215-10.png" alt="Create the VPN profile">
  </a>
</p>
<p>Finally, we restart the network filter:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-11.png">
    <img src="http://asciithoughts.com/images/20140215-11.png" alt="Start network filter">
  </a>
</p>
<p>Turn Wifi on and off:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-12.png">
    <img src="http://asciithoughts.com/images/20140215-12.png" alt="Restart Wifi">
  </a>
</p>
<p>And now Little Snitch wants you to choose a profile. If this your home connection, you could choose the newly created VPN profile. If you are at an unsafe location, or if you simply prefer to have VPN activated at all times, select &quot;Deactivate Active Profile&quot;:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-13.png">
    <img src="http://asciithoughts.com/images/20140215-13.png" alt="Deactivate Active Profile">
  </a>
</p>
<p>Since the default rules do not explicitly allow any connection, and we have set &quot;Silent Mode&quot; to &quot;Deny&quot;, we basically lost internet access:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-14.png">
    <img src="http://asciithoughts.com/images/20140215-14.png" alt="No internet access">
  </a>
</p>
<p>That&apos;s what we wanted :) Let&apos;s now configure the VPN.</p>
<h2>Step 5: Creating new rules</h2>
<p>We succeeded in stopping access for all applications, but the truth is: the VPN itself needs access. So we need to create a few rules for that.</p>
<p>
  <strong>Try</strong> to start the connection:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-15.png">
    <img src="http://asciithoughts.com/images/20140215-15.png" alt="Starting the VPN">
  </a>
</p>
<p>At this time, the VPN won&apos;t be able to connect, but since we activated Silent Mode, the connection attempts will appear in Little Snitch and we can create new rules:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-16.png">
    <img src="http://asciithoughts.com/images/20140215-16.png" alt="Creating rules for the VPN">
  </a>
</p>
<p>As soon as the rules are created, the VPN connection will succeed and you will be prompted with the familiar dialog. Choose &quot;VPN &amp; Safe Networks&quot;:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-17.png">
    <img src="http://asciithoughts.com/images/20140215-17.png" alt="Choose a profile">
  </a>
</p>
<p>Finally, now that the association has been made between the VPN network and the VPN profile, we need to restore access to all applications. Go back to the rules window, and click &quot;New&quot;. You will need to create 2 rules. One for all applications owned by you, and one for all system applications:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-18.png">
    <img src="http://asciithoughts.com/images/20140215-18.png" alt="Creating the new rules">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-19.png">
    <img src="http://asciithoughts.com/images/20140215-19.png" alt="Creating the new rules">
  </a>
  <a class="img" href="http://asciithoughts.com/images/20140215-20.png">
    <img src="http://asciithoughts.com/images/20140215-20.png" alt="Creating the new rules">
  </a>
</p>
<p>If all goes well, you now have full internet access:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20140215-21.png">
    <img src="http://asciithoughts.com/images/20140215-21.png" alt="Safari on apple.com">
  </a>
</p>
<h2>Caveats</h2>
<ul>
  <li>
    <p>I left access open for DNS. Depending on your VPN configuration and your needs, you may want to restrict that as well, and just allow access to the IP of your VPN server. It&apos;s up to you.</p>
  </li>
  <li>
    <p>The rules required to make your VPN work will of course depend on your VPN. It is usually pretty simple to figure out once you look at the connection attempts, but you may have to play with it a bit until you find the right set. You can double click any rule to adjust its parameters (expand it, restrict it).</p>
  </li>
  <li>
    <p>Some hotels/hotspots require that you accept their terms and conditions before getting access, in which case your VPN won&apos;t be able to connect. Because of this, I have a special rule to always allow access for Safari. I don&apos;t use it as a browser normally, but if the VPN fails and I&apos;m on a free network, I just open it and try apple.com. If there is an authentication phase, I&apos;ll get redirected. Once I&apos;ve accepted the terms and conditions, I&apos;ll get the full access and the VPN will be able to connect normally.</p>
  </li>
</ul>
<h2>Conclusion</h2>
<p>That&apos;s it! You&apos;re pretty much set :) Now, every time you connect to a new network, Little Snitch will ask you to choose a profile and you can either choose the safe one (for work and home), or deactivate the current profile and launch your VPN (for coffee shops, etc).</p>
<p>I hope this was helpful. Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2014/02/15/using-little-snitch-to-prevent-internet-access-without-vpn/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2014/02/15/using-little-snitch-to-prevent-internet-access-without-vpn/</guid>
            <pubDate>Sat, 15 Feb 2014 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Managing Vim Plugins with Vundle]]></title>
            <description><![CDATA[<h1>Managing Vim Plugins with Vundle</h1>
<p>If you are a heavy user of vim, chances are that over time you added a bunch of plugins, syntax files and other goodies to your vim configuration. This makes your editing experience smoother, but if you installed all these scripts manually, maintaining them can be a pain. Keeping up-to-date with the latest versions, or simply removing one you don&apos;t use anymore becomes difficult as files from the different scripts get mixed up in your <code>.vim</code> directory.</p>
<p>
  <a href="http://www.vim.org/scripts/script.php?script_id=1502">Vimballs</a> and
  <a href="http://www.vim.org/scripts/script.php?script_id=2332">pathogen</a> can both ease the process, but you still have to download the scripts manually, and take care of all the updates.</p>
<h2>Vundle</h2>
<p>
  <a href="https://github.com/gmarik/vundle">Vundle</a> is basically a package manager for vim plugins. It will search, install, update and delete plugins for your while maintaining a nice directory structure. To install it:</p> <pre><code>git clone git://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
</code></pre>
<p>Then, update your <code>.vimrc</code> with the following lines:</p> <pre><code>set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
</code></pre>
<p>You&apos;re good to go.</p>
<h2>Installing a plugin</h2>
<p>Let&apos;s say that you are want to install
  <a href="http://www.vim.org/scripts/script.php?script_id=294">Align</a>. You can do a search for it in vim:</p> <pre><code>:BundleSearch align
</code></pre>
<p>And you will be greeted by a new window listing all the search results:</p> <pre><code class="language-vim"><span class="hljs-comment" style="color: #998; font-style: italic;">&quot; Keymap: i - Install bundle; c - Cleanup; ...</span>
<span class="hljs-comment" style="color: #998; font-style: italic;">&quot; Search results for: align</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;right_align&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;align--Ahmad&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;Easy-alignment-to-column&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;Align.vim&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;AutoAlign&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;feralalign.vim&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;Align&apos;</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;Lineup--A-simple-text-aligner&apos;</span>
</code></pre>
<p>Press <code>i</code> to install a plugin, and then copy the relevant line to your <code>.vimrc</code> to have it loaded automatically when you launch vim:</p> <pre><code class="language-vim"><span class="hljs-comment" style="color: #998; font-style: italic;">&quot; Initialize Vundle</span>
<span class="hljs-keyword" style="color: #333; font-weight: bold;">set</span> rtp+=~/.<span class="hljs-keyword" style="color: #333; font-weight: bold;">vim</span>/bundle/vundle/
<span class="hljs-keyword" style="color: #333; font-weight: bold;">call</span> vundle#rc()

<span class="hljs-comment" style="color: #998; font-style: italic;">&quot; My plugins</span>
Bundle <span class="hljs-string" style="color: #d14;">&apos;Align&apos;</span>
</code></pre>
<p>Sometimes, the result of the search isn&apos;t very useful and you won&apos;t be sure which plugin to install. I find that an easier way is to go directly to the
  <a href="http://vim-scripts.org/vim/scripts.html">list of scripts</a> where you will find a short description and links to the plugins&apos; pages.</p>
<h2>Other commands</h2>
<p>Updating all your plugins:</p> <pre><code>:BundleInstall!
</code></pre>
<p>Removing unused plugins:</p> <pre><code>:BundleClean
</code></pre>
<h2>Where to find plugins?</h2>
<p>You can look on
  <a href="http://www.vim.org/">vim.org</a> for the list of all plugins, sorted by
  <a href="http://www.vim.org/scripts/script_search_results.php?keywords=&amp;script_type=&amp;order_by=rating&amp;direction=descending&amp;search=search">ratings</a> or
  <a href="http://www.vim.org/scripts/script_search_results.php?keywords=&amp;script_type=&amp;order_by=downloads&amp;direction=descending&amp;search=search">downloads</a>. This will give you an idea of what most people are using: chances are some of them will be useful to you too.</p>
<p>Beginners might also want to look at the
  <a href="https://github.com/carlhuda/janus">janus</a> distribution, which makes starting with vim easier. More advanced users will enjoy the
  <a href="https://github.com/thoughtbot/dotfiles">Thoughtbot dotfiles</a>.</p>
<p>That&apos;s it for today. Hope you enjoyed it.</p>
<p>Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2011/09/18/managing-vim-plugins-with-vundle/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2011/09/18/managing-vim-plugins-with-vundle/</guid>
            <pubDate>Sun, 18 Sep 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[Configuring your Bash Prompt (PS1)]]></title>
            <description><![CDATA[<h1>Configuring your Bash Prompt (PS1)</h1>
<p>As a developer, chances are you spend a lot of time every day in your terminal. Whether you use it for building, debugging, inspecting, deploying or administering, the shell is part of your life.</p>
<p>Since you spend such a large amount of time using it, it makes sense to get comfortable with it. You can treat your shell as a temporary mean to an end, an impersonal hotel room of sorts, functional but never yours, or you can make it your home, a warm and cozy place, with your things, your habits and your tools.</p>
<p>One way to achieve that goal, to make your shell your home, is to customize it. And what better place to start than the prompt? Most Linux distributions have a slightly customized prompt, designed to work in most situations. If you&apos;re lucky, it looks like this:</p> <pre><code class="language-bash">user@host $ ...
</code></pre>
<p>If you&apos;re not, it looks like this:</p> <pre><code class="language-bash">$ ...
</code></pre>
<p>Wouldn&apos;t it be better if you could have a little more information? Maybe the time of the day, the current directory, or just some colors? Thankfully you can, and it&apos;s easy.</p>
<h2>PS1</h2>
<p>The variable that contains the definition of the prompt format is <code>PS1</code>. You can experiment with it directly in your shell:</p> <pre><code class="language-bash">$ PS1=<span class="hljs-string" style="color: #d14;">&quot;% &quot;</span>
% PS1=<span class="hljs-string" style="color: #d14;">&quot;! &quot;</span>
! ...
</code></pre>
<p>Apart from using a simple string, you can also use special variables such as:</p> <pre><code>\n     newline
\r     carriage return

\j     number of jobs managed by the shell

\d     date in &quot;Weekday Month Date&quot;
\t     current time in 24-hour HH:MM:SS format
\T     current time in 12-hour HH:MM:SS format
\@     current time in 12-hour am/pm format
\A     current time in 24-hour HH:MM format

\H     hostname
\h     hostname up to the first &apos;.&apos;
\u     username of the current user
\w     current working directory
</code></pre>
<p>There are more available, which you can find in the man (search for PROMPTING). For example, in my shell, I use the following format:</p> <pre><code class="language-bash">$ PS1=<span class="hljs-string" style="color: #d14;">&quot;\u \h \t \w \\$ &quot;</span>
user hostname 20:46:05 ~/folder $
</code></pre>
<h2>Colors</h2>
<p>The shell has special escape codes for colors:</p> <pre><code class="language-bash">FG=COLOR_CODE
BG=COLOR_CODE

<span class="hljs-comment" style="color: #998; font-style: italic;"># &apos;\033[&apos; + FOREGROUND + &apos;\033[&apos; + BACKGROUND</span>
CODE=<span class="hljs-string" style="color: #d14;">&quot;\033[<span class="hljs-variable" style="color: #008080;">$FG</span>\033[<span class="hljs-variable" style="color: #008080;">$BG</span>&quot;</span>
</code></pre>
<p>The
  <a href="http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO">Bash Prompt HOWTO</a> provides a
  <a href="http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html">script</a> to quickly list all the colors:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110809-1.png">
    <img src="http://asciithoughts.com/images/20110809-1.png" alt="Execution of script colors.sh">
  </a>
</p>
<h2>Together</h2>
<p>So you can now create your own prompt, just the way you want it :) In my <code>~/.bashrc</code> I have:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># We need to encapsulate with &quot;\[ \]&quot; to use in a prompt</span>
BLUE1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;34m\]&apos;</span>
BLUE2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[34m\]&apos;</span>
GREEN1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;32m\]&apos;</span>
GREEN2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[32m\]&apos;</span>
CYAN1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;36m\]&apos;</span>
CYAN2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[36m\]&apos;</span>
RED1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;31m\]&apos;</span>
RED2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[31m\]&apos;</span>
PURPLE1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;35m\]&apos;</span>
PURPLE2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[35m\]&apos;</span>
BROWN1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;33m\]&apos;</span>
BROWN2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[33m\]&apos;</span>
GRIS1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;37m\]&apos;</span>
GRIS2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[37m\]&apos;</span>
YELLOW1=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;33m\]&apos;</span>
YELLOW2=<span class="hljs-string" style="color: #d14;">&apos;\[\033[33m\]&apos;</span>
WHITE=<span class="hljs-string" style="color: #d14;">&apos;\[\033[1;37m\]&apos;</span>
NOCOLOUR=<span class="hljs-string" style="color: #d14;">&apos;\[\033[0m\]&apos;</span>

<span class="hljs-comment" style="color: #998; font-style: italic;"># Don&apos;t forget to use double quotes &quot;</span>
PS1=<span class="hljs-string" style="color: #d14;">&quot;<span class="hljs-variable" style="color: #008080;">$RED1</span>\u <span class="hljs-variable" style="color: #008080;">$BLUE1</span>\h <span class="hljs-variable" style="color: #008080;">$PURPLE1</span>\t <span class="hljs-variable" style="color: #008080;">$GREEN2</span>\w <span class="hljs-variable" style="color: #008080;">$BROWN2</span>\\$ <span class="hljs-variable" style="color: #008080;">$NOCOLOUR</span>&quot;</span>
</code></pre>
<p>Which gives:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110809-2.png">
    <img src="http://asciithoughts.com/images/20110809-2.png" alt="My prompt">
  </a>
</p>
<h2>Going further</h2>
<p>This was just a simple customization, but you can do much more: you can run complex commands to create your prompt, using all the tools available in your system. If you want to know more, check out:</p>
<ul>
  <li>
    <a href="https://wiki.archlinux.org/index.php/Color_Bash_Prompt">Color Bash Prompt</a>
  </li>
  <li>
    <a href="http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/index.html">The Bash Prompt HOWTO</a>
  </li>
  <li>
    <a href="http://www.askapache.com/linux/bash-power-prompt.html">Crazy Powerful Bash Prompt</a>
  </li>
</ul>
<p>That&apos;s it for today. Hope you enjoyed it.</p>
<p>Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2011/08/09/configuring-your-bash-prompt-ps1/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2011/08/09/configuring-your-bash-prompt-ps1/</guid>
            <pubDate>Tue, 09 Aug 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[SSH Remote Tunneling]]></title>
            <description><![CDATA[<h1>SSH Remote Tunneling</h1>
<p>Last time we introduced local port forwarding with SSH. Today let&apos;s look at related features:
  <em>remote port forwarding</em> and
  <em>socks proxying</em>.</p>
<h2>Remote port forwarding</h2>
<p>Remote port forwarding works exactly like local forwarding, but the other way around: while
  <strong>local forwarding</strong> transfers local connections to the remote machine,
  <strong>remote forwarding</strong> transfers remote connections to you (or another machine you have access to).</p>
<p>When is this useful? Well most of the time, your network setup is such that you can access the server through SSH, because it has a public IP address, but the server can&apos;t access
  <strong>you</strong> because you are behind a firewall or a router.</p>
<p>So an example use case would be
  <strong>remote debugging</strong> an application. A debugger is installed on a remote development server, but the debugger can&apos;t reach you, and therefore your IDE/editor of choice. You thus setup things as follow:</p>
<ul>
  <li>Set the debugger on the server to connect to localhost (default)</li>
  <li>Forward the remote port (9000) to your machine (same port)</li>
  <li>Set your IDE on
    <em>listen</em> mode, ie listening for debugger connections</li>
  <li>Start a debugging session</li>
</ul>
<p>The SSH command is :</p> <pre><code>ssh -R 9000:127.0.0.1:9000 example.com
</code></pre>
<p>And the scenario:</p> <pre><code>http://example.com?START_DEBUG=1 ----+
                                     |
+-- SSH &lt;- server:9000 &lt;- debugger &lt;-+
|
+-&gt; SSH -&gt; local:9000 -&gt; IDE
</code></pre>
<p>Apart from the direction of the forwarding, it works exactly like local forward, and thus offers the same options: you bind to <code>localhost</code>, an external address, or all interfaces, and you can forward to a local port, or to another machine you have access to.</p>
<p>In a complex scenario, you could have something like this: a remote server B talks to a webservice on server A (also remote), but A doesn&apos;t have a webservice. The webservice is on
  <strong>your</strong> network, so you use your machine and machine A as bridges to make everybody communicate:</p> <pre><code>webservice &lt;---------&gt; you &lt;----&gt; A &lt;---------&gt; B
            network 1       SSH      network 2
</code></pre>
<p>with:</p> <pre><code>ssh -R 9000:webservice:9000 machine-a
</code></pre>
<p>That&apos;s it for local and remote port forwarding. Sometimes however, you need a little more than one or two ports, and you need to redirect a large part of your connections through a 3rd party: this is what SOCKS is for.</p>
<h2>SOCKS Proxying</h2>
<p>SOCKS is a protocol specifically designed to proxy network connections. It works by setting up a server which acts as a gateway/proxy to all connections. SOCKS aware applications can connect to it, send their packets, and the SOCKS server will take care of sending them to their destination.</p>
<p>As a general proxying mechanism, SOCKS is much more powerful than simple port forwarding. Through one single SOCKS server, you can proxy multiple applications and protocols, as long as they support SOCKS, which many applications do. Most web browsers and instant messengers for example natively support it.</p>
<p>So how is that related to SSH and how do you use it? Well SSH can act as a SOCKS server. It is as simple as:</p> <pre><code>ssh -D 7000 example.com
</code></pre>
<p>and you have a SOCKS server running on port 7000 of your machine, proxying all client connections through <code>example.com</code>. Now all you have to do is configure your applications to use the SOCKS server.</p>
<h2>Configuration: Mac OS X</h2>
<p>If your computer is running on Mac OS X, setting up a SOCKS proxy for
  <strong>all</strong> your applications can easily be done in one place. You need to go to your
  <strong>Network Preferences</strong>:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110628-1.png">
    <img src="http://asciithoughts.com/images/20110628-1.png" alt="Network icon in the System Preferences">
  </a>
</p>
<p>Select the
  <strong>Advanced</strong> button for your connection:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110628-2.png">
    <img src="http://asciithoughts.com/images/20110628-2.png" alt="Advanced button for Wifi connection">
  </a>
</p>
<p>Click the
  <strong>SOCKS Proxy</strong> option and fill out the parameters:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110628-3.png">
    <img src="http://asciithoughts.com/images/20110628-3.png" alt="Proxy configuration screen">
  </a>
</p>
<p>You&apos;re done. All the applications on your Mac will now use this configuration (at least those who do it right, which is most).</p>
<h2>Configuration: Firefox</h2>
<p>If your operating system doesn&apos;t provide such configuration, you can generally configure it on a per-application basis. With Firefox, go to
  <strong>Preferences</strong> -&gt;
  <strong>Advanced</strong> -&gt;
  <strong>Network</strong> and click on
  <strong>Settings</strong>
</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110628-4.png">
    <img src="http://asciithoughts.com/images/20110628-4.png" alt="Network configuration screen for Firefox">
  </a>
</p>
<p>There you can fill out the SOCKS proxy information:</p>
<p>
  <a class="img" href="http://asciithoughts.com/images/20110628-5.png">
    <img src="http://asciithoughts.com/images/20110628-5.png" alt="Proxy configuration screen for Firefox">
  </a>
</p>
<p>Other browsers have similar configuration screens. If you are gonna switch between
  <em>proxy</em> and
  <em>no proxy</em> pretty often, it might be useful to add a plugin/extension to your browser to be able to switch easily without having to go through all these configuration screens all the time :</p>
<ul>
  <li>
    <a href="https://addons.mozilla.org/en-US/firefox/addon/quickproxy/">QuickProxy</a> and
    <a href="https://addons.mozilla.org/en-US/firefox/addon/foxyproxy-standard/">FoxyProxy</a> for Firefox</li>
  <li>
    <a href="https://chrome.google.com/webstore/detail/caehdcpeofiiigpdhbabniblemipncjj">Proxy Switchy!</a> for Chrome</li>
</ul>
<h2>Final note: DNS</h2>
<p>The SOCKS protocol was built to proxy any kind of network connection through a server. When you enable a SOCKS proxy in
  <a href="http://kb.mozillazine.org/Network.proxy.socks_remote_dns">some browsers</a> however, there is one type of connection that doesn&apos;t get proxied: DNS.</p>
<p>When you request a web page at <code>http://example.com</code>, the browser will first ask the operating system to resolve <code>example.com</code> to a known IP address, and then go through the SOCKS proxy to connect to that IP. What this mean is that if you are using a proxy for privacy reasons, you are leaking information to the DNS resolver. Depending on your situation, that may or may not be an issue, but it&apos;s good to be aware of it. If you want to have full privacy, you need to go through an HTTP proxy instead, because in this case the HTTP proxy is in charge of the DNS resolution, not you (which of course only works if you are in control of the HTTP proxy, otherwise you just moved the problem somewhere else). Or you can just
  <a href="http://kb.mozillazine.org/Network.proxy.socks_remote_dns">change the default setting</a>.</p>
<h2>Conclusion</h2>
<p>That&apos;s it for today. Hope you enjoyed it.</p>
<p>Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2011/06/28/ssh-remote-tunneling/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2011/06/28/ssh-remote-tunneling/</guid>
            <pubDate>Tue, 28 Jun 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[SSH Local Tunneling]]></title>
            <description><![CDATA[<h1>SSH Local Tunneling</h1>
<p>What is tunneling? To put it simply, tunneling is the mechanism by which one port on your local machine
  <em>redirects</em> to another port on another machine. To understand what it means and why it&apos;s useful, let&apos;s look at a few use cases.</p>
<h2>Local forward, simple case</h2>
<p>Imagine you are working on a remote server, <code>example.com</code>. This server has a pretty standard configuration: Varnish, Apache, ...</p>
<ul>
  <li>Port 22 : SSH,
    <em>publicly accessible</em>
  </li>
  <li>Port 80 : Varnish,
    <em>publicly accessible</em>
  </li>
  <li>Port 8000 : Apache,
    <em>localhost only (private)</em>
  </li>
</ul>
<p>Let&apos;s now say that for some reason you are having troubles with the web server but you are not sure whether the problem is coming from Varnish or from the application itself. So you need to access Apache, but since Apache is listening on localhost, you can&apos;t just go to <code>http://example.com:8000</code>, you won&apos;t be able to connect. Here comes SSH tunneling to the rescue:</p> <pre><code>ssh -L 8000:127.0.0.1:8000 example.com
</code></pre>
<p>What does this do? It creates a tunnel between the local port <code>8000</code> on your machine to the IP address <code>127.0.0.1</code> and port <code>8000</code> on the remote machine. So, when you go to <code>http://127.0.0.1:8000</code>, your connection enters the tunnel and comes out on the other side knocking on Apache&apos;s door:</p> <pre><code>Browser -&gt; 127.0.0.1:8000 -&gt; SSH --+  # your machine
                                   |
Apache &lt;-  127.0.0.1:8000 &lt;- SSH &lt;-+  # the server
</code></pre>
<h2>Local forward, binding</h2>
<p>The full syntax of the local forward is this :</p> <pre><code>ssh -L bind:port:host:hostport example.com
</code></pre>
<p>By default, SSH will bind to
  <strong>all</strong> your network interfaces. So if you are connected to a network, and have an IP address (192.168.0.10 for example), anyone can connect to port 8000 on your machine and access the remote server:</p> <pre><code>Developer 2                          # other machine
  |
  +--&gt; 192.168.0.10:8000 -&gt; SSH --+  # your machine
                                  |
Apache &lt;- 127.0.0.1:8000 &lt;- SSH &lt;-+  # the server
</code></pre>
<p>Maybe the access to this server is sensitive, and you don&apos;t want anybody to come and fiddle with it. In this case, you can specify which network interface you want to bind to, and maybe restrict it to localhost:</p> <pre><code>ssh -L localhost:8000:127.0.0.1:8000 example.com
</code></pre>
<h2>Local forward, advanced case</h2>
<p>Let&apos;s go a little further. Imagine now that you want to access a web server located on <code>example2.com</code>. The problem is,
  <em>example2</em> is inside a private network and you don&apos;t have direct access to it. However,
  <em>example1</em> and
  <em>example2</em> are on the same network, and you have access to
  <em>example1</em>. This is the typical situation where you have access to a
  <em>bridge</em> machine which then gets you access to other machines. This is useful for us because SSH can redirect not only to a local port, but to any port on any machine we have access to on the remote server:</p> <pre><code>ssh -L 8000:example2.com:80 example1.com
</code></pre>
<p>Here is what happens : you enter the tunnel on port 8000 on your machine, get out on machine <code>example1.com</code> and then ask the operating system: &quot;get me to machine example2.com on port 80&quot;.</p> <pre><code>Browser -&gt; 127.0.0.1:8000 -&gt; SSH --+  # your machine
                                   |
         example2.com:80 &lt;- SSH &lt;--+  # example1
</code></pre>
<h2>With SSH config</h2>
<p>You can use aliases for this as well. If you regularly need to forward ports to/from the same host, you can do this in your <code>~/.ssh/config</code></p> <pre><code>Host ex1
  User user1
  Hostname example1.com

Host ex1proxy
  User user1
  Hostname example1.com
  LocalForward 8000 127.0.0.1:80
</code></pre>
<p>Which translates to:</p> <pre><code class="language-bash">ssh ex1       <span class="hljs-comment" style="color: #998; font-style: italic;"># ssh user1@example.com</span>
ssh ex1proxy  <span class="hljs-comment" style="color: #998; font-style: italic;"># ssh user1@example.com -L 8000:127.0.0.1:80</span>
</code></pre>
<h2>Multiple forwards</h2>
<p>You can of course use multiple forwards at the same time. So the following is valid:</p> <pre><code>ssh user1@example.com     \
  -L 8000:127.0.0.1:80    \
  -L 8080:127.0.0.1:8080  \
  -L 8000:example2.com:80
</code></pre>
<p>As well as:</p> <pre><code>Host ex1proxy
  User user1
  Hostname example1.com
  LocalForward 8000 127.0.0.1:80
  LocalForward 8080 127.0.0.1:8080
  LocalForward 8000 example2.com:80
</code></pre>
<h2>Daemon mode</h2>
<p>Finally, if you just want to use SSH to redirect ports, but do not need to launch commands on the remote server, you can do so with <code>-N</code>:</p> <pre><code>ssh ex1proxy -N &amp;  # launch SSH in daemon mode
[1] 30604          # PID of the process
</code></pre>
<p>When you want to kill it:</p> <pre><code>jobs
[1]+  Running ssh ...  # list background jobs

fg 1                   # bring job to the foreground
^CKilled by signal 2.  # stop it with CTRL+C
</code></pre>
<p>Or:</p> <pre><code>kill 30604
[1]+  Done  ssh ...
</code></pre>
<h2>Caveats</h2>
<p>While I used the same port <code>8000</code> for the local and remote machine throughout this post, you don&apos;t have to. So the following is just as valid:</p> <pre><code>ssh -L 9000:127.0.0.1:8000 example.com
</code></pre>
<p>Privileged ports still apply. So binding to port 80 for example requires you to sudo:</p> <pre><code>$ ssh -L 80:127.0.0.1:8000 example.com
Privileged ports can only be forwarded by root.

$ sudo ssh -L 80:127.0.0.1:8000 example.com
Password: ********

# SUCCESS
</code></pre>
<h2>Conclusion</h2>
<p>Tunneling is an invaluable tool in your arsenal, and you should definitely get familiar with it. As usual, you can find more information in the man:</p> <pre><code>man ssh
man ssh_config
</code></pre>
<p>Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2011/06/24/ssh-local-tunneling/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2011/06/24/ssh-local-tunneling/</guid>
            <pubDate>Fri, 24 Jun 2011 00:00:00 GMT</pubDate>
        </item>
        <item>
            <title><![CDATA[SSH Config & Aliases]]></title>
            <description><![CDATA[<h1>SSH Config &amp; Aliases</h1>
<p>SSH is a wonderful tool. It has many advanced features which can make your life easier as a developer, but they are not always well known or understood. So today, let&apos;s introduce one of these: aliases.</p>
<p>When you are working on a project, you often have to switch between different servers: a local server, a test/staging server and a production server. So you find yourself typing this all day:</p> <pre><code class="language-bash">ssh user@virtual-machine
ssh user@staging.server.com
ssh user@production.server.com
</code></pre>
<p>Some people find a workaround with shell aliases:</p> <pre><code class="language-bash"><span class="hljs-built_in" style="color: #0086b3;">alias</span> <span class="hljs-built_in" style="color: #0086b3;">test</span>=<span class="hljs-string" style="color: #d14;">&quot;ssh user@staging.server.com&quot;</span>
</code></pre>
<p>That works alright in most cases, but what happens for example when you have to use <code>scp</code> or a <code>rsync</code>: you have to type the full command again...</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># SSH to production server, quick and easy</span>
prod

<span class="hljs-comment" style="color: #998; font-style: italic;"># Copy a file from test server. #FAIL</span>
scp user@user.staging.server.com:/path .
</code></pre>
<h2>The solution</h2>
<p>Thankfully, SSH offers aliases too. They are defined in <code>~/.ssh/config</code>:</p> <pre><code>Host foo
  User user
  Hostname example.com
</code></pre>
<p>With that configuration in place, you can now do all of the following:</p> <pre><code class="language-bash"><span class="hljs-comment" style="color: #998; font-style: italic;"># ssh user@example.com</span>
ssh foo

<span class="hljs-comment" style="color: #998; font-style: italic;"># scp user@example.com:/some/path /bar</span>
scp foo:/some/path /bar

<span class="hljs-comment" style="color: #998; font-style: italic;"># rsync user@example.com:/some/path /bar</span>
rsync foo:/some/path /bar
</code></pre>
<h2>All servers in a project</h2>
<p>I follow a convention for all my servers and use suffixes to identify which server I&apos;m connecting too. So let&apos;s say I want quick access to all the environments of my project
  <em>example</em>. Here is what my config would look like:</p> <pre><code>Host example.dev
  User deploy
  Hostname virtual-machine

Host example.test
  User deploy
  Hostname test.example.com

Host example.prod
  User deploy
  Hostname www.example.com
</code></pre>
<p>This way, I can quickly go to any of those servers, and I don&apos;t have to remember their exact name, IP address, login, etc.:</p> <pre><code class="language-bash">ssh example.dev    <span class="hljs-comment" style="color: #998; font-style: italic;"># local vm</span>
ssh example.test   <span class="hljs-comment" style="color: #998; font-style: italic;"># testing server</span>
ssh example.prod   <span class="hljs-comment" style="color: #998; font-style: italic;"># production server</span>
</code></pre>
<p>Of course, the following now works too:</p> <pre><code class="language-bash">rsync -avz example.test:/var/www path
</code></pre>
<h2>Other options</h2>
<p>You don&apos;t have to limit yourself to <code>User</code> and <code>Hostname</code>, you can use the full range of SSH options. Here is a more advanced configuration:</p> <pre><code>Host example
  Port 678
  User special
  Hostname 88.88.88.88
  LocalForward 8000 127.0.0.1:80
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_example_dsa
  Compression yes
</code></pre>
<p>You can find more information about the options available and the general format of the file in the man:</p> <pre><code class="language-bash">man ssh_config
</code></pre>
<h2>Conclusion</h2>
<p>SSH aliases can dramatically ease your life. Use them!</p>
<p>Until next time, Cheers!</p>]]></description>
            <link>http://asciithoughts.com/posts/2011/06/15/ssh-config-and-aliases/</link>
            <guid isPermaLink="true">http://asciithoughts.com/posts/2011/06/15/ssh-config-and-aliases/</guid>
            <pubDate>Wed, 15 Jun 2011 00:00:00 GMT</pubDate>
        </item>
    </channel>
</rss>