"The pseudo-terminal will not be allocated because stdin is not a terminal" when running ssh via python without paramiko

I am running ssh from Python without using an external library such as Paramiko. I have reasons for this, and not through an external library.

I basically do subprocess.Popen("ssh -t bla -- command")

The following message appears:

 Pseudo-terminal will not be allocated because stdin is not a terminal. 

The reason I run it with -t is because the remote command exits when I kill my python script.

When I try to use -t -t (to force it), I get the following message:

 tcgetattr: Inappropriate ioctl for device 

Is there a way to run my ssh command using -t via Python?

+4
source share
3 answers

It is safe to assume that you are asking the wrong question and that the right question might be something like "how can I make sure that the subprocess ends when my python program finishes"

You can do some clever unix stuff, for example, highlight pty and make sure your subprocess uses this as std in, etc., so that the ssh team thinks that it is talking to the terminal, not the channel, but this, probably not what you really want to do.

+2
source

To terminate the remote command when uninstalling the python script, you can (as an alternative to the -t option to ssh ) implement a workaround that uses the named pipe to convert "EOF to SIGHUP" when stdin sshd closes (see Error 396 - orphan sshd when there is no pty allocated ).

 # sample code in Bash # press ctrl-d for EOF ssh localhost ' TUBE=/tmp/myfifo.fifo rm -f "$TUBE" mkfifo "$TUBE" #exec 3<>"$TUBE" <"$TUBE" sleep 100 & appPID=$! # cf. "OpenSSH and non-blocking mode", # http://lists.mindrot.org/pipermail/openssh-unix-dev/2005-July/023090.html #cat >"$TUBE" #socat -u STDIN "PIPE:$TUBE" dd of="$TUBE" bs=1 2>/dev/null #while IFS="" read -r -n 1 char; do printf '%s' "$char"; done > "$TUBE" #kill -HUP -$appPID kill $appPID rm -f "$TUBE" ' 
+1
source

It looks like you are really asking for the inverse of the old nohup command; this means that the remote command responds to the HUP (hang) signal that is generated by the TTY driver (PTY or psuedo-tty) when the basic connection to it is closed.

Personally, I suspect that Paramiko will be much better than trying to manage this with the subprocess module. You can make Paramiko open the connection, select pty and execute its commands without any problems; and the code for using existing files of known_files and identities (private keys) is relatively simple.

This recipe Copy files via SSH using paramiko in ActiveState shows the basics of ssh known_hosts downloads using identifier files and interacting with any ssh agent you can work with. From there, what you are asking for should be simple:

You can also use the TwistedConch implementation of the SSH protocols. However, wrapping your head around a Twisted frame can be quite complicated.

Here is an example of a simple ssh server in Twisted with pty support: Twisted Conch in 60 seconds: PTY requests Here, the SO stream was sent a week before yours. The best way to run remote commands via ssh in Twisted? but that didn’t deal with pty issues. Looking at the docs for twisted.conch.ssh.session suggests that it has pty support; but lists it as "Undocumented."

Of course, you can even go with Pexpect , run the shell on your local ssh client and send a remote command through it. This most accurately reflects how you run the command from your own shell.

0
source

Source: https://habr.com/ru/post/1335175/


All Articles