pexpect output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ennoil
    New Member
    • May 2010
    • 11

    pexpect output

    I have a script that uses pexpect to ssh to a list of servers and will run a user specified command on each server. I have tried to use pexpect logging to output the results of each command to a file so the user can see the output (of commands such as uptime and ntpdate). The remote servers are both Solaris and Red Hat. For some reason, not all output is captured in the file.
    Here is my code:

    Some code unnecessary and snipped for this question...

    Code:
    def DoStuff():
       fout = file(LOG_FILENAME,'w')
     <snip>
       child = pexpect.spawn('ssh -l root %s'%(i))
       k = child.expect (['connecting (yes/no)', '[Pp]assword: '])
       if k==0:
          print "Adding key for %s"%(i)
          child.sendline ("yes")
          child.expect ('[Pp]assword: ')
          child.sendline (Passwd1)
       if k==1:
          child.sendline (Passwd1)
       l = child.expect (['please try again.', '[#/$]'])
       if l==0:
          child.sendline (Passwd2)
       if l==1:
          child.sendline ('echo')
       child.expect ('[#/$]')
       child.sendline (cmnd)
       child.logfile_read = fout
       child.expect ('#')
       child.logfile = None
       child.sendline ('exit')
    Ideally, I would like a way to print the server name on a line then the output from the command on the next line(s).
    I can not figure out why the above, using uptime as the command, gives the following:


    Code:
    $ more /tmp/menu2.log
    root@server1 :#uptime
    root@server2 :#uptimeuptime
      2:53pm  up 174 day(s),  8:07,  1 user,  load average: 0.00, 0.00, 0.01
    # uptime
      2:46pm  up 174 day(s),  7:53,  1 user,  load average: 0.04, 0.04, 0.04
    #
    root@server3 :#uptime
    root@server4 :#uptime
    root@server5 :#uptime.example.com now...
Working...