{"id":234,"date":"2011-01-04T00:19:35","date_gmt":"2011-01-04T06:19:35","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/?p=234"},"modified":"2011-01-04T00:19:49","modified_gmt":"2011-01-04T06:19:49","slug":"managing-autossh-via-monit","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2011\/01\/04\/managing-autossh-via-monit\/","title":{"rendered":"Managing autossh via monit"},"content":{"rendered":"<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 \/>\n<!--more--><br \/>\nI 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>\n<p>The monit configuration stanza (in monitrc) looks like this:<\/p>\n<pre>check process mailtunnel with pidfile \/var\/run\/autossh.pid\r\n  start program = \"\/root\/start_mailtunnel.sh\"\r\n  stop program = \"\/root\/stop_mailtunnel.sh\"\r\n  if changed pid then alert\r\n  if failed host localhost port 2525 protocol smtp with timeout 30 seconds then alert\r\n  if 3 restarts within 5 cycles then timeout<\/pre>\n<p>start_mailtunnel.sh looks like this:<\/p>\n<pre>#!\/bin\/sh\r\nexport AUTOSSH_PIDFILE=\/var\/run\/autossh.pid\r\n\/usr\/bin\/autossh -N mailtunnel &<\/pre>\n<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>\n<p>The ssh mailtunnel config just has some port forwarding rules, and &#8220;ExitOnForwardFailure yes&#8221;.<\/p>\n<p>Finally, stop_mailtunnel.sh looks like this:<\/p>\n<pre>#!\/bin\/sh\r\ntest -e \/var\/run\/autossh.pid && kill `cat \/var\/run\/autossh.pid`<\/pre>\n","protected":false},"excerpt":{"rendered":"<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.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,57,56,55,3],"tags":[],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-linux","category-networking","category-openssh","category-security","category-ubuntu"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/234"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":7,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":241,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/234\/revisions\/241"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}