DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Show Output and Internal Diagnostics of Expect Script in Different Terminals


  1. Start a terminal, run tty to get its device file name, "/dev/pts/0" in this case.

  2. Start another terminal. Create a expect script "testDebug.exp":

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    #!/usr/bin/expect 
    set prompt "\\\[hadoop@49server\\s.*\\\]\\\$\\s" 
    spawn telnet 10.0.2.49 
    exp_internal 1 
    expect "login:" 
    send "hadoop\r" 
    expect "Password:" 
    send "h\r" 
    expect -re $prompt 
    send "df -h\r" 
    expect -re $prompt 
    send "exit\r" 
    expect eof
    
  3. Give execution priority to testDebug.exp (using chmod) and run it: ./testDebug.exp 2> /dev/pts/0. You can see all diagnostics information are printed in the terminal "/dev/pts/0" while all "normal" informal are printed in the second terminal window.

Explanation: the command "exp_internal 1" send all pattern diagnostics to standard error, which is specified as "2> ..." in the command line. Meanwhile, a terminal's id can be get from "tty" command.

You can use "exp_internal -f 0" instead of "exp_internal 1" to save all standard output and diagnostics information into file . while only standard output can be seen on the terminal.

Reference: Section "Enabling Internal Diagnostics" and "Logging Internal Diagnostics" in chapter 7:"Debugging Patterns And Controlling Output" of "Exploring Expect" by Don Libes.

Notes: If you run command "ls -l /dev/pts/*", you can get the following outputs:

crw--w---- 1 lichao tty 136, 0 Aug 28 11:56 0
...

Notice that the file type are "c" (the first character of the line), which stands for "character device" (standard input/output, like keyboard and screen, relative to "block device", like hard drives). See "http://en.wikipedia.org/wiki/Unix_file_types#Device_file" for details.



Published

Aug 28, 2013

Last Updated

Aug 28, 2013

Category

Tech

Tags

  • expect 10
  • linux 158
  • redirect 1

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor