expect:使用 expect 脚本生成 RSA 密钥不起作用
expect: RSA key generation using expect script is not working
我正在尝试使用 expect 脚本使用 ssh-keygen 命令生成 SSH 密钥,但它似乎不起作用(它无法创建新密钥或如果密钥已经存在则覆盖)我无法弄清楚问题在这
#!/usr/bin/expect
spawn ssh-keygen
expect { -re "Enter file in which to save the key.*:\s?" {send "\n\r"; exp_continue}
-re "Overwrite.*\?\s?" {send "y\r"; exp_continue}
-re "Enter passphrase.*:\s?" {send "\n\r"; exp_continue}
-re ".*#\s?" { puts "matched propmt"}
}
脚本执行的输出:
./ssh-key-gen.sh
spawn ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@localhost scripts]#
问题是 send "\n\r"
对 "Enter file in which to save the key" 的回应。 \n 接受默认文件名,然后 \r 应用于 "Overwrite (y/n)?" 提示符。对该问题的空响应被解释为 "no",因此命令终止。去掉那个 \n 就可以了。
但是这样做不是更容易吗:
set file [file normalize ~/.ssh/id_rsa]
file delete $file
exec ssh-keygen -f $file -N {}
这可以在普通的 Tcl 中完成,不需要 expect。
我正在尝试使用 expect 脚本使用 ssh-keygen 命令生成 SSH 密钥,但它似乎不起作用(它无法创建新密钥或如果密钥已经存在则覆盖)我无法弄清楚问题在这
#!/usr/bin/expect
spawn ssh-keygen
expect { -re "Enter file in which to save the key.*:\s?" {send "\n\r"; exp_continue}
-re "Overwrite.*\?\s?" {send "y\r"; exp_continue}
-re "Enter passphrase.*:\s?" {send "\n\r"; exp_continue}
-re ".*#\s?" { puts "matched propmt"}
}
脚本执行的输出:
./ssh-key-gen.sh
spawn ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): [root@localhost scripts]#
问题是 send "\n\r"
对 "Enter file in which to save the key" 的回应。 \n 接受默认文件名,然后 \r 应用于 "Overwrite (y/n)?" 提示符。对该问题的空响应被解释为 "no",因此命令终止。去掉那个 \n 就可以了。
但是这样做不是更容易吗:
set file [file normalize ~/.ssh/id_rsa]
file delete $file
exec ssh-keygen -f $file -N {}
这可以在普通的 Tcl 中完成,不需要 expect。