平衡点


2008/10/29

_ ssh でのポートフォワード

やりたい事は

localhost から gwhost を経由して remotehost へ接続

です. HostKeyAlias を使用します. とりあえず フォワードする Port は 30001 + SSH の番号とします.

/etc/hosts の編集

remotehost.forward という host が localhost を指すように設定します.

127.0.0.1       localhost remotehost.forward

.ssh/config の修正

以下の情報を追加

Host remote.forward
  ForwardAgent yes
  Port [30001 + remotehost の SSH の Port 番号]
  HostKeyAlias remotehost.remotedomain

接続時

実際に接続する際には以下の通り. この例では,

localhost:30023 <-> gwhost <-> remotehost:22

となる.

$ ssh -f -N -L 30023:remotehost.forward:22 gwhost
$ ssh remotehost.forward
(接続できる)
$ scp remotehost.forward:orig.txt .
(remotehost の orig.txt を手元に scp)
$ scp test.txt remotehost.forward:
(localhost の test.txt を remotehost へ scp)

最後に

最初に

$ ssh -f -N -L 30023:remotehost.forward:22 gwhost

を実行するのが面倒なので, 以下の ruby script を使用中.

#!/usr/bin/env ruby

ssh="/usr/bin/ssh"
gateway="gatewayhost.remotedomain"
config = ENV['HOME']+"/.ssh/config"
pidfile = "/tmp/create-gw.pid"

def str2hash(str)
  @str = str
  s = @str.split(/\:/)
  h = {}
  while ! s.empty?
    h.store(s.shift, s.shift)
  end
  return h
end

def parseconf(config)
  @config = config
  conf = []
  File.open(config){|f|
    while src = f.gets("") do
      if src =~/^Host.*forward$\n/
        s = src.strip!.gsub!(/^\s\s/,"").gsub!(/\n/,":").gsub(/ /,":")
        conf.push(str2hash(s))
      end
    end
  }
  return conf
end

def create_gateway(conf, ssh, gateway)
  @conf = conf
  @ssh = ssh
  @gateway = gateway
  @conf.each do |host|
    port = host['Port']
    hostkeyalias = host['HostKeyAlias']
    system("#{@ssh}","-f","-N","-L","#{port}:#{hostkeyalias}:22","#{@gateway}")
  end
end

def get_gateway_pid(ssh=nil)
  @ssh = ssh ||= "/usr/bin/ssh"
  return pids = `pgrep -f "#{@ssh} -f -N -L"`.split(/\n/)
end

opt = ARGV[0] ||= "create"
if opt == "create" or opt == "start" or opt == "init"
  unless FileTest.exist?(pidfile)
    create_gateway(parseconf(config), ssh, gateway) 
    pid = File.open(pidfile, 'w')
    pid.puts(get_gateway_pid(ssh))
    pid.close
  end
elsif opt == "close" or opt == "exit" or opt == "stop" 
  File.read(pidfile).each do |pid|
    Process.kill(9,pid.to_i)
  end
  File.delete(pidfile)
end

_ 12:00 からミーティングなのに

起きたら 11:38. タクシー使ってしまった...


連絡先など
最近の日記
  • 2024/04/28
    • 1. CF-SR3 での生活環境構築 (できてない)
  • 2024/03/29
    • 1. Debian GNU/Linux on CF-SR3
  • 2024/03/25
    • 1. org-mode → beamer でのリスト記号の一時変更
  • 2024/03/22
    • 1. 静的htmlによるGit Repository Browser
  • 2024/02/15
    • 1. org-mode → bemaer での番号付きリスト
一覧
2006|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|02|03|04|08|09|10|11|12|
2013|01|02|03|04|05|06|08|09|10|11|12|
2014|01|02|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|09|10|
2016|02|03|
2017|01|02|03|05|06|07|09|11|12|
2018|03|06|07|10|11|12|
2019|01|02|03|04|05|07|10|12|
2020|01|02|03|04|05|08|09|10|11|12|
2021|01|02|03|05|06|07|08|09|11|12|
2022|01|02|03|04|05|06|08|10|11|12|
2023|02|03|04|06|08|09|11|12|
2024|01|02|03|04|
Back to Top ▲