Linux经过expect配置自动登陆

home目录下建立.expect目录,并添加两个文件:bash

  1. expect登录脚本ssh-login.exp
#!/usr/bin/expect -f

set timeout 30

set host [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]

spawn ssh -p $port $user@$host
expect {
    "yes/no" { send "yes\r"; exp_continue }
    "password:" { send "$password\r" }
}

interact
  1. expect配置脚本ssh-login.sh
# alias ssh-name="./expect/ssh-login.exp host port username password"
alias ssh-n1.cluster=".expect/ssh-login.exp 192.168.9.181 22 root cluster"
alias ssh-n2.cluster=".expect/ssh-login.exp 192.168.9.182 22 root cluster"
alias ssh-n3.cluster=".expect/ssh-login.exp 192.168.9.183 22 root cluster"
alias ssh-n4.cluster=".expect/ssh-login.exp 192.168.9.184 22 root cluster"
alias ssh-n5.cluster=".expect/ssh-login.exp 192.168.9.185 22 root cluster"

.bashrc下添加ssh

if [ -f ~/.expect/ssh-login.sh ]; then
    . ~/.expect/ssh-login.sh
fi
相关文章
相关标签/搜索