LoginSignup
3
3

More than 5 years have passed since last update.

リモートサーバの出力をクリップボードにコピーしてくれるRubyサーバを立てる

Last updated at Posted at 2015-01-16

よくtmux on tmuxで作業をするんだけどリモートのコマンド出力をローカルにコピーするときに割と面倒くさい。

ssh接続したリモートサーバの出力を手元Macのクリップボードに送る - Glide Note - グライドノート を使おうかと思ったけどできればLaunchAgentを使いたくなかったのでRubyで簡単なサーバを書いた。

#!/usr/bin/env ruby

require 'socket'
require 'terminal-notifier'

server = TCPServer.open('localhost', 2224)

loop do
  socket = server.accept
  buffer = socket.read
  IO.popen('pbcopy', 'w') { |f| f << buffer }
  TerminalNotifier.notify(buffer, :title => 'Called Pbcopy')
  socket.close
end

server.close

これを起動しておいて(自分はgodでプロセス管理することにした) echo 'hoge' | nc localhost 2224 って打ってクリップボードにhogeがコピーされたら成功

あとはSSH先からトンネリングするように ~/.ssh/config に設定を追記しておいて、SSH先からncで叩けるようにして完成。

Host *
  RemoteForward 2224 localhost:2224
3
3
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
3
3