logger.log(level, msg)
-
if hasattr(os, "devnull"):
DEV_NULL = os.devnull
else:
return func(*p, **kw)
return rv
-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)
-
def rexec(command, host, user,
- port = None,
+ port = None,
+ gwuser = None,
+ gw = None,
agent = True,
sudo = False,
identity = None,
"""
tmp_known_hosts = None
- hostip = gethostbyname(host)
+ if not gw:
+ hostip = gethostbyname(host)
+ else: hostip = None
args = ['ssh', '-C',
# Don't bother with localhost. Makes test easier
'-o', 'ConnectionAttempts=3',
'-o', 'ServerAliveInterval=30',
'-o', 'TCPKeepAlive=yes',
+ '-o', 'Batchmode=yes',
'-l', user, hostip or host]
if persistent and openssh_has_persist():
# Do not check for Host key. Unsafe.
args.extend(['-o', 'StrictHostKeyChecking=no'])
+ if gw:
+ if gwuser:
+ proxycommand = 'ProxyCommand=ssh %s@%s -W %%h:%%p' % (gwuser, gw)
+ else:
+ proxycommand = 'ProxyCommand=ssh %%r@%s -W %%h:%%p' % gw
+ args.extend(['-o', proxycommand])
+
if agent:
args.append('-A')
args.append('-p%d' % port)
if identity:
+ identity = os.path.expanduser(identity)
args.extend(('-i', identity))
if tty:
blocking = blocking)
def rcopy(source, dest,
- port = None,
- agent = True,
+ port = None,
+ gwuser = None,
+ gw = None,
recursive = False,
identity = None,
server_key = None,
Source and destination should have the user and host encoded
as per scp specs.
- Source can be a list of files to copy to a single destination,
- in which case it is advised that the destination be a folder.
+ Source can be a list of files to copy to a single destination,
+ (in which case it is advised that the destination be a folder),
+ a single file in a string or a semi-colon separated list of files
+ in a string.
"""
-
+
# Parse destination as <user>@<server>:<path>
- if isinstance(dest, basestring) and ':' in dest:
+ if isinstance(dest, str) and ':' in dest:
remspec, path = dest.split(':',1)
- elif isinstance(source, basestring) and ':' in source:
+ elif isinstance(source, str) and ':' in source:
remspec, path = source.split(':',1)
else:
raise ValueError, "Both endpoints cannot be local"
if port:
args.append('-P%d' % port)
+ if gw:
+ if gwuser:
+ proxycommand = 'ProxyCommand=ssh %s@%s -W %%h:%%p' % (gwuser, gw)
+ else:
+ proxycommand = 'ProxyCommand=ssh %%r@%s -W %%h:%%p' % gw
+ args.extend(['-o', proxycommand])
+
if recursive:
args.append('-r')
if identity:
+ identity = os.path.expanduser(identity)
args.extend(('-i', identity))
if server_key:
if not strict_host_checking:
# Do not check for Host key. Unsafe.
args.extend(['-o', 'StrictHostKeyChecking=no'])
-
+
if isinstance(source, list):
args.extend(source)
else:
if openssh_has_persist():
args.extend([
'-o', 'ControlMaster=auto',
- '-o', 'ControlPath=%s' % (make_control_path(agent, False),)
+ '-o', 'ControlPath=%s' % (make_control_path(False, False),)
])
args.append(source)
- args.append(dest)
+ if isinstance(dest, list):
+ args.extend(dest)
+ else:
+ args.append(dest)
log_msg = " rcopy - host %s - command %s " % (host, " ".join(args))
host = None,
port = None,
user = None,
+ gwuser = None,
+ gw = None,
agent = None,
identity = None,
server_key = None,
host = host,
port = port,
user = user,
+ gwuser = gwuser,
+ gw = gw,
agent = agent,
identity = identity,
server_key = server_key,
host = None,
port = None,
user = None,
+ gwuser = None,
+ gw = None,
agent = None,
identity = None,
server_key = None):
host = host,
port = port,
user = user,
+ gwuser = gwuser,
+ gw = gw,
agent = agent,
identity = identity,
server_key = server_key
host = None,
port = None,
user = None,
+ gwuser = None,
+ gw = None,
agent = None,
identity = None,
server_key = None):
host = host,
port = port,
user = user,
+ gwuser = gwuser,
+ gw = gw,
agent = agent,
identity = identity,
server_key = server_key
host = None,
port = None,
user = None,
+ gwuser = None,
+ gw = None,
agent = None,
sudo = False,
identity = None,
host = host,
port = port,
user = user,
+ gwuser = gwuser,
+ gw = gw,
agent = agent,
identity = identity,
server_key = server_key
return ((out, err), proc)
-