先确保你安装了:expect
brew install expect
然后保存下面的脚本到 /usr/local/bin/ssh.sh
给这个文件执行权限 chmod a+x /usr/local/bin/ssh.sh
#!/usr/bin/expect
# 设置变量
set timeout 20
set port [lindex $argv 0]
set user [lindex $argv 1]
set host [lindex $argv 2]
set password [lindex $argv 3]
# 启动SSH进程
spawn ssh -p $port $user@$host
# 处理可能出现的提示
expect {
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$password\r"
}
"password:" {
send "$password\r"
}
}
# 交互模式
interact
登录命令:ssh.sh 22 root 192.191.192.8 mypassword
可以通过 ssh.sh 端口 用户名 地址 密码的 方式直接登录