[Python] Unable to execute shell script using ssh

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    def upload_script(cluster_config):
    logger = default_logger
    script_path =“some_path”
    if not os.path.exists(script_path):
    logger.error("IPv6 script not found at %s", script_path)
    return
    for node in cluster_config.cluster_members:
    cvm_ip = getattr(node, "cvm_ipv6_address", node.cvm_ip)
    logger.info("Connecting to CVM: %s", cvm_ip)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    remote_path = "/home/nam/cluster/bin/ipv6_disable.sh"
    try:
    ssh.connect(hostname=cvm_ip, username="nam", password="namu")
    with SCPClient(ssh.get_transport()) as scp:
    scp.put(script_path, remote_path)
    logger.info("Script uploaded to %s", remote_path)
    ssh.exec_command(f"chmod +x {remote_path}")
    logger.info("Script permissions changed to executable")
    logger.info("Attempting SSH connection to %s", cvm_ip)
    ssh.connect(hostname=cvm_ip, username="nam", password="namu")
    stdin, stdout, stderr = ssh.exec_command(f"{remote_path}")
    logger.info(stdout.read().decode())
    exit_status = stdout.channel.recv_exit_status()
    if exit_status == 0:
    logger.info("Script executed successfully on %s", cvm_ip)
    else:
    logger.error("Script execution failed on %s:\n%s", cvm_ip, stderr.read().decode())
    except Exception as e:
    logger.info(stderr)
    logger.error("Failed to upload/execute on %s: %s", cvm_ip, str(e))
    finally:
    ssh.close().


    Why is my code failing with

    2025-05-21 16:12:28,884Z fd_tools.py:2049 INFO Script uploaded to /home/nam/ster/bin/ipv6_disable.sh
    2025-05-21 16:12:28,886Z sample.py:1304 ERROR Failed to upload IPV6 script.. ignoring
    Traceback (most recent call last):
    File "fd/fd_tools.py", line 2050, in upload_script
    File "/home/nam/fd/lib/py/vendor/paramiko/client.py", line 560, in exec_command
    chan = self._transport.open_session(timeout=timeout)
    File "/home/nam/fd/lib/py/vendor/paramiko/transport.py", line 994, in open_session
    return self.open_channel(
    File "/home/nam/fd/lib/py/vendor/paramiko/transport.py", line 1136, in open_channel
    raise e
    paramiko.ssh_exception.ChannelException: ChannelException(2, 'Connect failed')


    During handling of the above exception, another exception occurred:

    Continue reading...

Compartilhe esta Página