2

I'm using Paramiko in order to execute a single or a multiple commands and get its output.

Since Paramiko doesn't allow executing multiple commands on the same channel session I'm concatenating each command from my command list and executing it in a single line, but the output can be a whole large output text depending on the commands so it's difficult to differentiate which output is for each command.

ssh.exec_command("pwd ls- l cd / ls -l")

I want to have something like:

command_output = [('pwd','output_for_pwd'),('ls -l','output_for_ls'), ... ]

to work easier with every command output.

Is there a way to do it without changing the Paramiko library?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
maudev
  • 974
  • 1
  • 14
  • 32

2 Answers2

1

The only solution is (as @Barmar already suggested) to insert unique separator between individual commands. Like:

pwd && echo "end-of-pwd" && cd /foo && echo "end-of-cd" && ls -l && echo "end-of-ls"

And then look for the unique string in the output.


Though imo, it is much better to simply separate the commands into individual exec_command calls. Though I do not really think that you need to execute multiple commands in a row often. Usually you only need something like, cd or set, and these commands do not really output anything.

Like:

  1. pwd
  2. ls -la /foo (or cd /foo && ls -la)

For a similar questions, see:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

I used to do this for sending commands in ssh and telnet, you can capture the output with each command and try.

cmd = ['pwd', 'ls - lrt', 'exit']
cmd_output =[]
for cmd in cmd:
    tn.write(cmd)
    tn.write("\r\n")
    out = tn.read_until('#')
    cmd_output.append((cmd,out))
    print out
Barmar
  • 741,623
  • 53
  • 500
  • 612
Ajith
  • 59
  • 1
  • 1
  • 7