[Mac] SSH自動ログイン用 bash ファイル

Mac の Terminal で SSH 接続する際、簡単にログインできるようにしたい。

環境:macOS Ventura 13.4(英語環境)

以下の内容で拡張子が bash のファイルを作る。このファイルをターミナルでファイルパスを指定して実行すればいい。あるいは拡張子を command にすれば、ダブルクリックで実行可能だ(ターミナルが開く)。

(A)ログインするだけなら

#1/bin/bash
ssh korosuke@192.168.0.9

(B)パスワードの入力までするなら

#!/usr/bin/expect -f
spawn ssh {ユーザー名}@192.168.0.9
expect "password:"
send "{パスワード}\r"
interact

作った bash ファイルはターミナルで実行権限を付与する

$ chmod +x {bash ファイルへのパス}

SSH ログインから $ exit コマンドでログアウトした際、[Process completed] と表示されてウィンドウが開いたままになる。単にウィンドウを閉じてしまえばいいが、自動でウィンドウを閉じるようにするには、「Terminal>Settings...>Profiles>Shell>When the shell exits」を「Close if the shell exited cleanly」にすればいい。

参考:
How to ssh with username AND password linux automatically from bash - Super User
http://superuser.com/questions/655582/how-to-ssh-with-username-and-password-linux-automatically-from-bash

terminal app stuck with process completed… - Apple Community
https://discussions.apple.com/thread/253694918


この記事が気に入ったらサポートをしてみませんか?