Results 1 to 7 of 7

Thread: Expect - when automation is expected.! [Introduction] Share/Save - My123World.Com!

  1. #1
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    Expect - when automation is expected.! [Introduction]

    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".

    Code:
    #!/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..

    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
    Expect Wiki

    Cheers.
    Hope this helps..
    Last edited by c0dist; 09-02-2012 at 11:49 PM.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  2. #2
    Super Commando Dhruv abhaythehero's Avatar
    Join Date
    Sep 2010
    Location
    Lucknow/Pune,India
    Posts
    466
    Blog Entries
    2
    You would also like to check out the posts by Hackuin regarding Expect

    http://www.garage4hackers.com/f51/tu....html#post3881
    http://www.garage4hackers.com/f51/ve....html#post4994
    In the world of 0s and 1s, are you a zero or The One !

  3. #3
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1
    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
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  4. #4
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    Spawn, Interact and Command Line Args

    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 :
    Code:
     ./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..

    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:
    Code:
    chmod +x su.exp
    ./su.exp your_password
    Hope it was easy and simple. Suggestions and rectifications invited..

    Cheers.
    Last edited by c0dist; 09-03-2012 at 09:09 PM.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  5. #5
    ... I am no Expert b0nd.g4h@gmail.com b0nd's Avatar
    Join Date
    Jul 2010
    Location
    irc.freenode.net #g4h
    Posts
    744
    Quote Originally Posted by abhaythehero View Post
    You would also like to check out the posts by Hackuin regarding Expect

    http://www.garage4hackers.com/f51/tu....html#post3881
    http://www.garage4hackers.com/f51/ve....html#post4994
    I always wonder how you manage to dig older posts! Gotta brain of Einstein or the "search" button loves you ?
    [*] To follow the path: look to the master, follow the master, walk with the master, see through the master,
    ------> become the master!!! <------
    [*] Everyone has a will to WIN but very few have the will to prepare to WIN
    [*] Invest yourself in everything you do, there's fun in being serious

  6. #6
    Super Commando Dhruv abhaythehero's Avatar
    Join Date
    Sep 2010
    Location
    Lucknow/Pune,India
    Posts
    466
    Blog Entries
    2
    Quote Originally Posted by b0nd View Post
    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 .. !!
    In the world of 0s and 1s, are you a zero or The One !

  7. #7
    InfoSec Consultant the_empty's Avatar
    Join Date
    Jul 2010
    Location
    the blue no-where
    Posts
    155
    Blog Entries
    2
    Being with abhay has showed me.. he is a living index of Garage4Hackers forum. Really proud of and lucky to have him.
    ACCESS is GOD

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •