2 Connect to a remote host via SSH and execute a command on the host.
5 from __future__ import absolute_import, print_function
6 import sys, os, re, subprocess
8 from bup import helpers, path
9 from bup.compat import environ
11 def connect(rhost, port, subcmd, stderr=None):
12 """Connect to 'rhost' and execute the bup subcommand 'subcmd' on it."""
13 assert not re.search(br'[^\w-]', subcmd)
14 if rhost is None or rhost == b'-':
15 argv = [path.exe(), subcmd]
17 buglvl = helpers.atoi(environ.get(b'BUP_DEBUG'))
18 force_tty = helpers.atoi(environ.get(b'BUP_FORCE_TTY'))
20 sh -c 'BUP_DEBUG=%d BUP_FORCE_TTY=%d bup %s'
21 """ % (buglvl, force_tty, subcmd)
24 argv.extend((b'-p', port))
25 argv.extend((rhost, b'--', cmd.strip()))
26 #helpers.log('argv is: %r\n' % argv)
27 if sys.version_info[0] < 3:
28 return subprocess.Popen(argv,
29 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
31 preexec_fn=lambda: os.setsid())
33 return subprocess.Popen(argv,
34 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
36 start_new_session=True)