1

I'm struggling to run mosh effectively within a python subprocess. I can spin up a mosh client and connect to a mosh server, but the process appears to hang.

example ssh command:

ssh -o SendEnv=ENVVAR -l username server_dns_name

example mosh command:

mosh --ssh="ssh -o SendEnv=ENVVAR -l username" server_dns_name

python invocation:

import subprocess

# command = "ssh -o SendEnv=ENVVAR -l username server_dns_name"
command = "mosh --ssh=\"ssh -o SendEnv=ENVVAR -l username\" server_dns_name"

proc = subprocess.run(command, env=os.environ, shell=True)

I expect: normal interaction with mosh.

I get: no way to interact, but a screen presents itself as if I'm connected.

What am I missing?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55
  • There's no reason to use `shell=True` here; pass the list `["mosh", "--ssh=ssh -o SendEnv=ENVVAR -l username", "server_dns_name"]` as the first argument instead. – chepner Aug 09 '18 at 19:17
  • A shell adds one more moving part than you would have without one -- extra complexity means more potential things that could be going wrong (f/e, any script named in an environment variable named `ENV` is executed during shell startup, *before* the shell gets around to invoking your copy of `mosh`). Not that that's a high likelihood source of your bug here, but the simpler the better. – Charles Duffy Aug 09 '18 at 21:14
  • BTW, `env=os.environ` is completely pointless -- the default if you don't pass `env` at all is to inherit the parent process's environment. – Charles Duffy Aug 09 '18 at 21:18
  • Is this a REPL session or a script? Does connecting the child's stdin, stdout and stderr explicitly to handles on `/dev/tty` (and closing the parent's copies first, if they are in fact directed there) have any effect? – Charles Duffy Aug 09 '18 at 21:19
  • (Another note -- if you don't need the `--ssh` argument to reproduce the issue, and can test that it reproduces without same, a proper [mcve] would take it out). – Charles Duffy Aug 09 '18 at 21:40
  • ...so, I can't reproduce this myself. ```python3.6 -c 'import subprocess, sys; command = ["mosh", sys.argv[1]]; proc = subprocess.run(command)' myhost``` behaves identically to running `mosh` directly. No difference when I add `"--ssh=ssh -o SendEnv=ENVVAR"` either. – Charles Duffy Aug 09 '18 at 21:43
  • (...as an aside, I strongly advise against using `shlex.split()` when you could instead be hardcoding an explicit argv -- like a shell, it's an abstraction layer that's adding complexity without any benefit for same; passing an explicit list is what the OS actually does under-the-hood when it invokes the `execve()` syscall, so if you want to have as much as control as possible over what's *actually* going on when you invoke a program, an explicit list is the way to go). – Charles Duffy Aug 09 '18 at 21:45
  • Without using shell=True, there's no issue. Can someone explain why shell=True is a problem? – Brian Bruggeman Aug 09 '18 at 22:17

0 Answers0