Ejemplo n.º 1
0
def connection_sharing_args(kitty_pid: int) -> List[str]:
    rd = runtime_dir()
    # Bloody OpenSSH generates a 40 char hash and in creating the socket
    # appends a 27 char temp suffix to it. Socket max path length is approx
    # ~104 chars. macOS has no system runtime dir so we use a cache dir in
    # /Users/WHY_DOES_ANYONE_USE_MACOS/Library/Caches/APPLE_ARE_IDIOTIC
    if len(rd) > 35 and os.path.isdir('/tmp'):
        idiotic_design = f'/tmp/kssh-rdir-{os.getuid()}'
        try:
            os.symlink(rd, idiotic_design)
        except FileExistsError:
            try:
                dest = os.readlink(idiotic_design)
            except OSError as e:
                raise ValueError(f'The {idiotic_design} symlink could not be created as something with that name exists already') from e
            else:
                if dest != rd:
                    with tempfile.TemporaryDirectory(dir='/tmp') as tdir:
                        tlink = os.path.join(tdir, 'sigh')
                        os.symlink(rd, tlink)
                        os.rename(tlink, idiotic_design)
        rd = idiotic_design

    cp = os.path.join(rd, ssh_control_master_template.format(kitty_pid=kitty_pid, ssh_placeholder='%C'))
    ans: List[str] = [
        '-o', 'ControlMaster=auto',
        '-o', f'ControlPath={cp}',
        '-o', 'ControlPersist=yes',
        '-o', 'ServerAliveInterval=60',
        '-o', 'ServerAliveCountMax=5',
        '-o', 'TCPKeepAlive=no',
    ]
    return ans
Ejemplo n.º 2
0
    def test_ssh_connection_data(self):
        def t(cmdline,
              binary='ssh',
              host='main',
              port=None,
              identity_file='',
              extra_args=()):
            if identity_file:
                identity_file = os.path.abspath(identity_file)
            en = set(f'{x[0]}' for x in extra_args)
            q = get_connection_data(cmdline.split(), extra_args=en)
            self.ae(
                q,
                SSHConnectionData(binary, host, port, identity_file,
                                  extra_args))

        t('ssh main')
        t('ssh un@ip -i ident -p34',
          host='un@ip',
          port=34,
          identity_file='ident')
        t('ssh un@ip -iident -p34',
          host='un@ip',
          port=34,
          identity_file='ident')
        t('ssh -p 33 main', port=33)
        t('ssh -p 34 ssh://un@ip:33/', host='un@ip', port=34)
        t('ssh --kitten=one -p 12 --kitten two -ix main',
          identity_file='x',
          port=12,
          extra_args=(('--kitten', 'one'), ('--kitten', 'two')))
        self.assertTrue(runtime_dir())