7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OSXでSSH Tunnelingを利用してSMTP接続を行う方法

Last updated at Posted at 2014-04-10

#やりたいこと

  • SSH Tunnelを利用して,外部のSMTPサーバに接続したい.
  • ssh コマンドで実現は可能
terminal
$ ssh -i <private_key> -N -L <tunneling port>:smtp.server:25 user@smtp.server &
  • OSXにログインした際に自動的にtunnelingをつなぎ続けてほしい. (本題)
  • bash_profileに設定すると毎回terminalを立ち上げる必要がある.
  • 一度接続が切れると再度実行が必要

#やったこと

  • launchctlを利用してsmtpに接続するjobを登録した.
  • login時に自動的に実行し,継続させるよう設定した.

#手順
以下の様にplistを作成.

~/Library/LaunchAgents/smtptunnel.plist
 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>smtptunnel</string>
	<key>UserName</key>
	<string>user_name</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/bin/ssh</string>
		<string>-i</string>
		<string>private_key</string>
		<string>-N</string>
		<string>-L</string>
		<string>xxxx:smtp.server:25</string>
		<string></string>
	</array>
	<key>KeepAlive</key>
	<true/>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

launchctlにjobを登録.

terminal
$ launchctl -w load ~/Library/LaunchAgents/smtptunnel.plist

登録されたjobが動いているかを確認

terminal
$ launchctl list | grep smtp
760	-	smtptunnel

最後にsmtpサーバを localhost:xxxx に設定すれば完了.

#感想

  • OSXでサービス管理をどうしてるのか今更理解できた.
  • 軽いサービスならplistいじるだけでできそうなので応用効きそう.

#参考

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?