为什么 bash 中的 expect 语句不能使用 ssh-keygen 命令?

Why expect statement in bash can't use ssh-keygen command?

我生成了一个 ssh-keygen 命令,并期待一些输出,发送 enter,这是以下 bash 脚本中的正确逻辑。

#! /bin/bash
ip="xxxx"
port="yyyy"
pass="zzzz"
cd  $HOME/.ssh
    /usr/bin/expect << EOF
    spawn ssh-keygen -t rsa -f $HOME/.ssh/id_rsa
    expect  "(empty for no passphrase):"
    send  "\r"
    expect  "Enter same passphrase again:"
    send  "\r"
EOF

执行 bash 脚本时没有错误信息,为什么我的 .ssh 目录中没有创建文件?

这里根本不需要 expect。如果您不需要密码,请使用 -N ""。 (~/.ssh/id_rsa 也是新密钥的默认位置,因此 -f 选项也不是必需的。)

#! /bin/bash
ip="xxxx"
port="yyyy"
pass="zzzz"

#cd  $HOME/.ssh
#    /usr/bin/expect << EOF
#    spawn ssh-keygen -t rsa -f $HOME/.ssh/id_rsa
#    expect  "(empty for no passphrase):"
#    send  "\r"
#    expect  "Enter same passphrase again:"
#    send  "\r"
#EOF

ssh-keygen -t rsa -f "$HOME"/.ssh/id_rsa -N ""