From 95a3927e0a70ec6fd1daeb3a76c5f2283283ae02 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Tue, 11 Dec 2012 23:24:55 +0100 Subject: [PATCH] Fix interactive mode for logshell --- logshell/cvmshell | 9 +++++++-- logshell/logcmd | 22 ++++++++++++++-------- logshell/logshell | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/logshell/cvmshell b/logshell/cvmshell index 2266f72..f34ce00 100644 --- a/logshell/cvmshell +++ b/logshell/cvmshell @@ -1,11 +1,15 @@ #!/usr/bin/env python -import sys, subprocess +import sys, subprocess, time try: if sys.argv[1] == "-c": try: command = sys.argv[2] - interactive = False + + if command == 'su -s "/bin/bash" cvm': + interactive = True + else: + interactive = False except: exit(1) else: @@ -15,6 +19,7 @@ except IndexError: if interactive == True: returncode = subprocess.call(["sudo", "/home/cvm/logshell"]) + else: returncode = subprocess.call(["sudo", "/home/cvm/logcmd", command]) diff --git a/logshell/logcmd b/logshell/logcmd index 013ff9d..1365378 100755 --- a/logshell/logcmd +++ b/logshell/logcmd @@ -18,14 +18,20 @@ logfile.write("$ " + command + "\n") prc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) -while prc.poll(): - stdout, stderr = prc.communicate() - - logfile.write(stdout + "\n" + stderr + "\n") - sys.stdout.write(stdout) - sys.stderr.write(stderr) - -stdout, stderr = prc.communicate() +while prc.poll() is None: + stdout = prc.stdout.read() + stderr = prc.stderr.read() + + if stdout.strip() != "": + sys.stdout.write(stdout) + logfile.write(stdout + "\n") + + if stderr.strip() != "": + sys.stderr.write(stderr) + logfile.write(stderr + "\n") + +stdout = prc.stdout.read() +stderr = prc.stderr.read() logfile.write(stdout + "\n" + stderr + "\n") sys.stdout.write(stdout) diff --git a/logshell/logshell b/logshell/logshell index 8f90ac7..a5eca93 100644 --- a/logshell/logshell +++ b/logshell/logshell @@ -10,7 +10,7 @@ rand = ''.join(random.choice(string.letters + string.digits) for i in xrange(8)) rand = "%s_%s" % (strftime("%Y%m%d_%H%M%S"), rand) -subprocess.call("script -qt 2>/etc/cvm/log/log.%s.time -c 'su -s \"/bin/bash\" cvm' -a /etc/cvm/log/log.%s" % (rand, rand), shell=True) +subprocess.call("script -qt -c 'su -s \"/bin/bash\" cvm' -a /etc/cvm/log/log.%s 2>/etc/cvm/log/log.%s.time" % (rand, rand), shell=True) if os.environ['SHELL'] != "/bin/bash": os.remove("/etc/cvm/log/log.%s.time" % rand)