iterm 实现自动登录远程服务器

日常开发测试中经常需要登录远程服务器,每次都使用 ssh 登录非常繁琐,使用 iterm 可以简化我们的操作步骤。

制作一个exp脚本

  • 打开命令行,执行 cd 命令,进入家目录,进入 bin 目录,如果没有,自行创建一个即可。
  • 创建一个 exp 脚本
  • 给予执行权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #!/usr/bin/expect

    set timeout 30
    spawn ssh [lindex $argv 0]@[lindex $argv 1]
    expect {
    "(yes/no)?"
    {send "yes\n";exp_continue}
    "password:"
    {send "[lindex $argv 2]\n"}
    }
    interact

iterm 设置

  • 打开 Perferences -> Profiles , 点击 + 创建一个 Profile, 名字自由定义。
  • 右边选项卡,选择 General, 找到 Command 选中 Login Shell
  • send text at start 中输入 login.exp root 111.111.11.11 'passport'
  • Working Directory 选中 Home Directory

有时候你可能需要传入多个参数

有时候你可能是个跳板机,或者其它。你可以让 exp 脚本多接受几个参数,进行拼接。
自行修改一下就可以

例如下面这个就是我修改过,接收了三个参数。

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/expect

set timeout 30
spawn ssh -p [lindex $argv 0] [lindex $argv 1]@[lindex $argv 2]
expect {
"(yes/no)?"
{send "yes\n";exp_continue}
"password:"
{send "[lindex $argv 3]\n"}
}
interact

这里是对应的 send text at start 中的内容供你参考
login-4.exp 1111 loginname auto.company.com 'passport'

如何使用?

上面的操作保存之后,你就可以在 iterm 中右击,创建一个 tab 或者 创建一个新的窗口中可以选择到你刚才创建的名字的 profile,即可自动跳转登录

Author: rexmolo
Link: http://rexmolo.github.io/2019/07/01/automatically-login-remote-server-on-iterm/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.