iTerm2 ssh 自动登陆脚本

新建一个shell文件,如ssh_setting/autologin.sh

#!/usr/bin/expect

set timeout 30
set host [lindex $argv 0]
#  This line is to set a variable , Variable names can be used casually , Try to make sense , The parameters passed in are shown later ,0  Represents the first parameter , Well use that later .
set port [lindex $argv 1]
set user [lindex $argv 2]
set pswd [lindex $argv 3]

spawn ssh -p $port $user@$host
# spawn  yes  expect  Internal command of the environment , Its main function is to  ssh  Add a shell to the running process , Used to pass interactive instructions .

expect {
          
   
        "(yes/no)?"
        {
          
   send "yes
";exp_continue;}
	      -re "(p|P)ass(word|wd):"
        {
          
   send "$pswd
"}
}
# expect  Its also  expect  An internal command of the environment , It is used to judge whether the output after the last instruction input contains  ""  String in double quotes ,-re  Means match by regularity .
#  If its the first time to log in , There will be  "yes/no"  String , Is sent (send) Instructions  "yes
", Then continue to (exp_continue).

interact
# interact: Keep interactive after execution , Give control to the console

然后在iterm2里设置profile,脚本参数依次是ip,port,host,password 再编辑一个触发器

Are you sure you want to continue connecting (yes/no)?
经验分享 程序员 微信小程序 职场和发展