logger.log(level, msg)
-
if hasattr(os, "devnull"):
DEV_NULL = os.devnull
else:
return rv
def rexec(command, host, user,
- port = None,
+ port = None,
+ gwuser = None,
+ gw = None,
agent = True,
sudo = False,
- stdin = None,
identity = None,
server_key = None,
env = 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:
log_msg = " rexec - host %s - command %s " % (host, " ".join(args))
- return _retry_rexec(args, log_msg, env = env, retry = retry,
+ stdout = stderr = stdin = subprocess.PIPE
+ if forward_x11:
+ stdout = stderr = stdin = None
+
+ return _retry_rexec(args, log_msg,
+ stderr = stderr,
+ stdin = stdin,
+ stdout = stdout,
+ env = env,
+ retry = retry,
tmp_known_hosts = tmp_known_hosts,
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),
+ or a single file 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):
+
+ 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
def _retry_rexec(args,
log_msg,
+ stdout = subprocess.PIPE,
+ stdin = subprocess.PIPE,
+ stderr = subprocess.PIPE,
env = None,
retry = 3,
tmp_known_hosts = None,
# connects to the remote host and starts a remote connection
proc = subprocess.Popen(args,
env = env,
- stdout = subprocess.PIPE,
- stdin = subprocess.PIPE,
- stderr = subprocess.PIPE)
+ stdout = stdout,
+ stdin = stdin,
+ stderr = stderr)
# attach tempfile object to the process, to make sure the file stays
# alive until the process is finished with it
# The argument block == False forces to rexec to return immediately,
# without blocking
try:
+ err = out = " "
if blocking:
(out, err) = proc.communicate()
- else:
- err = proc.stderr.read()
+ elif stdout:
out = proc.stdout.read()
+ if proc.poll() and stderr:
+ err = proc.stderr.read()
log(log_msg, logging.DEBUG, out, err)
return ((out, err), proc)
-