LoginSignup
1
2

More than 5 years have passed since last update.

エンジニア以外の人でも簡単にSSH秘密鍵と公開鍵と設定ファイルを生成できるバッチ

Posted at

エンジニア以外の人でも簡単にSSH鍵と設定ファイルを生成できるバッチを作った。

環境

・Windows 7 sp1以上(PowerShell3.0以上)
・OpenSSH7.3以上(Includeを使っているため)

コード

デフォルトではPowerShellの実行が許可されていないため、権限を変更してPowerShellを実行するバッチを作る。
ウインドウが自動で閉じられるとエラーが出た時に確認できないのでpauseを入れてある。

SSHSetup.bat
echo off
chcp 932
echo SSH鍵生成&設定変更開始…
powershell -NoProfile -ExecutionPolicy Unrestricted .\SSHSetup.ps1
echo 終了しました。
pause
exit

Includeは他の設定よりも前に書く必要があるので、~/.ssh/configの先頭に追加している。

SSHSetup.ps1
$ssh_host = "xxx.xxx.xxx.xxx"
$ssh_host_name = "xxx.xxx.xxx.xxx"
$config_dir = "~/.ssh"
$config_file = $config_dir + "/config"
$config_include_dir = $config_dir + "/include"
$config_include_file = $config_include_dir + "/config_" + ($ssh_host_name -replace "\.", "_")
$identity_file_name = "id_ed25519"
$identity_file = $config_dir + "/" + $identity_file_name
mkdir -Path $config_dir -Force

pushd $config_dir
if ((Test-Path $identity_file) -eq $false) {
  Start-Process -FilePath ssh-keygen.exe -ArgumentList ('-f ' + $identity_file_name + ' -t ed25519 -N ""') -NoNewWindow
}
popd

$config = "Host " + $ssh_host + "`r`n"
$config += "  HostName " + $ssh_host_name + "`r`n"
$config += "  IdentityFile " + $identity_file + "`r`n"
$config += "  AddressFamily inet`r`n"
$config += "  ServerAliveInterval 15`r`n"
$config += "  ServerAliveCountMax 10`r`n"
$config += "  ConnectionAttempts 3`r`n"
$config += "  GSSAPIAuthentication no`r`n"
$config += "  TCPKeepAlive yes`r`n"
$config += "  IdentitiesOnly yes`r`n"
$config += "  Compression yes`r`n"
$config += "  Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr`r`n"
$config += "  KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256`r`n"
$config += "  Macs umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-64-etm@openssh.com,umac-64@openssh.com`r`n"
$config += "  HostKeyAlgorithms ssh-ed25519-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,ssh-rsa,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256"
$config | Out-File -FilePath $config_include_file -Force -Encoding ascii

$add_include_file = $true
if ((Test-Path $config_file) -eq $true) {
  if ((Get-Content -Path $config_file -Raw -Encoding ascii | Select-String -Pattern ("Include " + $config_include_file) -Quiet -Encoding ascii) -eq $true) {
    $add_include_file = $false
  }
}
if ($add_include_file -eq $true) {
  $config_include = "Include " + $config_include_file + "`r`n"
  if ((Test-Path $config_file) -eq $true) {
    $config_file_data = Get-Content -Path $config_file -Raw -Encoding ascii
    $config_include | Out-File -FilePath $config_file -Encoding ascii
    $config_file_data | Out-File -FilePath $config_file -Append -NoClobber -Encoding ascii
  } else {
    $config_include | Out-File -FilePath $config_file -NoClobber -Encoding ascii
  }
}

exit 0

以上の2ファイルを作成して同じディレクトリに設定する。
SSHSetup.batを実行すると秘密鍵・公開鍵、SSH設定ファイルが生成される。
生成された公開鍵は接続先サーバーに設置しておく。

1
2
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
1
2