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

<channel>
	<title>Pervasive Code &#187; OpenSSH</title>
	<atom:link href="http://www.pervasivecode.com/blog/category/openssh/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pervasivecode.com/blog</link>
	<description>Jamie Flournoy's Software Development Blog</description>
	<lastBuildDate>Wed, 01 Feb 2012 06:11:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Managing autossh via monit</title>
		<link>http://www.pervasivecode.com/blog/2011/01/04/managing-autossh-via-monit/</link>
		<comments>http://www.pervasivecode.com/blog/2011/01/04/managing-autossh-via-monit/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 06:19:35 +0000</pubDate>
		<dc:creator>Jamie Flournoy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[networking]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.pervasivecode.com/blog/?p=234</guid>
		<description><![CDATA[SSH port forwarding is so useful that sometimes you want to daemonize it, to create encrypted tunnels that never go away. But it&#8217;s not trivial to do this. Fortunately it is possible with a little fiddling, and I did it using monit.

I have two servers, let&#8217;s call them A and B, and I want to [...]]]></description>
			<content:encoded><![CDATA[<p>SSH port forwarding is so useful that sometimes you want to daemonize it, to create encrypted tunnels that never go away. But it&#8217;s not trivial to do this. Fortunately it is possible with a little fiddling, and I did it using monit.<br />
<span id="more-234"></span><br />
I have two servers, let&#8217;s call them A and B, and I want to connect via ssh from A to B all the time, reconnecting as needed to deal with network problems, reboots, or other reasons why the connection might drop. A program called autossh does most of this, but doesn&#8217;t handle reboots. So since I already have monit set up to watch other daemons and keep them from going crazy, I set up a monit configuration to manage autossh. All of this runs on server A; server B just has sshd and a mail server that&#8217;s going to try to deliver mail to localhost:2525 to get mail to server A.</p>
<p>The monit configuration stanza (in monitrc) looks like this:</p>
<pre>check process mailtunnel with pidfile /var/run/autossh.pid
  start program = "/root/start_mailtunnel.sh"
  stop program = "/root/stop_mailtunnel.sh"
  if changed pid then alert
  if failed host localhost port 2525 protocol smtp with timeout 30 seconds then alert
  if 3 restarts within 5 cycles then timeout</pre>
<p>start_mailtunnel.sh looks like this:</p>
<pre>#!/bin/sh
export AUTOSSH_PIDFILE=/var/run/autossh.pid
/usr/bin/autossh -N mailtunnel &#038;</pre>
<p>And that -N is the key to making ssh happily connect and not run a remote shell, which would want a tty on stdin. That&#8217;s how you tell it to just be a port-forwarding connection.</p>
<p>The ssh mailtunnel config just has some port forwarding rules, and &#8220;ExitOnForwardFailure yes&#8221;.</p>
<p>Finally, stop_mailtunnel.sh looks like this:</p>
<pre>#!/bin/sh
test -e /var/run/autossh.pid &#038;&#038; kill `cat /var/run/autossh.pid`</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pervasivecode.com/blog/2011/01/04/managing-autossh-via-monit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

