X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=src%2Fnepi%2Futil%2Fsshfuncs.py;h=ac43099b4200554b62ee4a2ee6c2909e9579192c;hb=5857fa2d26b3aa135391cf936ccde907b9ff8c68;hp=81bf5f9b5ddba91ca99e8f61dc740b34a9b2e0ef;hpb=f4a5a9a345818610739e9e5841b9da5b1a062f5d;p=nepi.git diff --git a/src/nepi/util/sshfuncs.py b/src/nepi/util/sshfuncs.py index 81bf5f9b..ac43099b 100644 --- a/src/nepi/util/sshfuncs.py +++ b/src/nepi/util/sshfuncs.py @@ -46,7 +46,6 @@ def log(msg, level, out = None, err = None): logger.log(level, msg) - if hasattr(os, "devnull"): DEV_NULL = os.devnull else: @@ -207,7 +206,9 @@ def eintr_retry(func): return rv def rexec(command, host, user, - port = None, + port = None, + gwuser = None, + gw = None, agent = True, sudo = False, identity = None, @@ -225,7 +226,9 @@ def rexec(command, host, user, """ 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 @@ -234,6 +237,7 @@ def rexec(command, host, user, '-o', 'ConnectionAttempts=3', '-o', 'ServerAliveInterval=30', '-o', 'TCPKeepAlive=yes', + '-o', 'Batchmode=yes', '-l', user, hostip or host] if persistent and openssh_has_persist(): @@ -246,6 +250,13 @@ def rexec(command, host, user, # 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') @@ -253,6 +264,7 @@ def rexec(command, host, user, args.append('-p%d' % port) if identity: + identity = os.path.expanduser(identity) args.extend(('-i', identity)) if tty: @@ -288,8 +300,9 @@ def rexec(command, host, user, blocking = blocking) def rcopy(source, dest, - port = None, - agent = True, + port = None, + gwuser = None, + gw = None, recursive = False, identity = None, server_key = None, @@ -301,14 +314,15 @@ def rcopy(source, dest, 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 @: - 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" @@ -331,10 +345,18 @@ def rcopy(source, dest, 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: @@ -345,18 +367,21 @@ def rcopy(source, dest, 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)) @@ -374,6 +399,8 @@ def rspawn(command, pidfile, host = None, port = None, user = None, + gwuser = None, + gw = None, agent = None, identity = None, server_key = None, @@ -447,6 +474,8 @@ def rspawn(command, pidfile, host = host, port = port, user = user, + gwuser = gwuser, + gw = gw, agent = agent, identity = identity, server_key = server_key, @@ -463,6 +492,8 @@ def rgetpid(pidfile, host = None, port = None, user = None, + gwuser = None, + gw = None, agent = None, identity = None, server_key = None): @@ -489,6 +520,8 @@ def rgetpid(pidfile, host = host, port = port, user = user, + gwuser = gwuser, + gw = gw, agent = agent, identity = identity, server_key = server_key @@ -509,6 +542,8 @@ def rstatus(pid, ppid, host = None, port = None, user = None, + gwuser = None, + gw = None, agent = None, identity = None, server_key = None): @@ -533,6 +568,8 @@ def rstatus(pid, ppid, host = host, port = port, user = user, + gwuser = gwuser, + gw = gw, agent = agent, identity = identity, server_key = server_key @@ -556,6 +593,8 @@ def rkill(pid, ppid, host = None, port = None, user = None, + gwuser = None, + gw = None, agent = None, sudo = False, identity = None, @@ -610,6 +649,8 @@ fi host = host, port = port, user = user, + gwuser = gwuser, + gw = gw, agent = agent, identity = identity, server_key = server_key @@ -684,4 +725,3 @@ def _retry_rexec(args, return ((out, err), proc) -