Change missing hostkey policy
This commit is contained in:
parent
9e90d620e6
commit
b957200deb
@ -14,6 +14,7 @@ Classes:
|
|||||||
# Import libraries
|
# Import libraries
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import warnings
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
import shlex
|
import shlex
|
||||||
@ -31,6 +32,8 @@ from dotenv import load_dotenv
|
|||||||
import paramiko
|
import paramiko
|
||||||
from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
|
from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
|
||||||
|
|
||||||
|
warnings.filterwarnings('error')
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from systemd import journal
|
from systemd import journal
|
||||||
@ -298,30 +301,42 @@ class Backup:
|
|||||||
|
|
||||||
def _ssh_connection(self):
|
def _ssh_connection(self):
|
||||||
ssh = paramiko.SSHClient()
|
ssh = paramiko.SSHClient()
|
||||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
ssh.load_system_host_keys()
|
||||||
|
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
|
||||||
|
|
||||||
agent = paramiko.Agent()
|
try:
|
||||||
agent_keys = agent.get_keys()
|
ssh.connect(self.host, username=self.username)
|
||||||
|
|
||||||
for key in agent_keys:
|
return ssh
|
||||||
try:
|
except UserWarning:
|
||||||
ssh.connect(self.host, username=self.username, pkey=key)
|
k = input(f'Unknown key for host {self.host}. Continue anyway? (Y/N) ')
|
||||||
return ssh
|
|
||||||
except paramiko.SSHException:
|
if k[0].upper() == 'Y':
|
||||||
pass
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
except paramiko.SSHException:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
ssh.connect(self.host, username=self.username)
|
||||||
|
|
||||||
|
return ssh
|
||||||
|
except paramiko.SSHException:
|
||||||
|
pass
|
||||||
|
|
||||||
pkey = None
|
pkey = None
|
||||||
password = None
|
password = None
|
||||||
|
|
||||||
if self.ssh_keyfile is None:
|
if self.ssh_keyfile is None:
|
||||||
logger.critical('Can\'t connect to the server. No key specified')
|
logger.critical('Can\'t connect to the server. No authentication method available')
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pkey = RSAKey.from_private_key_file(self.ssh_keyfile)
|
pkey = RSAKey.from_private_key_file(self.ssh_keyfile)
|
||||||
except paramiko.PasswordRequiredException:
|
except paramiko.PasswordRequiredException:
|
||||||
password = getpass()
|
password = getpass(f'Enter passwphrase for key \'{self.ssh_keyfile}\': ')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pkey = RSAKey.from_private_key_file(self.ssh_keyfile, password)
|
pkey = RSAKey.from_private_key_file(self.ssh_keyfile, password)
|
||||||
@ -357,8 +372,8 @@ class Backup:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
ssh.connect(self.host, username=self.username, pkey=pkey)
|
ssh.connect(self.host, username=self.username, pkey=pkey)
|
||||||
except paramiko.SSHException as e:
|
except paramiko.SSHException:
|
||||||
logger.error(e)
|
logger.critical('SSH connection to server failed')
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user