0

I am trying to port some simple scripts that I have in tcl to python.

Using tcl/expect, we can see every executed command on the standard output. For example,

spawn ssh admin@$IP
send "ls\r"

would yield by default an output like this:

ssh admin@10.10.10.10
ls
....

In python, only way I saw was to decode the child.before or after outputs. Is there a way python can output everything it runs to the console or a file ?

This is what I am doing now:

#!/usr/bin/env python3
import pexpect
shellprompt = "] # "

child = pexpect.spawn('ssh admin@XYZ')
child.sendline('ls')
child.expect(shellprompt)
ls_out = child.before.decode()
print(ls_out)

This is run on a Linux machine, and doing ssh to a Linux machine

singleX
  • 375
  • 4
  • 13
  • @Grismar Added the commands that I am doing now in Python. I am expecting the 'ls' command as well as the output of it to show up on the console without doing a print – singleX Sep 29 '22 at 23:12
  • @Grismar Updated the code. child is the output of pexpect.spawn – singleX Sep 29 '22 at 23:14
  • You should perhaps read about [minimal, reproducible examples](https://stackoverflow.com/help/minimal-reproducible-example) - what is `pexpect`? (it's not the module name itself, since just `import pexpect` won't work) What is `shellprompt`? Are you running on Linux? (in which case `spawn` may work, while on Windows it may not) – Grismar Sep 29 '22 at 23:39
  • @Grismar pexpect is tagged. – SuperStormer Sep 30 '22 at 00:14
  • @SuperStormer yes, I'm aware, but since the code could run on either Windows or Linux, it won't work unless it's run on Linux (obviously, OP is ssh-ing into a Linux box, but the machine running the script would also have to be Linux). Hence the question, it's currently not a minimal, reproducible example. – Grismar Sep 30 '22 at 01:51
  • 1
    here's an example: https://stackoverflow.com/questions/36145276/ – sexpect - Expect for Shells Sep 30 '22 at 01:55
  • @Grismar I updated the question. But, "sexpect - Expect for Shells" gave me the right link. – singleX Sep 30 '22 at 02:40

0 Answers0