Python实现ssh公钥的本地验证

使用sshpubkeys实现本地对ssh公钥的验证:

用法说明:

import sys
from sshpubkeys import SSHKey

ssh = SSHKey("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQCxO38tKAJXIs9ivPxt7AY"
      "dfybgtAR1ow3Qkb9GPQ6wkFHQqcFDe6faKCxH6iDRteo4D8L8B"
      "xwzN42uZSB0nfmjkIxFTcEU3mFSXEbWByg78aoddMrAAjatyrh"
      "H1pON6P0= ojarva@ojar-laptop", strict_mode=True)
try:
    ssh.parse()
except InvalidKeyException as err:
    print("Invalid key:", err)
    sys.exit(1)
except NotImplementedError as err:
    print("Invalid key type:", err)
    sys.exit(1)

print(ssh.bits)  # 768
print(ssh.hash_md5())  # 56:84:1e:90:08:3b:60:c7:29:70:5f:5e:25:a6:3b:86
print(ssh.hash_sha256())  # SHA256:xk3IEJIdIoR9MmSRXTP98rjDdZocmXJje/28ohMQEwM
print(ssh.hash_sha512())  # SHA512:1C3lNBhjpDVQe39hnyy+xvlZYU3IPwzqK1rVneGavy6O3/ebjEQSFvmeWoyMTplIanmUK1hmr9nA8Skmj516HA
print(ssh.comment)  # ojar@ojar-laptop
print(ssh.options_raw)  # None (string of optional options at the beginning of public key)
print(ssh.options)  # None (options as a dictionary, parsed and validated)

安装:pip install sshpubkeys 或者  下载源码 python setup.py install

https://pypi.python.org/pypi/sshpubkeys/

https://github.com/ojarva/python-sshpubkeys

 

离线安装依赖包:pycrypto 和 ecdsa

安装pycrypto 出现问题:

F:\pip_package>easy_install pycrypto-2.6.1.tar.gz
Processing pycrypto-2.6.1.tar.gz
Writing c:\users\xxx\appdata\local\temp\easy_install-vny87z\pycrypto-2.6.1\
setup.cfg
Running pycrypto-2.6.1\setup.py -q bdist_egg --dist-dir c:\users\xxx\appdat
a\local\temp\easy_install-vny87z\pycrypto-2.6.1\egg-dist-tmp-8zzci9
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.

error: Setup script exited with error: Microsoft Visual C++ 9.0 is required. Get
it from http://aka.ms/vcpython27

根据提示下载安装Microsoft Visual C++ Compiler for Python 2.7 解决

 

转载于:https://www.cnblogs.com/9-fingers-zen/p/7641351.html

你可能感兴趣的:(Python实现ssh公钥的本地验证)