2014-01-16 10 views
0

authorized_keys 파일에 필요한 키를 추출하려고합니다. 그것은 키 파일 (.pub)을 열 때 얻을 수있는 것과는 다릅니다. 지금까지 내 코드입니다. pubfile에서 실행하려고 할 때마다 파일의 첫 번째 줄에 SSH2를 가리키는 잘못된 구문이 표시됩니다. "---- BEGIN SSH2 PUBLIC KEY ----"왜 이것이 작동하지 않는지는 모르겠다. , parsepub.py 명명 된 모듈을 가정python의 ssh2 공개 키에서 authorized_keys 키의 키 추출

#!/usr/bin/env python 

import subprocess 
import sys 

def parse_pubkey(pubfile): 
    """Return the key-type and key from a public-key file. 
    """ 
    try: 
     keystr = subprocess.check_output(
      'ssh-keygen -i -f %s 2>/dev/null' % pubfile, 
      shell=True) 
    except subprocess.CalledProcessError: 
     with open(pubfile) as f: 
      keystr = f.read() 
    return keystr.split()[0:2] 

if __name__ == '__main__': 
    pubfilename = sys.argv[1] 
    print parse_pubkey(pubfilename) 

그것은 따라서 실행된다 : 어떤 도움도 여기에

#!/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) 
+0

함수는 인수를 취합니다. b 당신이 게시 한 코드의 맨 아래에 전화를 걸면 그것을 포기하지 않을 것입니까? –

+0

당신은 parse_pubkey()에서 무엇을 의미합니까 ?? –

+0

나는 그것을 parse_pubkey (pubfile.pub) @Decency라고 불렀다. –

답변

1

에 미리 감사는 코드 내 재 작성, 산세 주석의

$ python parsepub.py id_rsa.pub