-def socat(local_socket_name, remote_socket_name,
- host, user,
- port = None,
- agent = True,
- sudo = False,
- identity = None,
- server_key = None,
- env = None,
- tty = False,
- connect_timeout = 30,
- retry = 3,
- strict_host_checking = True):
- """
- Executes a remote command, returns ((stdout,stderr),process)
- """
-
- tmp_known_hosts = None
- hostip = gethostbyname(host)
-
-
- args = ["socat"]
- args.append("UNIX-LISTEN:%s,unlink-early,fork" % local_socket_name)
-
- ssh_args = ['ssh', '-C',
- # Don't bother with localhost. Makes test easier
- '-o', 'NoHostAuthenticationForLocalhost=yes',
- '-o', 'ConnectTimeout=%d' % (int(connect_timeout),),
- '-o', 'ConnectionAttempts=3',
- '-o', 'ServerAliveInterval=30',
- '-o', 'TCPKeepAlive=yes',
- '-l', user, hostip or host]
-
- if not strict_host_checking:
- # Do not check for Host key. Unsafe.
- ssh_args.extend(['-o', 'StrictHostKeyChecking=no'])
-
- if agent:
- ssh_args.append('-A')
-
- if port:
- ssh_args.append('-p%d' % port)
-
- if identity:
- ssh_args.extend(('-i', identity))
-
- if tty:
- ssh_args.append('-t')
- ssh_args.append('-t')
-
- if server_key:
- # Create a temporary server key file
- tmp_known_hosts = make_server_key_args(server_key, host, port)
- ssh_args.extend(['-o', 'UserKnownHostsFile=%s' % (tmp_known_hosts.name,)])
-
- ssh_cmd = " ".join(ssh_args)
-
- exec_cmd = "EXEC:'%s socat STDIO UNIX-CONNECT\:%s'" % (ssh_cmd,
- remote_socket_name)
-
- args.append(exec_cmd)
-
- log_msg = " socat - host %s - command %s " % (host, " ".join(args))
-
- return _retry_rexec(args, log_msg,
- stdout = None,
- stdin = None,
- stderr = None,
- env = env,
- retry = retry,
- tmp_known_hosts = tmp_known_hosts,
- blocking = False)
-