本文介绍了如何使用 paramiko.RSAKey.from_private_key()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

知道如何使用 paramiko.RSAKey.from_private_key() 函数吗?

我知道有一个 from_private_key_file(),但我有兴趣使用函数来解析私钥(如下所示)并将该私钥用于 SSHClient.

私钥(示例):

----- BEGIN RSA私钥----- \ nMIICXgIBAAKCAIEAmfgmlY95SHXhCeBNdkhSrsG4JVbqyew845yoZRX3wcS2/DOZ \ niVQxgx0aiOwLi +/Rnkb3PLUIwoxb/的LoD/W0YMS6/NSUMt + LDH + zsjeNF2iq4rDzU \ nwDSqi27q/8U/egrK7H + 9HNKEVXb/87utAAm3VTM9KqKaK3VuVFrNrnsDSuECAwEA \ nAQKCAIBZn3y2KiGq8BLiMNJmO4sFdnW+ Jm3cw8pdo17SGItzGxJ5iX3ePkfjzhkY \ nAm5mMl6OBzj6 + VX0CMeywIR6C/q8HwDYSmZcuU5v76/DoW5bI6xkPrroqEz6aRE5 \ NYN + 2hf65RD3eoPATsdrP/kxiKjZg9uG9LhgIXyVwYFs1RcqewQJBAMCVJlEYXRio \ neynUtyES9HNmUGUqHKmri1FZfO56/mFdG5ZXsKE48qURCAGVxI + goGQ4vtJIXB2J \ nyTEr + 5qYtE0CQQDMq9/iigk + XDOa9xGCbwxbLGdPawaEivezMVdPqVzH971L6kZ8 \ nhEnev1DqujgGCyR + QYPW1ZCXH05FY9CqWwrlAkATzYJyJlI0XebER2ZJVVyjnSq5 \ nLFpkLAqYY95P23/a3SsgC4ZTHbr9tEGhgBgFONwlUhx1HRGzy95PWxl1LSylAkBk \ nwP93v8gJIM5urM27zfrhLxy0ZdVRji + d0N5QYuk/r19KbcvBJEZRFxE4W ++ UWgve \ n81V5fqytGEYptpdUJXlZAkEArxZDiT1HXXGciIgzZbh53McogPCGHiKOOPSjpM41 \npneDFVvwgezCWoDauxNDzu7Nl55qPJsmvfKZ+SKvCajrhQw==\n-----结束RSA私钥-----\n

我想运行的代码:

import paramikossh = paramiko.SSHClient()# 当我的private_key(如上所示)在字符串中时,如何传入private_key?mykey = paramiko.RSAKey.from_private_key(private_key)ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)

非常感谢.

解决方案

Lev 的方法对我有用:

>>>进口paramiko>>>f = open('/path/to/key.pem','r')>>>s = f.read()>>>导入字符串IO>>>密钥文件 = StringIO.StringIO(s)>>>mykey = paramiko.RSAKey.from_private_key(keyfile)>>>ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())>>>ssh.connect('myserver.compute-1.amazonaws.com', username='ubuntu', pkey=mykey)>>>标准输入,标准输出,标准错误 = ssh.exec_command('uptime')>>>标准输出.readlines()[' 19:21:10 up 24 天,42 分钟,1 个用户,平均负载:0.14, 0.06, 0.05\n']

Any idea how I can use the paramiko.RSAKey.from_private_key() function?

I know there is a from_private_key_file(), but I'm interested in using a function to parse a private key (like below) and use that private key for SSHClient.

Private key (sample):

-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKCAIEAmfgmlY95SHXhCeBNdkhSrsG4JVbqyew845yoZRX3wcS2/doz\niVQxgx0aiOwLi+/Rnkb3PLUIwoxb/LoD/W0YMS6/NSUMt+LdH+zsjeNF2iq4rDzU\nwDSqi27q/8u/egrK7H+9HNKEVXb/87utAAm3VTM9KqKaK3VuVFrNrnsDSuECAwEA\nAQKCAIBZn3y2KiGq8BLiMNJmO4sFdnW+Jm3cw8pdo17SGItzGxJ5iX3ePkfjzhkY\nAm5mMl6OBzj6+VX0CMeywIR6C/q8HwDYSmZcuU5v76/DoW5bI6xkPrroqEz6aRE5\nyN+2hf65RD3eoPATsdrP/kxiKjZg9uG9LhgIXyVwYFs1RcqewQJBAMCVJlEYXRio\neynUtyES9HNmUGUqHKmri1FZfO56/mFdG5ZXsKE48qURCAGVxI+goGQ4vtJIXB2J\nyTEr+5qYtE0CQQDMq9/iigk+XDOa9xGCbwxbLGdPawaEivezMVdPqVzH971L6kZ8\nhEnev1DqujgGCyR+QYPW1ZCXH05FY9CqWwrlAkATzYJyJlI0XebER2ZJVVyjnSq5\nLFpkLAqYY95P23/a3SsgC4ZTHbr9tEGhgBgFONwlUhx1HRGzy95PWxl1LSylAkBk\nwP93v8gJIM5urM27zfrhLxy0ZdVRji+d0N5QYuk/r19KbcvBJEZRFxE4W++UWgve\n81V5fqytGEYptpdUJXlZAkEArxZDiT1HXXGciIgzZbh53McogPCGHiKOOPSjpM41\npneDFVvwgezCWoDauxNDzu7Nl55qPJsmvfKZ+SKvCajrhQw==\n-----END RSA PRIVATE KEY-----\n

Code I wanted to run:

import paramiko
ssh = paramiko.SSHClient()
# how do I pass in the private_key, when my private_key (shown above) is in string?
mykey = paramiko.RSAKey.from_private_key(private_key)
ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)

Many thanks.

解决方案

Lev's method worked for me:

>>> import paramiko
>>> f = open('/path/to/key.pem','r')
>>> s = f.read()
>>> import StringIO
>>> keyfile = StringIO.StringIO(s)
>>> mykey = paramiko.RSAKey.from_private_key(keyfile)
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('myserver.compute-1.amazonaws.com', username='ubuntu', pkey=mykey)
>>> stdin, stdout, stderr = ssh.exec_command('uptime')
>>> stdout.readlines()
[' 19:21:10 up 24 days, 42 min,  1 user,  load average: 0.14, 0.06, 0.05\n']

这篇关于如何使用 paramiko.RSAKey.from_private_key()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:28