Exemple #1
0
def submit(crontab, username=None, password=None):
    """Submit a crontab via the crontab program. 
    Supply a crontab object. If it is to be installed for a different user
    supply the `username` parameter. If this is not run as root, then sudo is
    used and you must supply your own password for sudo."""
    if username is None:
        ct = proctools.spawnpipe("crontab -")
        ct.write(str(crontab))
        ct.close()
        return ct.wait()
    else:
        if os.getuid() == 0:
            ct = proctools.spawnpipe("crontab -u %s -" % (username,))
            ct.write(str(crontab))
            ct.close()
            return ct.wait()
        else:
            from pycopia import sudo
            if password is None:
                from pycopia import tty
                password = tty.getpass("Your password:"******"crontab -u %s -" % (username,), password=password)
            ct.write(str(crontab))
            ct.close()
            return ct.wait()
Exemple #2
0
def submit(crontab, username=None, password=None):
    """Submit a crontab via the crontab program.
    Supply a crontab object. If it is to be installed for a different user
    supply the `username` parameter. If this is not run as root, then sudo is
    used and you must supply your own password for sudo."""
    if username is None:
        ct = proctools.spawnpipe("crontab -")
        ct.write(str(crontab))
        ct.close()
        return ct.wait()
    else:
        if os.getuid() == 0:
            ct = proctools.spawnpipe("crontab -u %s -" % (username, ))
            ct.write(str(crontab))
            ct.close()
            return ct.wait()
        else:
            from pycopia import sudo
            if password is None:
                from pycopia import tty
                password = tty.getpass("Your password:"******"crontab -u %s -" % (username, ), password=password)
            ct.write(str(crontab))
            ct.close()
            return ct.wait()
Exemple #3
0
 def test_readaline(self):
     lspm = proctools.spawnpipe("ls /bin")
     lines = lspm.readlines()
     self.assertTrue(lines)
     lspm.close()
     es = lspm.stat()
     self.assertTrue(es)
Exemple #4
0
    def RunCommand(self, cmdline, env=None, timeout=None, logfile=None):
        """Run an external command. 

        This method will block until the command returns. An optional timeout
        may be supplied to prevent hanging forever.

        Arguments:
            A string that is the command line to be run. 
            A (optional) dictionary containing the environment variables.
            An (optional) timeout value that will forcibly return if the call
                takes longer than the timeout value.

        Returns:
         A tuple of ExitStatus object and stdout/stderr (string) of the program.
        """
        from pycopia import proctools
        p = proctools.spawnpipe(cmdline, logfile=logfile, env=env)
        try:
            if timeout:
                sched = scheduler.get_scheduler()
                text = sched.iotimeout(p.read, timeout=timeout)
            else:
                text = p.read()
        finally:
            p.wait()
            p.close()
        return p.exitstatus, text
Exemple #5
0
def netcat_version():
    """netcat_version() Return the version string for the netcat command on this system."""
    nc = proctools.spawnpipe("%s -h" % (NETCAT))
    ver = nc.readline()
    nc.read() # discard rest
    nc.wait()
    return ver.strip()
Exemple #6
0
def netcat_version():
    """netcat_version() Return the version string for the netcat command on this system."""
    nc = proctools.spawnpipe("%s -h" % (NETCAT))
    ver = nc.readline()
    nc.read() # discard rest
    nc.wait()
    return ver.strip()
Exemple #7
0
def ssh_version():
    """ssh_version() Return the version string for the ssh command on this
    system.
    """
    ssh = proctools.spawnpipe("ssh -TV")
    ver = ssh.read()
    return ver
Exemple #8
0
 def test_readaline(self):
     lspm = proctools.spawnpipe("ls /bin")
     lines = lspm.readlines()
     self.assertTrue(lines)
     lspm.close()
     es = lspm.stat()
     self.assertTrue(es)
Exemple #9
0
def ssh_version():
    """ssh_version() Return the version string for the ssh command on this
    system.
    """
    ssh = proctools.spawnpipe("ssh -TV")
    ver = ssh.read()
    return ver
Exemple #10
0
    def run_command(self, ctx):
        """run_command(testcondition)
        Runs command and tests responses as defined by the TestCondition.
        """
        cmdline = ctx.cmdline
        stdin = ctx.stdin
        expectedout = ctx.expectedout
        expectederr = ctx.expectederr
        expectedexit = ctx.expectedexit
        environ = ctx.environ

        if expectederr:
            mergeerr = 0
        else:
            mergeerr = 1
        self.info("running: %s" % cmdline)
        p = proctools.spawnpipe(cmdline, env=environ, merge=mergeerr)
        if stdin:
            p.write(stdin)
        if expectedout:
            output = p.read()
        if expectederr:
            errors = p.readerr()
        p.wait()
        es = p.exitstatus
        if int(es) != expectedexit:
            return self.failed("bad exit value: expected %d, got %d" % (expectedexit, int(es)))

        if expectedout and (output != expectedout):
            return self.failed("bad output: %r" % (output))

        if expectederr and (errors != expectederr):
            return self.failed("bad error output: %r" % (errors))

        return self.passed("no errors")
Exemple #11
0
    def test_command(self, testcondition):
        cmdline = testcondition.cmdline
        stdin = testcondition.stdin
        expectedout = testcondition.expectedout
        expectederr = testcondition.expectederr
        expectedexit = testcondition.expectedexit
        environ = testcondition.environ

        if expectederr:
            mergeerr = 0
        else:
            mergeerr = 1
        self.info("running: %s" % cmdline)
        p = proctools.spawnpipe(cmdline, env=environ, merge=mergeerr)
        if stdin:
            p.write(stdin)
        if expectedout:
            output = p.read()
        if expectederr:
            errors = p.readerr()
        p.wait()
        es = p.exitstatus
        if int(es) != expectedexit:
            self.failed("bad exit value: expected %d, got %d" % (expectedexit, int(es)))

        if expectedout and (output != expectedout):
            self.failed("bad output: %r" % (output))

        if expectederr and (errors != expectederr):
            self.failed("bad error output: %r" % (errors))

        self.passed("no errors detected.")
Exemple #12
0
    def test_command(self, testcondition):
        cmdline = testcondition.cmdline
        stdin = testcondition.stdin
        expectedout = testcondition.expectedout
        expectederr = testcondition.expectederr
        expectedexit = testcondition.expectedexit
        environ = testcondition.environ

        if expectederr:
            mergeerr = 0
        else:
            mergeerr = 1
        self.info("running: %s" % cmdline)
        p = proctools.spawnpipe(cmdline, env=environ, merge=mergeerr)
        if stdin:
            p.write(stdin)
        if expectedout:
            output = p.read()
        if expectederr:
            errors = p.readerr()
        p.wait()
        es = p.exitstatus
        if int(es) != expectedexit:
            self.failed("bad exit value: expected %d, got %d" %
                        (expectedexit, int(es)))

        if expectedout and (output != expectedout):
            self.failed("bad output: %r" % (output))

        if expectederr and (errors != expectederr):
            self.failed("bad error output: %r" % (errors))

        self.passed("no errors detected.")
Exemple #13
0
 def test_spawnpipe(self):
     ls = proctools.spawnpipe("ls /usr/bin")
     files = ls.read()
     self.assertTrue(files)
     self.assertFalse(ls.readerr())
     ls.close()
     es = ls.wait()
     self.assertTrue(es)
Exemple #14
0
def rsync_version():
    """rsync_version() Return the version string for the rsync command on this
system."""
    rsync = proctools.spawnpipe("rsync --version")
    ver = rsync.readline() # version on first line of output
    rsync.read() # discard rest
    rsync.close()
    return ver
Exemple #15
0
 def test_spawnpipe(self):
     ls = proctools.spawnpipe("ls /usr/bin")
     files = ls.read()
     self.assertTrue(files)
     self.assertFalse(ls.readerr())
     ls.close()
     es = ls.wait()
     self.assertTrue(es)
Exemple #16
0
 def test_pipeline(self):
     ptest = proctools.spawnpipe("cat /etc/hosts | sort")
     hosts = ptest.read()
     self.assertTrue(bool(hosts))
     self.assertFalse(bool(ptest.readerr()))
     ptest.close()
     es = ptest.stat()
     self.assertTrue(es)
Exemple #17
0
 def test_pipeline(self):
     ptest = proctools.spawnpipe("cat /etc/hosts | sort")
     hosts = ptest.read()
     self.assertTrue(bool(hosts))
     self.assertFalse(bool(ptest.readerr()))
     ptest.close()
     es = ptest.stat()
     self.assertTrue(es)
Exemple #18
0
def rsync_version():
    """rsync_version() Return the version string for the rsync command on this
system."""
    rsync = proctools.spawnpipe("rsync --version")
    ver = rsync.readline() # version on first line of output
    rsync.read() # discard rest
    rsync.close()
    return ver
Exemple #19
0
def sudo(command, user=None, password=None, extraopts=None, logfile=None):
    opts = "-S %s" % (IF(user, "-u %s" % (user,), ""),)
    cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command)
    proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0)
    if password:
        proc.readerr(9) # discard password prompt
        proc.write("%s\r" % (password,))
        proc.readerr(1) # discard newline
    return proc
Exemple #20
0
def sudo(command, user=None, password=None, extraopts=None, logfile=None):
    opts = "-S %s" % ("-u %s" % user if user else "")
    cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command)
    proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0)
    if password:
        proc.readerr(9)  # discard password prompt
        proc.write("%s\r" % (password, ))
        proc.readerr(1)  # discard newline
    return proc
Exemple #21
0
 def test_lserror(self):
     ls = proctools.spawnpipe("ls /usr/binxx", merge=0)
     print(ls.read())
     print("errors:")
     print(ls.readerr())
     ls.close()
     ls.wait()
     es = ls.stat()
     self.assertFalse(es)
Exemple #22
0
 def test_lserror(self):
     ls = proctools.spawnpipe("ls /usr/binxx", merge=0)
     print(ls.read())
     print("errors:")
     print(ls.readerr())
     ls.close()
     ls.wait()
     es = ls.stat()
     self.assertFalse(es)
Exemple #23
0
 def get_interfaces(self):
     proc = proctools.spawnpipe("/usr/bin/vtysh -c 'show int'")
     out = proc.read()
     es = proc.wait()
     if es:
         ifl = InterfaceList()
         ifl.parse(out)
         return ifl
     else:
         raise ControllerError("Could not read output of ip command.")
Exemple #24
0
 def test_lserror(self):
     ls = proctools.spawnpipe("ls /usr/binxx", merge=0)
     out = ls.read()
     errout = ls.readerr()
     self.assertFalse(bool(out))
     self.assertTrue(bool(errout))
     ls.close()
     ls.wait()
     es = ls.stat()
     self.assertFalse(es)
Exemple #25
0
def get_alsaplayer(session=0,
                   name="alsaplayer",
                   device=DEVICE,
                   extraopts="",
                   logfile=None):
    """Return a process object for the alsaplayer."""
    opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name,
                                                          device)
    CMD = "%s %s %s" % (PLAYER, opts, extraopts)
    aplayer = proctools.spawnpipe(CMD, logfile=logfile)
    return aplayer
Exemple #26
0
def open(filename=None, username=None):
    if filename is None:
        cmd = "crontab -l"
        if username:
            cmd += " -u %s" % (username,)
        ct = proctools.spawnpipe(cmd)
        text = ct.read()
        ct.close()
    else:
        text = open(filename).read()
    return CrontabFile(text, filename, username)
Exemple #27
0
def open(filename=None, username=None):
    if filename is None:
        cmd = "crontab -l"
        if username:
            cmd += " -u %s" % (username, )
        ct = proctools.spawnpipe(cmd)
        text = ct.read()
        ct.close()
    else:
        text = open(filename).read()
    return CrontabFile(text, filename, username)
Exemple #28
0
 def sendmail(self, From, rcpt_to, msg, mopts=None, rcptopts=None):
     from pycopia import proctools
     cmd = "/usr/sbin/sendmail -i"
     if From:
         cmd += " -f %s" % From
     for rcpt in rcpt_to:
         cmd += " %s" % rcpt
     proc = proctools.spawnpipe(cmd)
     proc.write(msg)
     proc.close()
     proc.wait()
     return proc.exitstatus
Exemple #29
0
 def sendmail(self, From, rcpt_to, msg, mopts=None, rcptopts=None):
     from pycopia import proctools
     cmd = "/usr/sbin/sendmail -i"
     if From:
         cmd += " -f %s" % From
     for rcpt in rcpt_to:
         cmd += " %s" % rcpt
     proc = proctools.spawnpipe(cmd)
     proc.write(msg)
     proc.close()
     proc.wait()
     return proc.exitstatus
Exemple #30
0
def redir(lport, cport, laddr=None, caddr=None, extraopts=None):
    """redir(lport, cport, laddr=None, caddr=None, extraopts=None)
Redirect local port to client port, possible to another host is caddr is given.
Optionally bind to a specific IP if laddr is also given.
    """
    opts = "--lport=%d --cport=%d" % (lport, cport)
    if laddr:
        opts += " --laddr=%s" % (laddr,)
    if caddr:
        opts += " --caddr=%s" % (caddr,)
    cmd = "%s %s %s" % (REDIR, opts, extraopts or "")
    proc = proctools.spawnpipe(cmd, merge=0)
    return proc
Exemple #31
0
    def run_command(self, ctx):
        """run_command(testcondition)
        Runs command and tests responses as defined by the TestCondition.
        """
        cmdline = ctx.cmdline
        stdin = ctx.stdin
        expectedout = ctx.expectedout
        expectederr = ctx.expectederr
        expectedexit = ctx.expectedexit
        environ = ctx.environ

        if expectederr:
            mergeerr = 0
        else:
            mergeerr = 1
        self.info("running: %s" % cmdline)
        p = proctools.spawnpipe(cmdline, env=environ, merge=mergeerr)
        if stdin:
            p.write(stdin)
        if expectedout:
            output = p.read()
        if expectederr:
            errors = p.readerr()
        p.wait()
        es = p.exitstatus
        if int(es) != expectedexit:
            return self.failed("bad exit value: expected %d, got %d" %
                               (expectedexit, int(es)))

        if expectedout and (output != expectedout):
            return self.failed("bad output: %r" % (output))

        if expectederr and (errors != expectederr):
            return self.failed("bad error output: %r" % (errors))

        return self.passed("no errors")
Exemple #32
0
def get_alsaplayer(session=0, name="alsaplayer", device=DEVICE, extraopts="", logfile=None):
    """Return a process object for the alsaplayer."""
    opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name, device)
    CMD = "%s %s %s" % (PLAYER, opts, extraopts)
    aplayer = proctools.spawnpipe(CMD, logfile=logfile)
    return aplayer
Exemple #33
0
def sudo_reset():
    proc = proctools.spawnpipe("%s -k" % (SUDO, ), merge=0)
    proc.read()
    proc.wait()
Exemple #34
0
def sudo_reset():
    proc = proctools.spawnpipe("%s -k" % (SUDO,), merge=0)
    proc.read()
    proc.wait()