Version 5 of spawn

Updated 2005-09-20 21:31:06 by escargo

[add information about expect's spawn command, pointers to its man page on http://www.tcl.tk/man/ , etc.]

spawn creates a session with a program or remote device that Expect can manage. Spawned sessions can be automated using other Expect commands, such as expect, interact and exp_send.

The man page for spawn is actually part of the Expect man page http://www.tcl.tk/man/expect5.31/expect.1.html .

A very simple example - ping an ip address:

        #!/bin/sh
        # The next line is executed by /bin/sh, but not tcl \
        exec /homes/ashnoc01/noc/rbacon/tcl/ActiveTcl/bin/tclsh8.4 $0 ${1+"$@"}

        package require Expect

        spawn -noecho /usr/local/bin/nping -c 5 $argv
        log_user 0
        expect {
                "\r"        {
                        puts -nonewline "$expect_out(buffer)"
                        exp_continue
                        }
                eof        {
                        puts -nonewline "$expect_out(buffer)"
                        }
                }
        puts "AutoPing finished\n"
        exit

Output:

  > AutoPing 62.188.74.130
  PING 62.188.74.130: 64 data bytes
  EchoReply from 62.188.74.130: len=64 ttl=242 seq=0 time=81.157 ms.
  EchoReply from 62.188.74.130: len=64 ttl=242 seq=1 time=80.768 ms.
  EchoReply from 62.188.74.130: len=64 ttl=242 seq=2 time=80.709 ms.
  EchoReply from 62.188.74.130: len=64 ttl=242 seq=3 time=80.915 ms.
  EchoReply from 62.188.74.130: len=64 ttl=242 seq=4 time=80.940 ms.

  ----62.188.74.130 PING Statistics----
  5 transmitted, 5 received, 0.00% packet loss.
  round-trip (ms) min/avg/max = 80.709/80.898/81.157
                var/sdev/skew/kurt = 0.030/0.174/0.323/1.311
  AutoPing finished

escargo 20 Sep 2005 - Let's see if I remember this correctly. A frequently made mistake in Expect is to spawn a new process in a procedure, and then not assign the returned process identifier to anything that gets it out of the proc local scope.

Without the pid to pass to other Expect commands, they won't work as intended. It might also be possible to pass the wrong pid because of the overwriting (or not overwriting) the spawn_id.


Category Command | Category Expect