Changing the system clock with pexpect confuses pexpect!

Discussion in 'Python' started by Saqib Ali, Dec 26, 2011.

  1. Saqib Ali

    Saqib Ali Guest

    See my code below.

    I'm controlling a shell logged in as root with pexpect.

    The class below has a method (startProc) which spawns a shell and
    keeps it alive until told to destroy it (stopProc).

    The other 2 methods in this class allow me to change the system clock
    and to get the IP Address of this machine.

    They all work fine.... except when I advance the system clock, and
    then try to get the IP Address.
    In that case, I get an exception because pexpect incorrectly thinks
    the output it is getting from ifconfig is invalid. But it is not.
    Pexpect is just confused. This doesn't happen when I move the clock
    backwards. It only happens when I move the clock forward.

    I believe what is going on is that internally pexpect uses the system
    clock to keep track of when it receives data from spawned processes.
    When I mess with the clock, that messes up the internal workings of
    pexpect.

    Any suggestions what I should do? I have numerous concurrent pexpect
    processes running when I modify the clock. Is there anyway to prevent
    them all from getting totally screwed up??

    -----------------------------------------------------------------------------------------------------


    #!/usr/bin/env python
    import pexpect, os, time, datetime, re

    def reportPrint(string):
    print string

    def reportAssert(condition, string)
    if condition == False:
    print string
    raise Exception


    class rootManager:

    rootProc = None
    rootPrompt = "] % "
    myPrompt = "] % "

    def __init__(self):
    pass




    def startProc(self):
    if self.rootProc != None:
    reportPrint("\t\t- Root Process is already created")
    else:
    self.rootProc = pexpect.spawn ('/bin/tcsh',)
    i = self.rootProc.expect([pexpect.TIMEOUT,
    self.myPrompt,])
    reportAssert(i != 0, "Time-Out.... exiting")

    reportPrint("\t\t- Sending su")
    self.rootProc.sendline("su")
    i = self.rootProc.expect([pexpect.TIMEOUT, "Password: ",])
    reportAssert(i != 0, "Time-Out.... exiting")

    reportPrint("\t\t- Sending Password")
    self.rootProc.sendline(ROOT_PASSWORD)
    i = self.rootProc.expect([pexpect.TIMEOUT,
    self.rootPrompt,])
    reportAssert(i != 0, "Time-Out.... exiting")

    reportPrint("\t\t- Root Process created")


    def getIPAddr(self):
    reportAssert(self.rootProc != None, "No active Root Process!")

    reportPrint("\t\t- Sending ifconfig -a")
    self.rootProc.sendline("ifconfig -a")
    i = self.rootProc.expect([pexpect.TIMEOUT, self.rootPrompt,])
    reportAssert(i != 0, "Time-Out.... exiting")

    outputTxt = self.rootProc.before
    ipList = [i for i in re.compile("(?<=inet )\d{1,3}\.\d{1,3}\.
    \d{1,3}\.\d{1,3}").findall(outputTxt) if i != "127.0.0.1"]
    reportAssert(len(ipList) == 1, "Cannot determine IP Address
    from 'ifconfig -a': \n%s" % outputTxt)
    return ipList[0]


    def changeClock(self, secondsDelta):
    reportAssert(self.rootProc != None, "No active Root Process!")

    newTime = datetime.datetime.now() +
    datetime.timedelta(seconds=secondsDelta)
    dateStr = "%02d%02d%02d%02d%s" % (newTime.month, newTime.day,
    newTime.hour, newTime.minute, str(newTime.year)[-2:])
    reportPrint("\t\t- Sending 'date %s' command" % dateStr)
    self.rootProc.sendline("date %s" % dateStr)
    #Remember, by changing the clock, you are confusing pexpect's
    timeout measurement!
    # so ignore timeouts in this case
    i = self.rootProc.expect([pexpect.TIMEOUT,
    self.rootPrompt,],)






    def stopProc(self):
    if self.rootProc == None:
    reportPrint("\t\t- Root Process is already destroyed")
    else:

    reportPrint("\t\t- Sending exit command")
    rootProc.sendline("exit")
    i = rootProc.expect([pexpect.TIMEOUT, self.myPrompt])
    reportAssert(i != 0, "Time-Out.... exiting")

    reportPrint("\t\t- Sending exit command")
    rootProc.sendline("exit")
    i = rootProc.expect([pexpect.TIMEOUT, pexpect.EOF])
    reportAssert(i != 0, "Time-Out.... exiting")
    self.rootProc.close()
    self.rootProc = None
    reportPrint("\t\t- Root Process Destroyed")
     
    Saqib Ali, Dec 26, 2011
    #1
    1. Advertising

  2. Saqib Ali

    Roy Smith Guest

    In article
    <>,
    Saqib Ali <> wrote:

    > I believe what is going on is that internally pexpect uses the system
    > clock to keep track of when it receives data from spawned processes.
    > When I mess with the clock, that messes up the internal workings of
    > pexpect.
    >
    > Any suggestions what I should do?


    Yeah. Don't mess with the system clock. Seriously.

    There is pretty much no reason for anybody not to be running NTP these
    days. Every OS ships with it, and there's gazillions of open NTP
    servers to use as references.

    NTP will keep your system clock accurate to within a fraction of a
    second and avoid all the horrible problems such as the one you've
    discovered with pexpect when you reset your clock.
     
    Roy Smith, Dec 26, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg G
    Replies:
    2
    Views:
    2,035
    Greg G
    Jul 12, 2006
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    825
    Dan Pop
    Jun 21, 2004
  3. Magnus Lyck?
    Replies:
    5
    Views:
    415
    Magnus Lyck?
    Dec 2, 2003
  4. Osiris
    Replies:
    0
    Views:
    305
    Osiris
    Jan 1, 2007
  5. Terry Jan Reedy
    Replies:
    20
    Views:
    259
    Chris Angelico
    Jun 13, 2013
Loading...

Share This Page