-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE-1190]: remote command executor for e2e (ssh) (#1191)
Signed-off-by: Dawid Korzepa <[email protected]>
- Loading branch information
1 parent
9a1b730
commit 5d21eab
Showing
3 changed files
with
52 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import paramiko | ||
from typing import Any, List, Tuple | ||
|
||
|
||
class SSHCommandExecutor: | ||
""" | ||
A class for executing SSH commands on a remote server. | ||
Args: | ||
ip_address (str): The IP address of the SSH server. | ||
username (str): The username for authentication. | ||
password (str): The password for authentication. | ||
""" | ||
|
||
def __init__(self, ip_address: str, username: str, password: str) -> None: | ||
""" | ||
Initializes the SSHCommandExecutor with the given IP address, username, and password. | ||
""" | ||
self.ip_address = ip_address | ||
self.username = username | ||
self.password = password | ||
|
||
def exec(self, command: str) -> Tuple[str, List[Any]]: | ||
""" | ||
Executes an SSH command on the remote server. | ||
Args | ||
command (str): The command to execute. | ||
Returns: | ||
str: The output of the executed command. | ||
list: A list of error messages, if any, from the executed command. | ||
""" | ||
ssh_client = paramiko.SSHClient() | ||
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | ||
ssh_client.connect( | ||
self.ip_address, username=self.username, password=self.password) | ||
|
||
logging.info(f"SSH connected, executing command: {command}") | ||
_, stdout, stderr = ssh_client.exec_command(command) | ||
output = stdout.read().decode().strip() | ||
error = stderr.readlines() | ||
|
||
ssh_client.close() | ||
|
||
if len(error) > 0: | ||
logging.error(f"SSH command {command} failed: {error}") | ||
|
||
return output, error |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ kubernetes==29.0.0 | |
tox==4.14.2 | ||
flake8==7.0.0 | ||
pylint==3.1.0 | ||
paramiko==3.4.0 |