]> arthur.barton.de Git - bup.git/blob - lib/bup/ssh.py
Replace atoi with explicit checks; rm unused atof
[bup.git] / lib / bup / ssh.py
1 """SSH connection.
2 Connect to a remote host via SSH and execute a command on the host.
3 """
4
5 from __future__ import absolute_import, print_function
6 import sys, os, re, subprocess
7
8 from bup import helpers, path
9 from bup.compat import environ
10
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]
16     else:
17         buglvl = int(environ.get(b'BUP_DEBUG', 0))
18         force_tty = int(environ.get(b'BUP_FORCE_TTY', 0))
19         tty_width = environ.get(b'BUP_TTY_WIDTH', None)
20         if tty_width is not None:
21             tty_width = b'BUP_TTY_WIDTH=%d' % int(tty_width)
22         else:
23             tty_width = b''
24         cmd = b"""
25                    sh -c 'BUP_DEBUG=%d BUP_FORCE_TTY=%d %s bup %s'
26                """ % (buglvl, force_tty, tty_width, subcmd)
27         argv = [b'ssh']
28         if port:
29             argv.extend((b'-p', port))
30         argv.extend((rhost, b'--', cmd.strip()))
31         #helpers.log('argv is: %r\n' % argv)
32     if sys.version_info[0] < 3:
33         return subprocess.Popen(argv,
34                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
35                                 stderr=stderr,
36                                 preexec_fn=lambda: os.setsid())
37     else:
38         return subprocess.Popen(argv,
39                                 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
40                                 stderr=stderr,
41                                 start_new_session=True)