Expect programming language is a program to automate interactions between the program and the user that expose a text terminal interface such as telnet, FTP, fsck, rlogin, ssh, and others. It is written by Don Libes in 1990 as a Tcl extension and can be installed on almost any Unix type distribution. This article will explain how to install expect on CentOS any version and how to use it.
We can install expect on CentOS using the command:
Expect scripts are written like many other scripts. Like Bash or Perl the binary is called at the top “#!/usr/bin/expect” and the scripts commands below it. Expect is an extension to the Tcl scripting language so it uses all of Tcl’s syntax. Generally, expect script files has .exp as extensions. There are commands are used in Expect:
|send||To send the strings to the process|
|expect||Wait for the specific string from the process|
|spawn||To start the command|
|timeout||To set maximum time to wait for a connection or response (in seconds or -1 if want to wait forever)|
|interact||To give control of the current process over to the user for interaction|
The example below is expect script to remote ssh from admin to client:
Line 1 used to set expect as the interpreter for this script with option -f tells Expect that it is reading commands from a file.
Line 2 will connect to the server remote with ssh protocol.
Line 3 sets a timeout for each expect a statement to 2 seconds.
Line 4 will expect for the specified text with asterisks show matches zero or more occurrences before the colon.
Line 5 will send password for the ssh connection
Line 6 will expect for the specified text (]# ).
Line 7 will send hostname command in client
Line 8 will expect for the specified text (]# ).
Line 9 will send ip addr and grep 192 commands to the client.
Line 10 will expect for the specified text and if there is no script anymore, will back to admin.
Before You execute the script, please make sure to make the script executable. You must know that expect script only works well if we have already remote to another host. If you use the script to remote for the first time to another host, the script will fail. An example we have two servers (Server A and server B) and we want to use expect script to remote using SSH from A to B.
So, we must remote server B from server A use SSH first before we use expect script. In short, we must first remote to the remote server using normal ssh before we use expect scripts and this applies not only to ssh but also to others.
Expect can use argument and here is example for that:
If we have script above, we must execute the script:
But how to if we want to send command such as Ctrl-D in the remote host using Expect script? To send ASCII code such as Ctrl-D using expect script we must see table below:
In the table above Ctrl-D is represented in the Char part as the eot or end of transmission, so to use it in the expect script to use the code in the Octal part which in this case is 0004, then in the script type:
So the script will send Ctrl-D to the remote host. If we want to type ls command to the remote host using ASCII code in the expect script we can type:
So, the script will send ls to the remote host. Maybe the ASCII table above is less useful if we only use expect script for one server only but it would be useful if we use expect script for multiple servers.