PDA

View Full Version : Expect - when automation is expected.! [Introduction]



c0dist
09-02-2012, 11:20 PM
Introduction:
Automation of tasks is one of those bless that saves hell lot of time of any sysAdmin. Mostly automation is done through scripting. And in case of Linux, through bash or Tcl scripts. Scripts come handy in the case of tasks, which are repetitive or need to be done on a regular basis. But interacting with the application program that requires user's input, for example - partitioning, changing users' password, ssh, ftp, etc, is pretty time consuming. In this situation, "expect" is one such tool, that comes to rescue you.

What is Expect:

Expect is a program to control interactive applications. These applications interactively prompt and expect a user to enter keystrokes in response. By using
Expect, you can write simple scripts to automate these interactions. And using automated interactive programs, you will be able to solve problems that you never
would have even considered before.
- Don Lines (Man behind Expect), in his book, "Exploring Expect"

So we can see using expect we can control the programs that require user inputs too. We'll soon see how. It is an extension to Tcl scripting language.

Some main commands:

spawn
send
expect
interact


We'll see how these commands work, by using a simple program:
Note: You might need to install expect, you can simply by "apt-get install expect".



#!/usr/bin/env expect

# hello.exp
# Description: Hello world program in expect
# Author: RahulB

expect {
"hello" { send "world\n" }
}
exit 0


Now lets disassemble this, line by line..
1. First line is the shebang/ hashbang line that tells which interpreter to use
2. Next 3 lines are the comment section tell us the info about code. Note that extension is .exp.
3. Next is the expect command. Expect command defines the string that is expected from the process. For example when we do ssh, it prompts for password as "password: ". So while writing writing a ssh code, we'll expect a "password: ".
4. In next line, we define what we are expecting or looking for, in this case "hello". and then there is send command, that tell us what is the value we can to send to the process, in this case hello.
Please Note: There are many other ways by which we can define this expect/send clause, I like this one. Matter of personal choice.

5. Last line returns 0 to show successful execution of script.. :)

Note: I've started learning expect today only. So please help me in rectifying any error(s), if present. I was searching for a way to automate ssh login, when I stumbled unto this, have been working reading from last 2-3 hours, and I'm loving it.. :D

I'll be writing some more programs, like getting root shell i.e. su, fdisk, ssh, and more as i'll learn.. :)
Sources:
6 Expect Script Examples (http://www.thegeekstuff.com/2010/10/expect-examples/)
Expect Wiki (http://en.wikipedia.org/wiki/Expect)

Cheers.
Hope this helps.. :)

abhaythehero
09-03-2012, 01:08 PM
You would also like to check out the posts by Hackuin regarding Expect

http://www.garage4hackers.com/f51/tutorial-sed-awk-pen-testers-perspective-860-2.html#post3881
http://www.garage4hackers.com/f51/verify-mail-address-1235.html#post4994

c0dist
09-03-2012, 06:42 PM
yeah read that.. actually that verifying email was the post that increased the curiosity towards this..
will be posting about spawn and interact as soon as I pay my dongle's bill.. :P

c0dist
09-03-2012, 08:36 PM
Hi all,
In this post I'll try to explain about spawn, interact and using command line arguments.. In last post, I told you that - expect, send, spawn and interact are some of the most imp commands in expect.

Lets see what they're in single line definition:
Expect : Expect command defines the string that is expected from the process. For example when we do ssh, it prompts for password as "password: ". Here "password: " is expected string.
Send : Send command tell us what is the value we want to send to the process, in this case of ssh, our password. Send feeds the value as if it's typed by any human.
Spawn : Spawn specifies the [interactive] command we want to execute, say "ssh user@server". Syntax is : "spawn command_to_be_executed"
Interact : Interact is used to pass the control to the spawned process. Interact returns the control to our script after the spawned process ends.

Command line arguments :
Command line args as we all know are the values/ arguments that we send to our script at the time of execution. Lets understand this by an example :

./testScript.exp hello world
In this case "hello" and "world" are the arguments passed from command line. arguments are separated by whitespaces. And they're generally stored in an array or list named as "argv".
Expect stores command line args in list. Enough of theory, time to code. As they say, Talk is dirty. Show me the code.. ;)



#!/usr/bin/env expect

################################################## ###
# Filename: su.exp #
# Description: Script to automate the granting of SU shell using expect #
# Author: RahulB #
################################################## ###

if {[llength $argv] == 0} {
puts "Usage: ./su password"
exit 1
}

set passwd [lindex $argv 0]
spawn sudo su -
expect {
"password" {send "$passwd\r"}
}
interact


Now lets inspect the code.
In this we first check if the command line argument is given or not, if not we print error using puts and exit w/ error code 1.
Note : We've checked the length of list argv using the llength (yeah no typo, it stands for list length). Or we can use $argc. Both are same.

Then we declare the variable passwd. Variables are declared using "set". We read the first element of our list and stores it into passwd.

Then we invoke our interactive command, i.e "sudo su -" using spawn. Then the string "password: " is expect. When this string appears, we send our password, stored in variable followed by "\r".
Please Note : we're sending "\r", not "\n" because former is Carriage Return and latter is new line/ line feed. So it's like we entered our password and pressed Enter key. :)

At last we use interact to pass the control to the shell.. :)

Save it as su.exp and execute these commands:


chmod +x su.exp
./su.exp your_password

Hope it was easy and simple. Suggestions and rectifications invited.. :)

Cheers. :)

b0nd
09-04-2012, 02:09 PM
You would also like to check out the posts by Hackuin regarding Expect

http://www.garage4hackers.com/f51/tutorial-sed-awk-pen-testers-perspective-860-2.html#post3881
http://www.garage4hackers.com/f51/verify-mail-address-1235.html#post4994

I always wonder how you manage to dig older posts! Gotta brain of Einstein or the "search" button loves you ;)?

abhaythehero
09-04-2012, 05:35 PM
I always wonder how you manage to dig older posts! Gotta brain of Einstein or the "search" button loves you ;)?

Just the "search button" :)

P.S for girls : Naa I am kidding. I have brain of Einstein. Shh..shh .. !!

the_empty
09-04-2012, 10:11 PM
Being with abhay has showed me.. he is a living index of Garage4Hackers forum. Really proud of and lucky to have him.