LoginSignup
3
0

~/.ssh/configを使わずワンラインで多段sshをする方法

Last updated at Posted at 2023-05-31

github actionsで多段sshの処理を下記のような内容でテストしていました

secrets.CONFIG

Host hoge
  Hostname IPアドレス
  User ユーザー名
  IdentityFile ~/.ssh/hoge_private_key
  IdentitiesOnly yes

Host hogehoge
  Hostname IPアドレス
  User ユーザー名
  IdentityFile ~/.ssh/hogehoge_private_key
  IdentitiesOnly yes
  ProxyCommand ssh -CW %h:%p hoge

hostname.yml

# hostname.yml

name: hogehoge_hostname

on:
  push:

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 10

     - name: exec
       run: |
         
         mkdir ~/.ssh
         cd ~/.ssh
         echo "${{ secrets.HOGE_PRIVATE_KEY }}" > hoge_private_key
         chmod 600 hoge_private_key
         echo "${{ secrets.HOGEHOGE_PRIVATE_KEY }}" > hogehoge_private_key
         chmod 600 hogehoge_private_key
         echo "${{secrets.CONFIG}}" > config

         ssh -o StrictHostKeyChecking=no hogehoge "hostname"

以上を実施していたところ、下記のようなエラーが出てうまくかない、、、
kex_exchange_identification: Connection closed by remote host

よくわからんので~/.ssh/configを使わず、ワンラインで多段sshしちゃいましょう

実際に~/.ssh/configを使わずに多段sshをする処理に変える

参考

name: hogehoge_hostname

on:
  push:

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 10

     - name: exec
       run: |
         
         echo "${{ secrets.HOGE_PRIVATE_KEY }}" > hoge_private_key
         chmod 600 hoge_private_key
         echo "${{ secrets.HOGEHOGE_PRIVATE_KEY }}" > hogehoge_private_key
         chmod 600 hogehoge_private_key

         ssh -o StrictHostKeyChecking=no -o ProxyCommand='ssh -o StrictHostKeyChecking=no -i hoge_private_key -W %h:%p ユーザー名@IPアドレス' ユーザー名@IPアドレス -i hogehoge_private_key "hostname"

上記の内容でyamlを修正したところ、接続したいホストのhostnameが取得できました。

まとめ

本件はgithub actionsを使用した例でしたが、基本的には多少環境変わっても使えると思いますので、なにかの役に立てれば幸いです。

ちなみに

下記のジョブを追加すれば普通にgithub actionsで~/.ssh/config使えるようでした、、、

- name: SSH
  uses: appleboy/ssh-action@master
  with:
    host: IPアドレス
    username: ユーザー名
    key: ${{ secrets.SSH_PRIVATE_KEY }}
    passphrase: ${{ secrets.SSH_PASSPHRASE }}
    port: 22
3
0
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
0