Exemple #1
0
def syncexec_timeout(shellcmds, t=600, logcmd=False, logrst=False, outputfile=sys.stdout):
    if not shellcmds:
        return None
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    ret = ShellResult(-1)
    child = None
    try:
        child = pexpect.spawn('/bin/bash', ['-c', shellcmds], timeout=t)
        #index = p.expect ([pexpect.EOF, pexpect.TIMEOUT])
        while(1):
            i = child.readline()
            ret.output += i
            if not i:
                break
    except pexpect.TIMEOUT as e:
        ret.error = "Timeout while running: {0}".format(shellcmds)
    except pexpect.ExceptionPexpect as e:
        ret.error = str(e)
    else:
        if child.isalive():
            child.wait()
        ret.ret = child.exitstatus if not child.exitstatus == None else -1
        ret.output = ret.output.rstrip()
        ret.error = ret.output
    finally:
        if child:
            child.close()

    if logrst:
        if ret:
            myprt(ret.output)
        else:
            myprt(ret.error)
    return ret
Exemple #2
0
def syncexec_generater(shellcmds, t=600, logcmd=False, logrst=False, outputfile=sys.stdout):
    if not shellcmds:
        return
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    child = None
    try:
        child = pexpect.spawn('/bin/bash', ['-c', shellcmds], timeout=t)
        #index = p.expect ([pexpect.EOF, pexpect.TIMEOUT])
        while(1):
            i = child.readline()
            if i:
                if logrst:myprt(i.rstrip())
                yield i.rstrip()
            else:
                break
    except pexpect.TIMEOUT as e:
        yield "Timeout while running: {0}".format(shellcmds)
    except pexpect.ExceptionPexpect as e:
        yield str(e)
    #else:
        #yield str(child.exitstatus)
    finally:
        if child:
            child.close()
    return
Exemple #3
0
def syncexec(shellcmds, logcmd=False, logrst=False, outputfile=sys.stdout):
    if not shellcmds:
        return None
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    p = subprocess.call(shellcmds,shell=True)
    return ShellResult(p)
Exemple #4
0
def syncexec(shellcmds, logcmd=False, logrst=False, outputfile=sys.stdout):
    if not shellcmds:
        return None
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    args = shlex.split(shellcmds)
    ret = ShellResult(0)
    p = subprocess.Popen(args,shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    ret.output,ret.error = p.communicate()
    ret.ret = p.returncode

    if logrst:
        if ret:
            myprt(ret.output)
        else:
            myprt(ret.error)
    return ret
Exemple #5
0
def syncexec_generater(shellcmds, t=600, logcmd=False, logrst=False, outputfile=sys.stdout, retry = 1):
    if not shellcmds:
        return
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    child = None
    count = int(retry)
    while count:
        try:
            child = pexpect.spawn('/bin/bash', ['-c', shellcmds], timeout=t)
            #index = p.expect ([pexpect.EOF, pexpect.TIMEOUT])
            while(1):
                i = child.readline()
                if i:
                    if logrst:myprt(i.rstrip())
                    yield i.rstrip()
                else:
                    break
        except pexpect.TIMEOUT as e:
            yield "Timeout while running: {0}".format(shellcmds)
        except pexpect.ExceptionPexpect as e:
            yield str(e)
        #else:
            #yield str(child.exitstatus)
        finally:
            if child.isalive():
                child.wait()
            #print("child.exitstatus is {0}".format(child.exitstatus ))
            result = child.exitstatus if not child.exitstatus == None else -1
            if child:
                child.close()
            if logrst:myprt("result is {0}".format(result))
            if result == 0:
                break
        count -= 1
        time.sleep(0.5)
    else:
        raise Exception("command failed:{0}".format(shellcmds))
    return
Exemple #6
0
def syncexec_force(shellcmds, t=120, logcmd=False, logrst=False, outputfile=sys.stdout):
    if not shellcmds:
        return None
    myprt = prtwrapper(file=outputfile)
    if logcmd: myprt(shellcmds)
    ret = ShellResult(-1)
    child = None
    child_result_list = []
    try:
        child = pexpect.spawn('/bin/bash', ['-c', shellcmds], timeout=t)
        #index = p.expect ([pexpect.EOF, pexpect.TIMEOUT])
        while(1):
            i = child.expect ([pexpect.EOF, '.*(y/n).*$'])
            if i==0:
                child_result_list.append(child.before)
                break 
            elif i==1:
                child.sendline('y')             
    except pexpect.TIMEOUT as e:
        ret.error = "Timeout while running: {0}".format(shellcmds)
    except pexpect.ExceptionPexpect as e:
        ret.error = str(e)
    else:
        if child.isalive():
            child.wait()
        ret.ret = child.exitstatus if not child.exitstatus == None else -1
        ret.output = ''.join(child_result_list)
        ret.error = ret.output
    finally:
        if child:
            child.close()

    if logrst:
        if ret:
            myprt(ret.output)
        else:
            myprt(ret.error)
    return ret