예상을 통한 MySQL 자동화

예상을 통한 MySQL 자동화

프로세스를 자동화해야 mysql_secure_installation하고 이 스크립트를 작성했지만 비참하게 실패하고 데이터베이스에 대한 비밀번호를 설정하고 싶지 않습니다.

#!/usr/bin/expect

set timeout 10
spawn mysql_secure_installation

expect "Enter current password for root (enter for none):"
send -- "\r"
expect "Set root password? [Y/n]"
send  "n\r"
expect "Remove anonymous users? [Y/n]"
send  "Y\r"
expect "Disallow root login remotely? [Y/n]"
send  "n\r"
expect "Remove test database and access to it? [Y/n]"
send "Y\r"
expect "Reload privilege tables now? [Y/n]"
send  "Y\r"
interact

실수:

[root@localhost ansible]# ./mysql.sh
spawn mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): invalid command name "Y/n"
    while executing
"Y/n"
    invoked from within
"expect "Set root password? [Y/n]""
    (file "./mysql.sh" line 8)

답변1

[TCL은 특별하고 "..."보간법이므로

"blah [foo]"

fooTCL이 프로시저( proc또는 다른 언어가 호출할 수 있는 sub또는 ) 호출을 시도하게 합니다 function. 사람들은 반격할 수 있다[

expect "blah \[foo]"

또는 따옴표를 사용하여 {}보간을 비활성화합니다.

expect {blah [foo]}

이것은 현명한 선택입니다. 이 지점을 넘어서는 코드를 사용하지 마십시오!

멍청한 프로그래밍 부서

return이 호출되도록 하는 함수를 만들 수도 있습니다 proc.Y/n[Y/n]

$ expect
expect1.1> proc Y/n {} { return "\[Y/n]" }
expect1.2> puts "abort [Y/n]"
abort [Y/n]
expect1.3> 

이를 통해 [Y/n]보간된 문자열 내에서 작업이 가능합니다. 어쩌면 훨씬 더 문제가 될 수도 있습니다. 이렇게 하면 unknown(n)어디에나 삽입된 대부분의 임의 문자열에 대해 하나를 생성할 수 있기 때문입니다... 물론 이전에 말했듯이 주어진 프로시저 이름이 이미 존재하지 않는 한 이것은 나쁜 생각이므로 사용해서는 안 됩니다.proc[...]proc

expect1.1> proc unknown args { return "\[$args]" }
expect1.2> puts "abort [Y/n]"
abort [Y/n]
expect1.3> puts "already exists [puts -nonewline puts\ ]"
puts already exists 
expect1.4> 

관련 정보