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

posted @   liwl1991  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示