fix(ssh): log in with any key type, the key before the password
Only RSA keys were read, and a key was used only when no password was set; with the API password always present it never was. The key is now read as Ed25519, ECDSA or RSA and offered first, the password second. The rest of the issue (the API exec branch, errors read as empty output) belongs to the shared transport of NAPALM/napalm-device-types#35. Refs #17
This commit is contained in:
1 parent
5195cebfca
commit
f491f9f9ec
3 files changed
+105
-7
No files matched your search
@@ -343,17 +343,17 @@ class ProxmoxDriver(
|
||||
import paramiko
|
||||
|
||||
if self._ssh_client is None:
|
||||
ssh_pass = self._ssh_password or self.password
|
||||
# A key is tried first; paramiko falls back to the password.
|
||||
connect_kwargs: _JsonDict = {
|
||||
"username": self._ssh_username or self.username,
|
||||
"timeout": self.timeout,
|
||||
}
|
||||
if self._ssh_key and not ssh_pass:
|
||||
from io import StringIO as _StringIO
|
||||
|
||||
connect_kwargs["pkey"] = paramiko.RSAKey.from_private_key(_StringIO(self._ssh_key))
|
||||
else:
|
||||
connect_kwargs["password"] = ssh_pass
|
||||
password = self._ssh_password or self.password
|
||||
if password:
|
||||
connect_kwargs["password"] = password
|
||||
pkey = _load_private_key(self._ssh_key) if self._ssh_key else None
|
||||
if pkey is not None:
|
||||
connect_kwargs["pkey"] = pkey
|
||||
client = paramiko.SSHClient()
|
||||
try:
|
||||
HostKeyCheck(self.optional_args, self.hostname, 22).connect(
|
||||
@@ -511,3 +511,18 @@ class ProxmoxDriver(
|
||||
def cli(self, commands: list[str], encoding: str = "text") -> dict[str, str]:
|
||||
"""Execute a list of shell commands and return their outputs."""
|
||||
return {cmd: self._exec_ssh_command(cmd) for cmd in commands}
|
||||
|
||||
|
||||
def _load_private_key(text: str) -> Any: # paramiko.PKey; paramiko is imported lazily
|
||||
"""The SSH private key in *text*, whatever its type, or None when it cannot be read."""
|
||||
import io
|
||||
|
||||
import paramiko
|
||||
|
||||
for key_class in (paramiko.Ed25519Key, paramiko.ECDSAKey, paramiko.RSAKey):
|
||||
try:
|
||||
return key_class.from_private_key(io.StringIO(text))
|
||||
except (paramiko.SSHException, ValueError):
|
||||
continue
|
||||
logger.warning("The SSH private key could not be read; logging in with the password")
|
||||
return None
|
||||
Reference in new issue
Block a user