iTerm2 ssh自动登陆脚本

方式一

新建一个shell文件,如ssh.sh

#!/usr/bin/expect -f
set user username
set password password
set host ip/domian
set port 22
set timeout -1

spawn ssh $user@$host
expect "*assword:*"
send "$password\r"
interact
expect eof

然后在iterm2里新建profile, command里面写文件路径在这里插入图片描述

第一次登入服务器遇到yes/no的问题

MacOS编辑

vi /etc/ssh/ssh_config 或者 vi ~/.ssh/config 加入如下内容
Host *
StrictHostKeyChecking=no
UserKnownHostsFile=/dev/null

方式二

新建一个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 , We'll use that later .
set port 22
set user username
set pswd password

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\n";exp_continue;}
        -re "(p|P)ass(word|wd):"
        {send "$pswd\n"}
}
# expect  It's 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 it's the first time to log in , There will be  "yes/no"  String , Is sent (send) Instructions  "yes\r", Then continue to (exp_continue).

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

添加权限
chmod a+x ./iterm2ssh.sh

然后在iterm2里新建profile,
在这里插入图片描述
在这里插入图片描述
再编辑一个触发器

Are you sure you want to continue connecting (yes/no)?

在这里插入图片描述

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址