0

I am doing ssh using the subprocess run command. I am aware of popen through https://www.cyberciti.biz/faq/python-run-external-command-and-get-output/ - it works.

I am trying run command at the moment. I am getting an error when I use the run command: Pseudo-terminal will not be allocated because stdin is not a terminal.

>>> import subprocess
>>> cmd = ["ssh", "-i", "sshkey.pem", "user@111.1.1.1"]
>>> ip = 'exit'.encode('utf-8')
>>> result = subprocess.run(cmd, stdout=subprocess.PIPE, input=ip)
>>> # I want to access the ssh terminal here and run a few commands like  ip in the ssh terminal

My basic goal is to:

  • Run a few commands like exit (the ip variable) in the SSH terminal using code. Which arguments can be used for SSH terminal input and output in the SSH pseudo terminal, IF I can access one in .run() command. (I am looking at this Running shell command and capturing the output)

  • Run a few commands like exit in the SSH terminal using shell with (a.) possibly be able to stream output of the SSH terminal and (b.) possibly be able to stream inputs into the SSH terminal.

UPDATE:

Able to get into the shell using the .popen() function. Unable to run list of commands from code automatically even using .communicate() function even with .popen() using input argument, not just .run() function.

cmd = ["ssh", "-i", "sshkey.pem", "user@111.1.1.1"]

r = subprocess.run(cmd, stdout=subprocess.PIPE, input=ip)
# does not work with ip
# Unable to run list of commands into the process automatically via commands

c = subprocess.call(cmd, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Unable to run list of commands into the process automatically via commands

p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate(input='exit'.encode())[0]
# Able to get into the shell of the subprocess
# but unable to run list of commands into the shell automatically, output doesnot work using `input` options
Gary
  • 2,293
  • 2
  • 25
  • 47
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. – furas Nov 15 '21 at 13:57
  • code work correctly if I use `bash` instead of `ssh` - so all problem makes `ssh`. It may have problem to access `stdin`. – furas Nov 15 '21 at 14:13
  • @furas Pseudo-terminal will not be allocated because stdin is not a terminal – Gary Nov 16 '21 at 14:03
  • @furas yes, even I was expecting that. what should be the stdin - subprocess.PIPE? It did not work for both showing the shell while running the run() function nor getting a shell allocated through code (that will get a set of commands in a list or bash script running) – Gary Nov 16 '21 at 14:06
  • I don't know what it should be - I use module [fabric](https://www.fabfile.org/) (which uses module [paramiko](https://www.paramiko.org/)) to run `ssh` - `result = fabric.Connection('pi@raspberry').run('ls', hide=True)` and `print( result.stdout )`. – furas Nov 16 '21 at 15:05
  • for sending many commands in one session I would use [pexpect](https://pexpect.readthedocs.io/en/stable/api/pexpect.html) with `ssh` or even [pxssh](https://pexpect.readthedocs.io/en/stable/api/pxssh.html) – furas Nov 16 '21 at 15:16
  • @furas I have seen paramiko. It is `very` good. I dropped it to avoid an external dependency. Let me have a look at fabric. Is it possible for me to use it without an external dep? Let me have a look at the code and see if I can replicate it in a simpler way in my code. I am intending to use `run` or `popen` of which `popen` worked. But I am unable to stream stdin and stdout for both `popen` and `run`. I am trying to find the args options to allow that and access that to the `result`ing object in my case – Gary Nov 21 '21 at 06:20
  • @furas updated the commands for `popen` and `run` in the code above. Unable to run list of command codes automatically using code from both popen and run. – Gary Nov 21 '21 at 06:38
  • farbric use paramiko. You think that with `popen` it will be `simpler way` but it will not. – furas Nov 21 '21 at 14:49
  • @furas true. It is not about `ssh` actually. If I use a different command with `popen` how do I add inputs to stdin programmatically with `run` or with `popen`? – Gary Nov 22 '21 at 15:09
  • @furas say I run a telnet command, what options do I use to write to shell programmatically in `run` of subprocess or `popen` of subprocess – Gary Nov 22 '21 at 16:25

0 Answers0