LoginSignup
5
12

More than 3 years have passed since last update.

TeraTermマクロ SSH自動ログイン

Posted at

2017/06/03に作成した資料をアップします。

TeraTermのインストールフォルダにあるログインマクロの雛型「ssh2login.ttl」をコピーまたは編集し、そのファイルを実行することにより。自動ログインを行うことができる。

1. パスワード入力版(ダイアログでパスワードを入力する。)

ssh2login.ttl
username = 'user01'          ; ログインするサーバーのユーザー名
hostname = '192.168.0.8'     ; ログインするサーバーのホスト名またはIPアドレス
portnum = '22'

msg = 'Enter password for user '
strconcat msg username
passwordbox msg 'Get password'

msg = hostname
strconcat msg ':portnum /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg inputstr

connect msg

2. パスワード保存版(平文)

マクロファイルにパスワードを保存し、SSH接続を自動化する。

ssh2login.ttl
username = 'user01'          ; ログインするサーバーのユーザー名
hostname = '192.168.0.8'     ; ログインするサーバーのホスト名またはIPアドレス
userpasswd = 'xxxxxxxx'      ; ログインするサーバーのパスワード
portnum = '22'

msg = hostname
strconcat msg ':portnum /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg userpasswd     ; パスワード変数(userpasswd)を利用
strconcat msg inputstr

connect msg

3. パスワード保存版(暗号化)

パスワード暗号化ファイル(passwd.dat)でパスワードを保存し、SSH接続を自動化する。
(初回のみパスワードを入力、2回目以降はパスワード入力無し。)

ssh2login.ttl
username = 'user01'          ; ログインするサーバーのユーザー名
hostname = '192.168.0.8'     ; ログインするサーバーのホスト名またはIPアドレス
passwdfile = 'passwd.dat'    ; パスワード暗号化ファイル
portnum = '22'

getpassword passwdfile username userpasswd  ; パスワード暗号化ファイルからパスワード取得

msg = hostname
strconcat msg ':portnum /ssh /auth=password /user='
strconcat msg username
strconcat msg ' /passwd='
strconcat msg userpasswd
strconcat msg inputstr

connect msg

4. 公開鍵認証版

秘密鍵でSSH接続を自動化する。
(初回のみ秘密鍵のパスフレーズを入力、2回目以降はパスフレーズ入力無し。)

ssh2login.ttl
username = 'otsuka'          ; ログインするサーバーのユーザー名
hostname = '192.168.0.8'     ; ログインするサーバーのホスト名またはIPアドレス
key_file = 'id_rsa'          ; 秘密鍵ファイルを指定
passwdfile = 'passwd.dat'    ; パスワード暗号化ファイル
portnum = '22'

getpassword passwdfile username userpasswd ; パスワード暗号化ファイルからパスフレーズ取得

msg = hostname
strconcat msg ':portnum /ssh /auth=publickey /user='     ; 公開鍵認証を指定
strconcat msg username
strconcat msg ' /passwd='
strconcat msg userpasswd
strconcat msg ' /keyfile=' 
strconcat msg key_file
strconcat msg inputstr
connect msg

【秘密鍵、公開鍵の作成及び配置】

  • ホームディレクトリに移動
$ cd
  • 秘密鍵、公開鍵のセットを作成
$ ssh-keygen -t rsa -b 2048 -P パスフレーズ -f id_rsa

id_rsa(秘密鍵)、id_rsa.pub(公開鍵)が作成される。
id_rsa(秘密鍵)をローカル端末(TaraTerm側に配置)する。
id_rsa.pub(公開鍵)を以下の通り、サーバー側のユーザーのホームディレクトリに配置する。

  • ホームディレクトリに.sshディレクトリが存在しいない場合は作成
$ mkdir .ssh
  • .sshの権限を変更
$ chmod 700 .ssh
  • .sshディレクトリにauthorized_keysファイルが存在しない場合は作成
$ touch ./.ssh/authorized_keys
  • authorized_keysファイルに公開鍵を追加
$ cat id_rsa.pub >> ./.ssh/authorized_keys
  • authorized_keysファイルの権限を変更
$ chmod 600 ./.ssh/authorized_keys

以上

5
12
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
5
12