Sunday, April 3, 2016

Expect Scripts: How to Automate Your Tasks

Task to be Automated

Below shows an interactive session that psm — an Oracle Application Container Cloud Service command line tool—prompted a user for needed authentication and authorization information before he/she can sign in to an Oracle Cloud service and work on a specific identity domain.

$ psm setup
Username: weblogic1 Password: Retype Password: Identity domain: myIdDomain Region [us]: http://anycloudserver.example.com:7103 Output format [json]:
In this article, we will demonstrate how to utilize Expect to automate the above task , but not focus on the correctness of the information needed by psm.

Expect


Expect is an extension to the Tcl scripting language that "talks" to other interactive programs according to a script.[1] Following the script, Expect knows what can be expected from a program and what the correct response should be.

It can be used to automate control of interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others including psm . Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Finally, Tk, another Tcl extension, can be used to provide a GUI.


Expect Script


To automate the said task, we need to write an Expect script (i.e., psmSetup.exp) as shown below:

psmSetup.exp
#!/usr/bin/expect -f #exp_internal 1 set argDomain [lindex $argv 0] spawn psm setup expect "Username: " send "weblogic\r" expect "Password: " send "welcome1\r" expect "Retype Password: " send "welcome1\r" expect "Identity domain: " send "$argDomain\r" expect "Region \\\[us\\\]: " send "http://anycloudserver.example.com:7103\r" expect "Output format \\\[json\\\]: " send "\r" expect "\r" spawn psm accs apps expect "\r"

Expect scripts can have any file name suffix you like, though they generally have an .exp extension. Read [5] for more details.

How to Debug Expect Scripts?


Writing an Expect script the first time, it is easy to be completely lost and not getting the result you expect. In this case, un-comment the following line in psmSetup.exp:

#exp_internal 1

Setting "exp_internal 1" at the beginning of an Expect script is similar to -d flag (When using Expectk, this option is specified as -diag.), which enables some diagnostic output. This primarily reports internal activity of commands such as expect and interact. In addition, the strace command is useful for tracing statements, and the trace command is useful for tracing variable assignments.

How to Pass Variables from Shell Script to Expect Script?


Say, if you write a Korn shell script as below:

#!/bin/ksh
...

./psmSetup.exp $domain

and would like to pass $domain from shell script to psmSetup.exp script, you can add the following line to the Expect script:

set argDomain [lindex $argv 0]

To reference the Expect variable (i.e, argDomain), you use prefix "$" as below:

expect "Identity domain: "
send "$argDomain\r"

In the above psm dialog, it depicts the interaction between a sender (i.e., end user) and a receiver (i.e. psm) as:
"Identity domain: " is the prompt you "expect" from the psm; therefore, you enter that expected response (i.e., "$argDomain\r") by using "send".
Read [7] for more explanation.

How to Escape Special Characters?


In the below dialog,
Region [us]: http://anycloudserver.example.com:7103
psm will prompt the user for a response:

Region [us]: 

which includes some special characters "[" and "]". To escape special characters in Expect, you can use backslash. However, to protect backslash from being substituted, you actually need to use "\\\" in front of both "[" and "]":[9-12]
expect "Region \\\[us\\\]: "
send "http://anycloudserver.example.com:7103\r"

References

  1. Expect User Command
  2. Tcl
  3. How to pass variables from shell script to expect script?
  4. How to write a script that accepts input from a file or from stdin?
  5. Using Expect Scripts
  6. Debugging Expect Programs
  7. Using Expect Scripts to Automate Tasks
  8. How to escape unusual/uniq characters from expect scripts?
  9. Passing '\' In Username To Expect
  10. Problem in expect script with password involving trailing backslash
  11. How to send escape characters through Expect
  12. How to escape unusual/uniq characters from expect scripts?
  13. Oracle Application Container Cloud Service
  14. Introduction to the Oracle VM Command Line Interface (CLI)
  15. All Cloud-related articles on Xml and More
  16. Tcl Commands (Tcl 8.4)
  17. New Regular Expression Features in Tcl 8.1
  18. Understanding Login Authentication

1 comment:

swathi said...

It is very use full content, thank you so much
Python Training
Python Online Training