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.

Aaron88
  • 177
  • 1
  • 8
  • 19

1 Answers1

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.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Hi glenn, thanks for replying. I will try this and let you know the results. – Aaron88 Dec 11 '14 at 01: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 '14 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. – glenn jackman Dec 11 '14 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 '14 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 '14 at 18:34
  • I have posted the question here: http://stackoverflow.com/questions/27450355/while-loop-in-expect-script. Kindly help me with this. – Aaron88 Dec 12 '14 at 19:15