Cyberpunk7711
Cyberpunk7711

Reputation: 119

Use Expect script with Expect/send

I want to get the value from the SQL command below. But I've got an error message every time.

The Expect code:

#!/usr/bin/expect
set timeout 60

spawn ssh -i id_rsa [email protected]
sleep 10
send -- "sudo su\r"
sleep 10
expect '[sudo] password for user: '
send -- "secret\r"
sleep 2
send "mysql -u root\r"
sleep 2
send -- "use nextcloud;\r"
sleep 2
send -- "SELECT * FROM oc_accounts;\r"

Error message:

spawn ssh -i id_rsa [email protected]
invalid command name "sudo"
    while executing
"sudo"
    invoked from within
"expect '[sudo] password for user: ' "
    (file "./test.sh" line 8)

Upvotes: 1

Views: 1651

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

Single quotes have no special meaning in Expect. Expect is an extension of Tcl, where braces are the non-interpolating quoting mechanism.

You get the "invalid command name" error because square brackets are the Tcl command substitution syntax.

You want

expect {[sudo] password for user: }

Instead of sleep, with idiomatic Expect you should expect some pattern: your remote shell's prompt, the MySQL prompt, etc.


@pynexj has a good point. Try

expect -exact {[sudo] password for user:}

Upvotes: 1

Related Questions