5

Hi here is my expect script:

#!/usr/local/bin/expect -f
set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "ssh IP_addr\r"
expect -exact "password: "
send -- "something\r"
expect -exact "\r"
#expect -exact "Entering server port, ..... type ^z for port menu."
send -- "\r"
expect -exact "login: "
send -- "admin\r"
expect -exact "password: "
send -- "something\r"
expect -exact "something > "
send -- "reboot\r"
expect -exact "REBOOT THE SYSTEM? (y or n): "
send -- "y\r"
expect -exact "SYSTEM REBOOTING!\r"
set no 20
for {set i 1} {$i < $no} {incr i 1} { 
    send -- "^[-"   
}
expect -exact "\/----Enter Password----\\"
expec eof

I want to send escape and hyphen character multiple times until I receive "/----Enter Password-----\ prompt. But I'm receiving following error at this line:

missing close-bracket
            while executing
        "'send -- "^[-"'
        "
            ("for" body line 2)
            invoked from within
        "for {set i 1} {$i < $no} {incr i 1} {
                'send -- "^[-"'
        }"
            (file "script_auto.exp" line 31)

I'm newbie to expect. Kindly let me know what does that error mean and how can I resolve it.

1 Answer 1

7

expect is an extension of the Tcl language. In Tcl, square brackets are used for command substitution, exactly the way backticks are used in posix shells.

Change

send -- "^[-"   

to

send -- {^[-}   

The curly braces prevent command substitution, so the open bracket is seen as just a plain character.

6
  • Hi glenn, thanks for replying. I will try this and let you know the results.
    – Aaron88
    Dec 11, 2014 at 1:31
  • Hi glenn, I have one more question. Is it possible use multiple sends without using expect even if something is printing on the screen?
    – Aaron88
    Dec 11, 2014 at 22:28
  • yes, in the same way that you can keep typing in your terminal while some program is working. Not usually recommended though. Dec 11, 2014 at 23:59
  • Like I want to enter Boot Menu of some server, for that I need to keep entering esacpe and hyphen until it goes to boot menu. I'm using for loop to generate multiple escape hyphen sequence. But for some reason it is not going to Boot menu when I run the script. If I do it manually it is going to Boot menu.
    – Aaron88
    Dec 12, 2014 at 18:28
  • I want to write a code like while expect is not exact to "\/----Enter Passowrd----\\" send -- {^[-}. Is it possible?
    – Aaron88
    Dec 12, 2014 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.