Find the answer to your Linux question:
Results 1 to 2 of 2
Is there a similar thing in expect , as there is 'read'(Read a line from standard input) in bash ? I mean one could easily construct following script in bash: ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    3

    Question 'read' functionality in expect script


    Is there a similar thing in expect, as there is 'read'(Read a line from standard input) in bash?

    I mean one could easily construct following script in bash:

    Code:
    > cat script.sh 
    #!/bin/bash
    
    read -p "Username: " uname
    stty -echo
    read -p "Password: " passwd; echo
    stty echo
    
    
    echo "Now I am able to use username(which is $uname in this case) and password($passwd in this case) as variables in the script."
    However, this:

    Code:
    #!/usr/local/bin/expect -f
    
    read -p "Username: " uname
    stty -echo
    read -p "Password: " passwd; echo
    stty echo
    
    set timeout -1
    spawn telnet -K 192.168.1.10
    match_max 100000
    expect -exact "Username: "
    send -- "$uname\r"
    expect -exact "Password: "
    send -- "$passwd\r"
    expect -exact "homeswitch#"
    send -- "exit\r"
    expect eof
    ..obviously does not work. I know there is "set password" option in expect, but this does look bit pointless as I have to write password into the expect script file anyway, which I need to avoid

    Any suggestions? Or please ask if I was unclear

  2. #2
    Just Joined!
    Join Date
    Oct 2007
    Posts
    15

    RE: expect script interactive read

    Quote Originally Posted by m4rtin View Post

    [/CODE]

    However, this:

    Code:
    #!/usr/local/bin/expect -f
    
    read -p "Username: " uname
    stty -echo
    read -p "Password: " passwd; echo
    stty echo
    
    set timeout -1
    spawn telnet -K 192.168.1.10
    match_max 100000
    expect -exact "Username: "
    send -- "$uname\r"
    expect -exact "Password: "
    send -- "$passwd\r"
    expect -exact "homeswitch#"
    send -- "exit\r"
    expect eof
    If you are not married to the idea of an interactive approach, maybe just specifying the uname and password on the expect command line would suffice. Expect was designed to remove the human from the task performed so under typical use you would call the expect script with command line options and "regexp" them out of $argv (pg. 175 in Libes' Expect book)

    Otherwise, you could double wrapper with the expect script in-lined into a shell script that does what you want up front reading from user's input and then passing those to the expect script as command line args.

Posting Permissions

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