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
这样就可以为登录服务器输入密码了。
本文介绍了如何通过安装expect并在Mac上使用Homebrew,编写expect脚本来实现SSH登录时自动输入密码的功能。步骤包括安装expect,创建并编辑expect脚本,设置脚本执行权限,以及在.bash_profile中添加命令别名,最后演示了如何使用这个脚本快速登录服务器。
924

被折叠的 条评论
为什么被折叠?



