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

猜你喜欢

转载自blog.csdn.net/Nightwish5/article/details/109448146