pexpect module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gianluca Trombetta

    pexpect module

    Someone know pexpect module?
    I've a problem working with it...

    I need to run some commands on remote hosts, like ls, df -k etc..All right.
    Although, when i launch a command that have a "more" inside, i don't know
    what i must expect!
    An example:

    if i want to run an "ls -l | more" on a remote host, it don't return me a
    prompt, but a "-------------More-------------"...thus i don't know how much
    this.

    I've tried with:

    child.expect('. *')

    it works, but do not return any value.

    Any idea?
    I hope to have clearly explained my problem with my bad english...:-)

    Thank you in advance
    Regards

    Gianluca Trombetta




  • Simon Burton

    #2
    Re: pexpect module

    On Fri, 14 May 2004 19:50:54 +0200, Gianluca Trombetta wrote:
    [color=blue]
    > Someone know pexpect module?
    > I've a problem working with it...
    >
    > I need to run some commands on remote hosts, like ls, df -k etc..All
    > right. Although, when i launch a command that have a "more" inside, i
    > don't know what i must expect![/color]

    Why don't you use a local more ? Or write your own "more" in python.

    Simon.


    Comment

    • Lee Harr

      #3
      Re: pexpect module

      On 2004-05-14, Gianluca Trombetta <gianluca.tromb etta@tin.it> wrote:[color=blue]
      > Someone know pexpect module?
      > I've a problem working with it...
      >
      > I need to run some commands on remote hosts, like ls, df -k etc..All right.
      > Although, when i launch a command that have a "more" inside, i don't know
      > what i must expect!
      > An example:
      >
      > if i want to run an "ls -l | more" on a remote host, it don't return me a
      > prompt, but a "-------------More-------------"...thus i don't know how much
      > this.
      >[/color]


      The only reason I can see for piping the command through more would
      be if there were a person sitting there wanting to read the output
      before going on to read the next page of output. That seems counter
      to the idea of using pexpect to automate the process. Do you
      really need to pipe through more?

      Comment

      • Gianluca Trombetta

        #4
        Re: pexpect module

        hmmm, i'm not so stupid...:-).
        The command ls -l | more was an example...i'm not really need to run ls -l |
        more in an automate program.
        But I need to run some cisco commands like "show ip bgp summary", this
        command print a table and put a "More" by default, if the table is too big
        for a single terminal screen.
        So I need to remove this behavior because if I match the " --More-- " with
        expect module i can match only the first one "More", then this the program
        crash and receives a timeout.

        Hi
        Gianluca



        "Lee Harr" <missive@fronti ernet.net> ha scritto nel messaggio
        news:P9Lpc.5055 $xg3.4383@news0 1.roc.ny...[color=blue]
        > On 2004-05-14, Gianluca Trombetta <gianluca.tromb etta@tin.it> wrote:[color=green]
        > > Someone know pexpect module?
        > > I've a problem working with it...
        > >
        > > I need to run some commands on remote hosts, like ls, df -k etc..All[/color][/color]
        right.[color=blue][color=green]
        > > Although, when i launch a command that have a "more" inside, i don't[/color][/color]
        know[color=blue][color=green]
        > > what i must expect!
        > > An example:
        > >
        > > if i want to run an "ls -l | more" on a remote host, it don't return me[/color][/color]
        a[color=blue][color=green]
        > > prompt, but a "-------------More-------------"...thus i don't know how[/color][/color]
        much[color=blue][color=green]
        > > this.
        > >[/color]
        >
        >
        > The only reason I can see for piping the command through more would
        > be if there were a person sitting there wanting to read the output
        > before going on to read the next page of output. That seems counter
        > to the idea of using pexpect to automate the process. Do you
        > really need to pipe through more?
        >
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]




        Comment

        • Cameron Laird

          #5
          Re: pexpect module

          In article <mailman.14.108 4810407.6949.py thon-list@python.org >,
          Gianluca Trombetta <gianluca.tromb etta@tin.it> wrote:[color=blue]
          >hmmm, i'm not so stupid...:-).
          >The command ls -l | more was an example...i'm not really need to run ls -l |
          >more in an automate program.
          >But I need to run some cisco commands like "show ip bgp summary", this
          >command print a table and put a "More" by default, if the table is too big
          >for a single terminal screen.
          >So I need to remove this behavior because if I match the " --More-- " with
          >expect module i can match only the first one "More", then this the program
          >crash and receives a timeout.[/color]

          Comment

          • John Hazen

            #6
            Re: pexpect module

            * Gianluca Trombetta <gianluca.tromb etta@tin.it> [2004-05-17 09:10]:[color=blue]
            > But I need to run some cisco commands like "show ip bgp summary", this
            > command print a table and put a "More" by default, if the table is too big
            > for a single terminal screen.
            > So I need to remove this behavior because if I match the " --More-- " with
            > expect module i can match only the first one "More", then this the program
            > crash and receives a timeout.[/color]

            Why don't you post your code and the traceback you're getting. I think
            you should be able to expect the "More" multiple times.

            Try using expect with a list argument, and detect which one matched.

            [untested]

            console = pexpect.spawn(" whatever_comman d_you_use")
            # probably other stuff here (enabling, etc.) ...
            console.sendlin e("sh ip bgp summ")
            # look for more or prompt
            return_index = console.expect([" --More-- ","#"])
            while return_index == 0:
            console.send(" ")
            return_index = console.expect([" --More-- ","#"])
            # matched prompt, so now you're good to go.


            Note, that you may have to save the console.before in the while loop, so
            you don't lose it before you match the prompt.

            HTH-

            John

            P.S. [OT]: The cisco command I use to disable the pager is:
            # terminal length 0

            Comment

            • Adrian Casey

              #7
              Re: pexpect module

              First of all, look up the manual for Cisco's 'show' command. See if there
              is a way to turn off the paging. If not, try to fool cisco into thinking
              you have a very large page size on your terminal (e.g. 10,000 lines).

              If this is not possible, you can loop in your python script -:

              while 1:
              i=child.expect( '--More--',pexpect.TIMEO UT)
              if i==0:
              child.sendline( ' ')
              else:
              break

              Adrian.

              Gianluca Trombetta wrote:
              [color=blue]
              > hmmm, i'm not so stupid...:-).
              > The command ls -l | more was an example...i'm not really need to run ls -l
              > | more in an automate program.
              > But I need to run some cisco commands like "show ip bgp summary", this
              > command print a table and put a "More" by default, if the table is too big
              > for a single terminal screen.
              > So I need to remove this behavior because if I match the " --More-- " with
              > expect module i can match only the first one "More", then this the program
              > crash and receives a timeout.
              >
              > Hi
              > Gianluca
              >
              >
              >
              > "Lee Harr" <missive@fronti ernet.net> ha scritto nel messaggio
              > news:P9Lpc.5055 $xg3.4383@news0 1.roc.ny...[color=green]
              >> On 2004-05-14, Gianluca Trombetta <gianluca.tromb etta@tin.it> wrote:[color=darkred]
              >> > Someone know pexpect module?
              >> > I've a problem working with it...
              >> >
              >> > I need to run some commands on remote hosts, like ls, df -k etc..All[/color][/color]
              > right.[color=green][color=darkred]
              >> > Although, when i launch a command that have a "more" inside, i don't[/color][/color]
              > know[color=green][color=darkred]
              >> > what i must expect!
              >> > An example:
              >> >
              >> > if i want to run an "ls -l | more" on a remote host, it don't return me[/color][/color]
              > a[color=green][color=darkred]
              >> > prompt, but a "-------------More-------------"...thus i don't know how[/color][/color]
              > much[color=green][color=darkred]
              >> > this.
              >> >[/color]
              >>
              >>
              >> The only reason I can see for piping the command through more would
              >> be if there were a person sitting there wanting to read the output
              >> before going on to read the next page of output. That seems counter
              >> to the idea of using pexpect to automate the process. Do you
              >> really need to pipe through more?
              >>
              >> --
              >> http://mail.python.org/mailman/listinfo/python-list
              >>[/color][/color]

              Comment

              • Adrian Casey

                #8
                Re: pexpect module

                Ooops - forgot the search for the prompt :-(
                Change the expect line to -:
                i=child.expect(['--More--', PROMPT],pexpect.TIMEOU T)

                (Assumes PROMPT evaluates to a regular expression matching the cisco
                prompt).

                Adrian Casey wrote:
                [color=blue]
                > First of all, look up the manual for Cisco's 'show' command. See if there
                > is a way to turn off the paging. If not, try to fool cisco into thinking
                > you have a very large page size on your terminal (e.g. 10,000 lines).
                >
                > If this is not possible, you can loop in your python script -:
                >
                > while 1:
                > i=child.expect( '--More--',pexpect.TIMEO UT)
                > if i==0:
                > child.sendline( ' ')
                > else:
                > break
                >
                > Adrian.
                >
                > Gianluca Trombetta wrote:
                >[color=green]
                >> hmmm, i'm not so stupid...:-).
                >> The command ls -l | more was an example...i'm not really need to run ls
                >> -l
                >> | more in an automate program.
                >> But I need to run some cisco commands like "show ip bgp summary", this
                >> command print a table and put a "More" by default, if the table is too
                >> big for a single terminal screen.
                >> So I need to remove this behavior because if I match the " --More-- "
                >> with expect module i can match only the first one "More", then this the
                >> program crash and receives a timeout.
                >>
                >> Hi
                >> Gianluca
                >>
                >>
                >>
                >> "Lee Harr" <missive@fronti ernet.net> ha scritto nel messaggio
                >> news:P9Lpc.5055 $xg3.4383@news0 1.roc.ny...[color=darkred]
                >>> On 2004-05-14, Gianluca Trombetta <gianluca.tromb etta@tin.it> wrote:
                >>> > Someone know pexpect module?
                >>> > I've a problem working with it...
                >>> >
                >>> > I need to run some commands on remote hosts, like ls, df -k etc..All[/color]
                >> right.[color=darkred]
                >>> > Although, when i launch a command that have a "more" inside, i don't[/color]
                >> know[color=darkred]
                >>> > what i must expect!
                >>> > An example:
                >>> >
                >>> > if i want to run an "ls -l | more" on a remote host, it don't return
                >>> > me[/color]
                >> a[color=darkred]
                >>> > prompt, but a "-------------More-------------"...thus i don't know how[/color]
                >> much[color=darkred]
                >>> > this.
                >>> >
                >>>
                >>>
                >>> The only reason I can see for piping the command through more would
                >>> be if there were a person sitting there wanting to read the output
                >>> before going on to read the next page of output. That seems counter
                >>> to the idea of using pexpect to automate the process. Do you
                >>> really need to pipe through more?
                >>>
                >>> --
                >>> http://mail.python.org/mailman/listinfo/python-list
                >>>[/color][/color][/color]

                Comment

                • Gianluca Trombetta

                  #9
                  Re: pexpect module

                  Great!

                  I've disable the pager with the command "terminal length 0" and now it seem
                  work very well.
                  Thank you very much John and thank to all other!

                  Regards,
                  Gianluca Trombetta

                  "John Hazen" <john@hazen.net > ha scritto nel messaggio
                  news:2004051721 0708.GA28582@ga te2.hazen.net.. .[color=blue]
                  > * Gianluca Trombetta <gianluca.tromb etta@tin.it> [2004-05-17 09:10]:[color=green]
                  > > But I need to run some cisco commands like "show ip bgp summary", this
                  > > command print a table and put a "More" by default, if the table is too[/color][/color]
                  big[color=blue][color=green]
                  > > for a single terminal screen.
                  > > So I need to remove this behavior because if I match the " --More-- "[/color][/color]
                  with[color=blue][color=green]
                  > > expect module i can match only the first one "More", then this the[/color][/color]
                  program[color=blue][color=green]
                  > > crash and receives a timeout.[/color]
                  >
                  > Why don't you post your code and the traceback you're getting. I think
                  > you should be able to expect the "More" multiple times.
                  >
                  > Try using expect with a list argument, and detect which one matched.
                  >
                  > [untested]
                  >
                  > console = pexpect.spawn(" whatever_comman d_you_use")
                  > # probably other stuff here (enabling, etc.) ...
                  > console.sendlin e("sh ip bgp summ")
                  > # look for more or prompt
                  > return_index = console.expect([" --More-- ","#"])
                  > while return_index == 0:
                  > console.send(" ")
                  > return_index = console.expect([" --More-- ","#"])
                  > # matched prompt, so now you're good to go.
                  >
                  >
                  > Note, that you may have to save the console.before in the while loop, so
                  > you don't lose it before you match the prompt.
                  >
                  > HTH-
                  >
                  > John
                  >
                  > P.S. [OT]: The cisco command I use to disable the pager is:
                  > # terminal length 0
                  >
                  > --
                  > http://mail.python.org/mailman/listinfo/python-list
                  >[/color]




                  Comment

                  Working...