本文整理汇总了Python中paramiko.RSAKey方法的典型用法代码示例。如果您正苦于以下问题:Python paramiko.RSAKey方法的具体用法?Python paramiko.RSAKey怎么用?Python paramiko.RSAKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类paramiko
的用法示例。
在下文中一共展示了paramiko.RSAKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def validate_key(key_path):
""" Validate a key
:param key_path: path to a key to use for authentication
:type key_path: str
:return: key object used for authentication
:rtype: paramiko.RSAKey
"""
key_path = os.path.expanduser(key_path)
if not os.path.isfile(key_path):
return False
return paramiko.RSAKey.from_private_key_file(key_path)
示例2: get_authorized_keys
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def get_authorized_keys(path):
keys = []
with open(path) as fp:
for line in fp:
flds = line.split(' ')
if len(flds) < 2: continue
if flds[0] == 'ssh-rsa':
f = paramiko.RSAKey
elif flds[0] == 'ssh-dss':
f = paramiko.DSSKey
elif flds[0].startswith('ecdsa-'):
f = paramiko.ECDSAKey
else:
continue
data = decodebytes(flds[1].encode('ascii'))
keys.append(f(data=data))
return keys
# run_server
示例3: get_pkey_obj
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def get_pkey_obj(cls, privatekey, password, filename):
bpass = to_bytes(password) if password else None
pkey = cls.get_specific_pkey(paramiko.RSAKey, privatekey, bpass)\
or cls.get_specific_pkey(paramiko.DSSKey, privatekey, bpass)\
or cls.get_specific_pkey(paramiko.ECDSAKey, privatekey, bpass)\
or cls.get_specific_pkey(paramiko.Ed25519Key, privatekey, bpass)
if not pkey:
if not password:
error = 'Invalid private key: {}'.format(filename)
else:
error = (
'Wrong password {!r} for decrypting the private key.'
) .format(password)
raise InvalidValueError(error)
return pkey
示例4: ssh_key_gen
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def ssh_key_gen(length=2048, type='rsa', password=None, username='jumpserver', hostname=None):
"""Generate user ssh private and public key
Use paramiko RSAKey generate it.
:return private key str and public key str
"""
if hostname is None:
hostname = os.uname()[1]
f = StringIO()
try:
if type == 'rsa':
private_key_obj = paramiko.RSAKey.generate(length)
elif type == 'dsa':
private_key_obj = paramiko.DSSKey.generate(length)
else:
raise IOError('SSH private key must be `rsa` or `dsa`')
private_key_obj.write_private_key(f, password=password)
private_key = f.getvalue()
public_key = ssh_pubkey_gen(private_key_obj, username=username, hostname=hostname)
return private_key, public_key
except IOError:
raise IOError('These is error when generate ssh key.')
示例5: _check_deserialize_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def _check_deserialize_key(key):
res = None
if isinstance(key, paramiko.RSAKey):
LOG.trace("Key is already in the proper format.")
res = key
elif type(key) is str:
LOG.trace("Deserializing PEM-encoded private key.")
res = utils.deserialize_key(
key, CONF.serialization.temp_keypair_password)
else:
raise exception.CoriolisException(
"Private key must be either a PEM-encoded string or "
"a paramiko.RSAKey instance. Got type '%s'." % (
type(key)))
return res
示例6: test_7_banner_timeout
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def test_7_banner_timeout(self):
"""
verify that the SSHClient has a configurable banner timeout.
"""
# Start the thread with a 1 second wait.
threading.Thread(target=self._run, kwargs={'delay': 1}).start()
host_key = paramiko.RSAKey.from_private_key_file(test_path('test_rsa.key'))
public_host_key = paramiko.RSAKey(data=host_key.asbytes())
self.tc = paramiko.SSHClient()
self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port), 'ssh-rsa', public_host_key)
# Connect with a half second banner timeout.
kwargs = dict(self.connect_kwargs, banner_timeout=0.5)
self.assertRaises(
paramiko.SSHException,
self.tc.connect,
**kwargs
)
示例7: connect
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def connect(self, host, user, password=None, ssh_key=None, port=22, timeout=30,
term='xterm', pty_width=80, pty_height=24):
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if ssh_key:
key = get_key_obj(paramiko.RSAKey, pkey_obj=ssh_key, password=password) or \
get_key_obj(paramiko.DSSKey, pkey_obj=ssh_key, password=password) or \
get_key_obj(paramiko.ECDSAKey, pkey_obj=ssh_key, password=password) or \
get_key_obj(paramiko.Ed25519Key, pkey_obj=ssh_key, password=password)
ssh_client.connect(username=user, hostname=host, port=port, pkey=key, timeout=timeout)
else:
ssh_client.connect(username=user, password=password, hostname=host, port=port, timeout=timeout)
transport = ssh_client.get_transport()
self.channel = transport.open_session()
self.channel.get_pty(term=term, width=pty_width, height=pty_height)
self.channel.invoke_shell()
for i in range(2):
recv = self.channel.recv(1024).decode('utf-8')
self.message['status'] = 0
self.message['message'] = recv
message = json.dumps(self.message)
self.websocker.send(message)
except socket.timeout:
self.message['status'] = 1
self.message['message'] = 'ssh 连接超时'
message = json.dumps(self.message)
self.websocker.send(message)
self.close()
except:
self.close()
示例8: get_pkey
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def get_pkey(self, privatekey, password):
password = password.encode('utf-8') if password else None
pkey = self.get_specific_pkey(paramiko.RSAKey, privatekey, password)\
or self.get_specific_pkey(paramiko.DSSKey, privatekey, password)\
or self.get_specific_pkey(paramiko.ECDSAKey, privatekey, password)\
or self.get_specific_pkey(paramiko.Ed25519Key, privatekey,
password)
if not pkey:
raise ValueError('Not a valid private key file or '
'wrong password for decrypting the private key.')
return pkey
示例9: test_get_pkey_obj_with_plain_rsa_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def test_get_pkey_obj_with_plain_rsa_key(self):
pk = self.get_pk_obj('test_rsa.key')
self.assertIsInstance(pk.get_pkey_obj(), paramiko.RSAKey)
示例10: test_get_pkey_obj_with_encrypted_rsa_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def test_get_pkey_obj_with_encrypted_rsa_key(self):
fname = 'test_rsa_password.key'
password = 'television'
self._test_with_encrypted_key(fname, password, paramiko.RSAKey)
示例11: test_get_pkey_obj_with_encrypted_new_rsa_key
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def test_get_pkey_obj_with_encrypted_new_rsa_key(self):
fname = 'test_new_rsa_password.key'
password = '123456'
self._test_with_encrypted_key(fname, password, paramiko.RSAKey)
示例12: __init__
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def __init__(self, server, client_conn):
self.server = server
self.thread = None
self.command_queues = {}
client, _ = client_conn
self.transport = t = paramiko.Transport(client)
t.add_server_key(paramiko.RSAKey(filename=SERVER_KEY_PATH))
t.set_subsystem_handler("sftp", sftp.SFTPServer)
示例13: add_user
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def add_user(self, uid, private_key_path, keytype="ssh-rsa"):
if keytype == "ssh-rsa":
key = paramiko.RSAKey.from_private_key_file(private_key_path)
elif keytype == "ssh-dss":
key = paramiko.DSSKey.from_private_key_file(private_key_path)
elif keytype in paramiko.ECDSAKey.supported_key_format_identifiers():
key = paramiko.ECDSAKey.from_private_key_file(private_key_path)
elif keytype == "ssh-ed25519":
key = paramiko.Ed25519Key.from_private_key_file(private_key_path)
else:
raise Exception("Unable to handle key of type {}".format(keytype))
self._users[uid] = (private_key_path, key)
示例14: client
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def client(self, uid):
private_key_path, _ = self._users[uid]
c = paramiko.SSHClient()
host_keys = c.get_host_keys()
key = paramiko.RSAKey.from_private_key_file(SERVER_KEY_PATH)
host_keys.add(self.host, "ssh-rsa", key)
host_keys.add("[%s]:%d" % (self.host, self.port), "ssh-rsa", key)
c.set_missing_host_key_policy(paramiko.RejectPolicy())
c.connect(hostname=self.host,
port=self.port,
username=uid,
key_filename=private_key_path,
allow_agent=False,
look_for_keys=False)
return c
示例15: load_keyfile
# 需要导入模块: import paramiko [as 别名]
# 或者: from paramiko import RSAKey [as 别名]
def load_keyfile(keyfile):
for cls in (paramiko.RSAKey, paramiko.DSSKey, paramiko.ECDSAKey):
try:
return cls.from_private_key_file(keyfile)
except paramiko.SSHException:
pass
else:
raise