-
Notifications
You must be signed in to change notification settings - Fork 655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse ssh connection #869
Open
pasenor
wants to merge
7
commits into
main
Choose a base branch
from
reuse-ssh-connection
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Reuse ssh connection #869
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
456f86b
Use the same SSH connection for main thread and completer thread
pasenor 3db1e2d
handle errors thrown by the SSH config reader
pasenor 57b7520
Merge remote-tracking branch 'origin' into reuse-ssh-connection
pasenor 664e62c
fix error handling
pasenor deff68f
Merge branch 'master' into reuse-ssh-connection
pasenor d9c604d
added ssh connection logs
pasenor 6392029
Merge branch 'master' into reuse-ssh-connection
pasenor File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,57 @@ | ||
"""A very thin wrapper around paramiko, mostly to keep all SSH-related | ||
functionality in one place.""" | ||
from io import open | ||
import logging | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
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()) | ||
_logger.debug( | ||
f'Connecting to ssh server with \n' | ||
' host = {ssh_host}\n' | ||
' port = {ssh_port}\n' | ||
' user = {ssh_user}\n' | ||
' password = {ssh_password}\n' | ||
' key_filename = {ssh_key_filename}\n' | ||
) | ||
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) | ||
except FileNotFoundError as e: | ||
raise SSHException(str(e)) | ||
# 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
raise SSHException( | ||
f"Could not parse SSH configuration file {config_path}:\n{err} ", | ||
) | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: the list_ssh_config feature is unreliable if the SSH config file uses certain features such as Match. Listing the hosts is not really in the scope of a tool such as mycli, and we ought to consider removing it.