1 安装expect
brew install expect
2 编写脚本
我这里需要两次输入密码,所以写了两遍expect
#! /usr/bin/expect
# ./expect_ssh ip_address
set timeout 30
set ip [lindex $argv 0]
set password your_password
spawn ssh $ip
# for gateway
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
# for hadoop host
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
文件保存为expect_ssh.sh
添加执行权限
chmod u+x expect_ssh.sh
在~/.bash_profile中添加命令别名
alias ssh2='~/Shell/expect_ssh.sh'
执行命令
ssh2 192.168.1.1
这样就可以为登录服务器输入密码了。