在 Mac 中用 Parallel Desktop 安裝了 Ubuntu Server 的虛擬機(jī),而 Mac 下的 iTerm2 本就是一款很不錯(cuò)的 Terminal 工具,但默認(rèn)情況下,需要手動(dòng)輸入 SSH 的登錄腳本,是一個(gè)比較低效的動(dòng)作。
實(shí)現(xiàn)自動(dòng)登錄是一個(gè)很自然的想法,而 iTerm2 也正好提供了自動(dòng)執(zhí)行腳本機(jī)制,如下簡(jiǎn)單的幾步即可實(shí)現(xiàn):
1. 編寫(xiě)自動(dòng)登錄的 bash 腳本
#!/usr/bin/expect -f
set user andy
set host 10.211.55.3
set password 12345678
set timeout 30
spawn ssh $user@$host
expect "*assword:*"
send "$password\r"
interact
expect eof
2. 務(wù)必將 bash 腳本文件的權(quán)限修改為可執(zhí)行
3. 設(shè)置 iTerm2 Profile
iTerm2 - Preferences - Profiles 打開(kāi)設(shè)置界面。點(diǎn)擊左下角的“+”創(chuàng)建一個(gè)新的 Profile,在右側(cè) Command 設(shè)置第一步腳本的路徑,在 Name 中設(shè)置名稱以及快捷鍵,如下圖所示

Profiles設(shè)置界面
3. 執(zhí)行 Profile
Profiles - Ubuntu-Server,執(zhí)行指定的 Profile 自動(dòng)執(zhí)行腳本,執(zhí)行結(jié)果如下
spawn ssh andy@10.211.55.3
andy@10.211.55.3's password:
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.15.0-38-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Thu Oct 25 13:12:16 UTC 2018
System load: 0.08 Processes: 108
Usage of /: 6.2% of 62.74GB Users logged in: 1
Memory usage: 16% IP address for enp0s5: 10.211.55.5
Swap usage: 0%
89 packages can be updated.
35 updates are security updates.
Last login: Thu Oct 25 13:11:43 2018 from 10.211.55.2
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
andy@andy-ubuntu-server:~$