簡體   English   中英

ssh-keygen 為空 output,在 macOS Ventura 13.1 上的 python3.10 中帶有 subprocess.run 和 os.system

[英]ssh-keygen empty output with subprocess.run and os.system in python3.10 on macOS Ventura 13.1

我一直在嘗試 output 使用 python 和“子進程”庫從 ssh-keygen (CLI) 生成的簽名公鑰。 我還嘗試了具有相同結果的“os”庫。 我真的很想了解為什么它沒有按照我的意願進行:像我期望的那樣在屏幕上顯示“ssh-keygen -Lf {keyfile}”的 output。

當我使用 CLI (darwin / macOS Ventura 13.1) 在同一密鑰文件上使用此命令時,我得到了預期的結果。 當我在 Python3.10 中使用命令時,我得到一個返回碼“1”並且沒有重大錯誤來幫助我理解問題。 請看下面:

預期成績:

shell-prompt$> ssh-keygen -Lf ~/.ssh/signed_key.pub
/Users/USER/.ssh/signed_kali-os.pub:
        Type: ssh-rsa-cert-v01@openssh.com user certificate
        Public key: RSA-CERT SHA256:REDACTED
        Signing CA: RSA SHA256:REDACTED (using rsa-sha2-256)
        Key ID: "vault-oidc-USER@domain.com-REDACTED"
        Serial: REDACTED
        Valid: from 2023-01-24T20:56:07 to 2023-01-24T21:01:37
        Principals: 
                principal-user
        Critical Options: (none)
        Extensions: 
                permit-pty

基本 Python3.10 腳本中的 Output 在打印返回碼時為“1”,在打印 stdout 和/或 stderr 時為空(無)。 我已經嘗試了所有我能想到的印刷方式。

這是我嘗試過但沒有成功的兩種代碼解決方案。 當我用諸如“ls -l”或“cat”之類的基本命令替換命令 (ssh-keygen) 時,我得到了預期的 output。 我相信變量 signedPath 會按預期工作,因為它在未顯示的代碼的其他部分工作,並且當我用硬編碼路徑替換變量時它仍然失敗。

1)
if sys.platform == "linux" or sys.platform == "linux2" or sys.platform == "darwin":
        keyOut = subprocess.run(['ssh-keygen','-Lf',signedPath],capture_output=True)
        print(keyOut.stdout.decode())
2)
os.system('ssh-keygen -Lf {key}'.format(key=signedPath))

我在找什么? 最終,我想將此代碼用於屏幕上的簽名公鑰 output,因為我喜歡使用 ssh-keygen 獲得的 output 格式,並且我在 Python 中遇到其他 SSH 密鑰庫的問題。如果有一個更好的解決方案,我很想得到一些幫助,但我真的開始嘗試理解為什么這個特定的代碼不起作用,所以我想要一個答案而不是一個單獨的解決方案。 非常感謝這里的任何幫助。

---+++ 已解決 +++---


好吧……這很尷尬……但我知道這發生在我們所有人身上。 我添加了 5 秒的睡眠,現在它按預期工作。 令人沮喪的是,在我的代碼退出后,文件已完成寫入,所以我不知道它在運行時是 0 字節,直到我放入更多打印語句。我正在編寫的代碼與保險庫服務器通信以簽署我的公鑰,並且我沒有給它時間來完成,所以 python 試圖讀取一個 0 字節的文件......我在制作另一個最小代碼示例時想到了這一點。

---+++ 解決方案+++---


如果您遇到類似的問題,可能是您嘗試讀取的文件在運行時為 0 字節。

這不是一個完整的答案——從你的問題中不清楚為什么你沒有看到預期的 output——但我想展示一個最小的、完整的、可驗證的示例(“MCVE”)可能是什么樣子的作為提供一個工作示例。

考慮這段代碼,它 (a) 生成一個密鑰用作簽名證書,(b) 生成一個要簽名的密鑰,然后 (c) 使用 (a) 中的密鑰對 (b) 中的密鑰進行簽名:

#!/usr/bin/python

import os
import subprocess

expected_files = [
    "ca_key",
    "ca_key.pub",
    "user_key",
    "user_key.pub",
    "user_key-cert.pub",
]

for name in expected_files:
    try:
        os.remove(name)
    except FileNotFoundError:
        pass

# I'm using `check_output` -- and ignoring the return value -- in order to
# supress the output from these commands. This is effectively a shortcut
# for `subprocess.run` with `capture_output=True` and `check=True`.
print("generating keys")
subprocess.check_output(["ssh-keygen", "-t", "rsa", "-f", "ca_key", "-N", ""])
subprocess.check_output(["ssh-keygen", "-t", "rsa", "-f", "user_key", "-N", ""])
subprocess.check_output(
    ["ssh-keygen", "-s", "ca_key", "-I", "user_key", "user_key.pub"]
)

# Verify that we created everything we expected to create.
for name in expected_files:
    assert os.path.isfile(name)

# Here's the code from your question.
signedPath = "user_key-cert.pub"
keyOut = subprocess.run(["ssh-keygen", "-Lf", signedPath], capture_output=True)

print("keyOut:", keyOut.stdout.decode())

上面的代碼是一個獨立的示例:它創建了所有必要的文件來運行您在問題中詢問的代碼; 有人可以暗示復制示例,將其粘貼到文件中,然后運行它。

在我的系統 ( sys.platform == linux ) 上,運行這個例子會產生:

generating keys
Signed user key user_key-cert.pub: id "user_key" serial 0 valid forever
keyOut: user_key-cert.pub:
        Type: ssh-rsa-cert-v01@openssh.com user certificate
        Public key: RSA-CERT SHA256:Bca+gRvhx3twR0vFhIU3gr+TKIfFq2I1+lraJ6Z2QAI
        Signing CA: RSA SHA256:muz+6U5h9ZGyslm50m1F0VNG8VrseuQRzYwdUm2iGoo (using rsa-sha2-512)
        Key ID: "user_key"
        Serial: 0
        Valid: forever
        Principals: (none)
        Critical Options: (none)
        Extensions:
                permit-X11-forwarding
                permit-agent-forwarding
                permit-port-forwarding
                permit-pty
                permit-user-rc

如果您在 Linux 系統上運行代碼並且您看到不同的行為,我相信通過一些關於您的環境的額外細節,我們可以弄清楚發生了什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM