-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the same SSH connection for main thread and completer thread
- Loading branch information
Showing
8 changed files
with
115 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .client import get_config_hosts, create_ssh_client, SSHException, read_config_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
A very thin wrapper around paramiko, mostly to keep all SSH-related functionality in one place | ||
""" | ||
from io import open | ||
|
||
try: | ||
import paramiko | ||
except ImportError: | ||
from mycli.packages.paramiko_stub import paramiko | ||
|
||
|
||
class SSHException(Exception): | ||
pass | ||
|
||
|
||
def get_config_hosts(config_path): | ||
config = read_config_file(config_path) | ||
return { | ||
host: config.lookup(host).get("hostname") for host in config.get_hostnames() | ||
} | ||
|
||
|
||
def create_ssh_client(ssh_host, ssh_port, ssh_user, ssh_password=None, ssh_key_filename=None) -> paramiko.SSHClient: | ||
client = paramiko.SSHClient() | ||
client.load_system_host_keys() | ||
client.set_missing_host_key_policy(paramiko.WarningPolicy()) | ||
client.connect( | ||
ssh_host, ssh_port, ssh_user, password=ssh_password, key_filename=ssh_key_filename | ||
) | ||
return client | ||
|
||
|
||
def read_config_file(config_path) -> paramiko.SSHConfig: | ||
ssh_config = paramiko.config.SSHConfig() | ||
try: | ||
with open(config_path) as f: | ||
ssh_config.parse(f) | ||
# Paramiko prior to version 2.7 raises Exception on parse errors. | ||
# In 2.7 it has become paramiko.ssh_exception.SSHException, | ||
# but let's catch everything for compatibility | ||
except Exception as err: | ||
raise SSHException( | ||
f"Could not parse SSH configuration file {config_path}:\n{err} ", | ||
) | ||
except FileNotFoundError as e: | ||
raise SSHException(str(e)) | ||
return ssh_config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.