shell调用expect实现用户创建免密登录
这是一个用于(批量或者免交互)创建用户免密的shell脚本
通过shell的for循环和变量,实现批处理和免交互
#!/bin/bash
password="liwanliang"
expect -c "
spawn ssh liwl@node084
expect {
\"*yes/no*\" { send \"yes\r\", exp_continue }
\"*password:*\" { send \"${password}\r\" }
}
expect \"$ \"
send \"ssh-keygen -t rsa -f ~/.ssh/id_rsa -P \'\'\r\"
expect \"$ \"
send \"cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys\r\"
expect \"$ \"
send \"chmod 600 ~/.ssh/authorized_keys\r\"
expect \"$ \"
send \"exit\r\"
"
echo "操作成功!"
上面的cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys
可以换成ssh-copy-id -i ~/.ssh/id_rsa.pub xxx@localhost