Interative shell paramiko how to read input like the exec_command

When using paramiko exec_command without the interactive shell, it's really easy to get the output.

However I need to use the interactive shell and it seems a pain to read the output of a single command. I couldn't find a demo or any example of this.

I tried using something like : Implement an interactive shell over ssh in Python using Paramiko?

But it's in really bad shape.

Upvotes: 2

Views: 908

Answers (2)

misha
misha

Reputation: 837

After using the solution given in the link, what do you mean by "it's in really bad shape"?

The output had some unnecessary characters so I removed them, please see the edited solution.

Upvotes: 0

Samuel
Samuel

Reputation: 3801

You don't have to use paramiko for it.

Just use paramiko to open passwordless ssh connection and then implement your method, using subporcess module.

For example:

def run_shell_remote_command(remote_host, remote_cmd):
        p = subprocess.Popen(['ssh', '-nx', remote_host, remote_cmd], stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if p.returncode != 0:
            raise RuntimeError("%r failed, status code %s stdout %r stderr %r" % (
                remote_cmd, p.returncode, stdout, stderr))
        return stdout.strip()  # This is the stdout from the shell command

Upvotes: 1

Related Questions