|
| 1 | +"""Zowe Client Python SDK. |
| 2 | +
|
| 3 | +This program and the accompanying materials are made available under the terms of the |
| 4 | +Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 5 | +
|
| 6 | +https://www.eclipse.org/legal/epl-v20.html |
| 7 | +
|
| 8 | +SPDX-License-Identifier: EPL-2.0 |
| 9 | +
|
| 10 | +Copyright Contributors to the Zowe Project. |
| 11 | +""" |
| 12 | + |
| 13 | +from typing import Optional |
| 14 | +import paramiko |
| 15 | +from zowe.core_for_zowe_sdk import SdkApi |
| 16 | + |
| 17 | + |
| 18 | +class Uss(SdkApi): |
| 19 | + """ |
| 20 | + Class to interact with Unix System Services (USS) on z/OS via SSH. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + connection : dict |
| 25 | + A dictionary containing SSH connection details like hostname, username, password, and port. |
| 26 | + log : bool |
| 27 | + Flag to enable or disable logging. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__(self, connection: dict, log: bool = True): |
| 31 | + super().__init__(connection, "/zosmf/restuss", logger_name=__name__, log=log) |
| 32 | + self.connection = connection |
| 33 | + self.ssh_client = None |
| 34 | + |
| 35 | + def connect(self): |
| 36 | + """ |
| 37 | + Establish an SSH connection using the connection details. |
| 38 | + """ |
| 39 | + self.ssh_client = paramiko.SSHClient() |
| 40 | + self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 41 | + self.ssh_client.connect( |
| 42 | + hostname=self.connection["host"], |
| 43 | + username=self.connection["user"], |
| 44 | + password=self.connection.get("password"), |
| 45 | + port=self.connection.get("port", 22), |
| 46 | + ) |
| 47 | + |
| 48 | + def disconnect(self): |
| 49 | + """ |
| 50 | + Close the SSH connection. |
| 51 | + """ |
| 52 | + if self.ssh_client: |
| 53 | + self.ssh_client.close() |
| 54 | + |
| 55 | + def execute_command(self, command: str, cwd: Optional[str] = None): |
| 56 | + """ |
| 57 | + Execute a Unix command over SSH. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + command : str |
| 62 | + The command to execute. |
| 63 | + cwd : Optional[str] |
| 64 | + The working directory for the command. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + tuple |
| 69 | + A tuple of (stdout, stderr). |
| 70 | + """ |
| 71 | + if cwd: |
| 72 | + command = f"cd {cwd} && {command}" |
| 73 | + stdin, stdout, stderr = self.ssh_client.exec_command(command) |
| 74 | + return stdout.read().decode(), stderr.read().decode() |
0 commit comments