0

我想使用可选文件和可选密码来自动化 ssh-keygen 并在找到文件时覆盖。如果我在终端中写出脚本,它会问我三个问题。我根本不希望它提示我,只是运行并返回响应或返回错误。

到目前为止,这是我的代码:

#!/usr/bin/osascript

on run

    set email to "myemail@email.com"
    set result to do shell script "ssh-keygen -t rsa -C \"" & email & "\""

    return result
end run

注意:这个问题非常相似,但发帖人没有提到传入可选文件,也没有提到他如何处理已经存在的文件。

4

1 回答 1

1

用 指定密码(或无密码)-N和用 指定文件位置-f,如果存在密钥,请先删除它们。例如:

set keyPath to "~/.ssh/my_rsa_key"

do shell script ("rm " & keyPath & " " & keyPath & ".pub &> /dev/null;:")

set email to "myemail@email.com"
set myPassPhrase to "abcdefgh"

do shell script ¬
    "ssh-keygen -t rsa -C \"" & email & "\" -N \"" & myPassPhrase & "\" -f " & keyPath

result是分配给最后一条指令结果的自动变量,因此您无需显式指定它,但如果您确实想要分配变量,则可以将其替换为其他变量名。)

此外,考虑到大多数这些操作都发生在 内do shell script,您最好将整个事情作为 Bash 脚本执行,例如:

#!/bin/bash

keyPath="~/.ssh/my_rsa_key"
email="myemail@email.com"
myPassPhrase="abcdefgh"

rm $keyPath $keyPath.pub &> /dev/null
ssh-keygen -t rsa -C "$email" -N "$myPassPhrase" -f "$keyPath"
于 2015-01-18T08:05:49.593 回答