Linux expect用法 ssh-keygen免交互操作

通过expect指令,将 ssh-key -t -rsa -b 2048 自动免交互。
平常手动要操作的操作(假设是无密码按回车,按y):
在这里插入图片描述

$ cat testExpect.sh
#!/usr/bin/expect

echo "test expect ....."
expect -c "
        spawn ssh-keygen -t rsa -b 2048
        expect {
                \"Enter file in\" {send \"\r\"; exp_continue}
                \"Overwrite\" {send \"y\r\"; exp_continue}
                \"Enter passphrase\" {send \"\r\";exp_continue}
                \"passphrase again\" {send \"\r exit\";exp_continue}

        }
"

echo "end ....."

=分割线===

参考例子:

# -c:执行脚本前先执行的命令,可多次使用。
    expect -c "
        #使用spawn进行 连接操作
       spawn ssh -p $PORT $USER@$IP
       expect {
          #当碰到 yes/no的时候send发送 yes , \r表示回车键, 否则 exp_continue表继续执行
          \"(yes/no)\" {send \"yes\r\"; exp_continue}
          \"password:\" {send \"$PASS\r\"; exp_continue}
           # 碰到 $USER@*时,这里的*是通配符。  
          \"$USER@*\" {send \"$COMMAND\r exit\r\"; exp_continue}
       }
    "

参考博客:https://blog.csdn.net/zhandar44/article/details/91488287

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值