使用 expect 脚本的 mariadb 数据库条目
mariadb database entry using expect script
我想自动添加 mariadb 数据库条目,但在使用 expect 脚本时出现一些错误。
我期望的脚本:
#!/usr/bin/expect
spawn /usr/bin/mysql -u root -p
expect "Enter password:"
send "123456 \r"
expect "MariaDB [(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB [(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB [(none)]>"
send "flush privileges;\r"
expect "MariaDB [(none)]>"
send "exit\r"
但是一直失败,报错信息:
spawn /usr/bin/mysql -u root -p
Enter password: invalid command name "(none)"
while executing
"(none)"
invoked from within
"expect "MariaDB [(none)]>""
(file "./mariadb.sh" line 9)
我做错了什么?
从错误消息来看,问题似乎出在未转义的左方括号 [
上。如果要匹配文字左方括号,那么应该是,
"MariaDB \\[(none)]>"
原因是,[
对于Tcl
和Expect
都是特殊的。除非转义,否则它将被视为命令调用。
将您的代码更改为
expect "MariaDB \\[(none)]>"
注意:我们不需要转义右方括号。
问题解决了,我的脚本:
#!/usr/bin/expect
spawn mysql -u root -p123456
expect "MariaDB \\[(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB \\[(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB \\[(none)]>"
send "flush privileges;\r"
expect "MariaDB \\[(none)]>"
send "exit;\r"
主要问题是密码验证,不要使用任何 space 使用“-p123456”
我想自动添加 mariadb 数据库条目,但在使用 expect 脚本时出现一些错误。 我期望的脚本:
#!/usr/bin/expect
spawn /usr/bin/mysql -u root -p
expect "Enter password:"
send "123456 \r"
expect "MariaDB [(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB [(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB [(none)]>"
send "flush privileges;\r"
expect "MariaDB [(none)]>"
send "exit\r"
但是一直失败,报错信息:
spawn /usr/bin/mysql -u root -p
Enter password: invalid command name "(none)"
while executing
"(none)"
invoked from within
"expect "MariaDB [(none)]>""
(file "./mariadb.sh" line 9)
我做错了什么?
从错误消息来看,问题似乎出在未转义的左方括号 [
上。如果要匹配文字左方括号,那么应该是,
"MariaDB \\[(none)]>"
原因是,[
对于Tcl
和Expect
都是特殊的。除非转义,否则它将被视为命令调用。
将您的代码更改为
expect "MariaDB \\[(none)]>"
注意:我们不需要转义右方括号。
问题解决了,我的脚本:
#!/usr/bin/expect
spawn mysql -u root -p123456
expect "MariaDB \\[(none)]>"
send "create database zabbixdb character set utf8;\r"
expect "MariaDB \\[(none)]>"
send "grant all privileges on zabbixdb.* to 'zabbixuser'@'localhost' identified by '123456';\r"
expect "MariaDB \\[(none)]>"
send "flush privileges;\r"
expect "MariaDB \\[(none)]>"
send "exit;\r"
主要问题是密码验证,不要使用任何 space 使用“-p123456”