我正在尝试为 authorized_keys 文件提取所需的密钥。它与我打开密钥文件 (.pub) 时得到的不同。到目前为止,这是我的代码。每当我尝试在 pubfile 上运行它时,我都会在文件的第一行得到一个指向 SSH2 的无效语法。“---- BEGIN SSH2 PUBLIC KEY ----” 我不知道为什么这不起作用。提前感谢您的帮助
#!/bin/env python
import fileinput
import subprocess
import sys
def parse_pubkey( pubfile ):
"""This routine returns the key-type and key from a public-key file.
"""
try:
# try to parse the Windows-format file into an OpenSSH-compatible representation
# by calling the Unix "ssh-keygen" utility. This call will fail if the keyfile
# is already in OpenSSH format
keystr = subprocess.check_output( 'ssh-keygen -i -f %s 2>/dev/null' % pubfile, shell=True )
except subprocess.CalledProcessError:
# we caught an exception, so the file must already be in OpenSSH format. Just
# read in the contents
keystr = open( pubfile, 'r' ).read()
# now split the resulting string on whitespace and return the first two fields
return keystr.split()[0:2]
parse_pubkey(pubfilename.pub)