Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect within a Bash script

Tags:

bash

expect

I wrote a Bash script with Expect within, to connect to a terminal server and clear lines.

I am unable to figure out the error I am getting, as in I have given all the braces necessary. I also do not understanding the couldn't read file "line": no such file or directory error. How can I fix this?

My script:

#!/bin/bash
VAR=$(expect -c "
spawn telnet 1.1.1.1
expect {
       "Password:" { send "password\r" ; exp_continue}
       "Prompt>" { send "en\r" ; exp_continue}
       "Password:" { send "password\r" ; exp_continue}
       "Prompt#" {send "clea line 10\r" ; exp_continue}
       "[confirm]" {send "Y\r" ; exp_continue}
       "Prompt#" {send "clea line 11\r" ; exp_continue}
       "[confirm]" {send "Y\r" ; exp_continue}
       "Prompt#" {send "exit\r" }
    }
")

echo $VAR

Its output:

missing close-brace
    while executing
"expect"
couldn't read file "line": no such file or directory
like image 399
Pkp Avatar asked Apr 07 '11 03:04

Pkp


1 Answers

The problem is that that the double quotes in the Expect script are treated as the end of the Expect script. Try mixing single quotes with double quotes:

#!/bin/bash
VAR=$(expect -c '
spawn telnet 1.1.1.1
expect {
"Password:" { send "password\r" ; exp_continue}
"Prompt>" { send "en\r" ; exp_continue}
"Password:" { send "password\r" ; exp_continue}
"Prompt#" {send "clea line 10\r" ; exp_continue}
"[confirm]" {send "Y\r" ; exp_continue}
"Prompt#" {send "clea line 11\r" ; exp_continue}
"[confirm]" {send "Y\r" ; exp_continue}
"Prompt#" {send "exit\r" }
}
')
like image 115
David Avatar answered Sep 28 '22 15:09

David