programing

mariadb를 설치하기 위한 예상 스크립트를 어떻게 작성합니까?

javaba 2022. 9. 16. 22:57
반응형

mariadb를 설치하기 위한 예상 스크립트를 어떻게 작성합니까?

환경: centos7 + mariadb 5.5.64
언제 실행할지 화면에 설치 정보 표시mysql_secure_installation.

# 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): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

mariadb를 설치하기 위한 자동화 예상 스크립트를 작성합니다.

  vim secure.exp
  set timeout 60
  spawn mysql_secure_installation
  expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? [Y/n] " {send "y\r";exp_continue}
      "New password:" {send "123456\r";exp_continue}
      "Re-enter new password:" {send "123456\r";exp_continue}
      "Remove anonymous users? [Y/n]" {send "y\r";exp_continue}
      "Disallow root login remotely? [Y/n]" {send "y\r";exp_continue}
      "Remove test database and access to it? [Y/n]" {send "y\r";exp_continue}
      "Reload privilege tables now? [Y/n]" {send "y\r";exp_continue}
  }

실행하다/usr/bin/expect secure.exp에러가 발생했습니다.

spawn mysql_secure_installation
invalid command name "Y/n"
    while executing
"Y/n"
    invoked from within
"expect {
          "Enter current password for root (enter for none): " {send "\r";exp_continue}
          "Set root password? [Y/n] " {send "y\r";exp..."
    (file "secure.exp" line 3)

다음과 같이 써도 소용없다.

  set timeout 60
  spawn mysql_secure_installation
  expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? \\[Y/n] " {send "y\r";exp_continue}
      "New password:" {send "123456\r";exp_continue}
      "Re-enter new password:" {send "123456\r";exp_continue}
      "Remove anonymous users? \\[Y/n]" {send "y\r";exp_continue}
      "Disallow root login remotely? \\[Y/n]" {send "y\r";exp_continue}
      "Remove test database and access to it? \\[Y/n]" {send "y\r";exp_continue}
      "Reload privilege tables now? \\[Y/n]" {send "y\r";exp_continue}
  }

같은 오류:

invalid command name "Y/n"
    while executing
"Y/n"
    invoked from within
"expect {
      "Enter current password for root (enter for none): " {send "\r";exp_continue}
      "Set root password? \\[Y/n] " {send "y\r";exp_conti..."
    (file "secure.exp" line 3)

그럼 exp 스크립트를 수정하려면 어떻게 해야 하나요?

이러한 스크립트는, 옵션의 출력을 수신할 때까지 대기합니다(timeout -1「타임 아웃 없음」을 의미하며, 다른 응답을 구별할 수 있습니다.yum install그리고.mysql_secure_installation.와 함께#!/bin/expect -fshebang으로서 스크립트를 실행할 수 있습니다.chmod +x.

A) 우선,mariadb_yum.exp(비활성화)su또는sudo):

#!/bin/expect -f
set timeout 30
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_yum.exp \[linux sudo password\]\n"
    exit 1
}
set USERNAME "[exec whoami]"
set PASSWORD [lindex $argv 0];

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/$USERNAME/mariadb_install.log"

spawn sudo yum -y install MariaDB-server
set yum_spawn_id $spawn_id

# On GCE it will never ask for a sudo password:
expect -ex "\[sudo\] password for $USERNAME: " {
   exp_send "$PASSWORD\r"
}

expect {
    # when the package was already installed
    -ex "Nothing to do" {
        send_user "package was already installed\n"
    }
    # when the package had been installed
    -ex "Complete!" {
        send_user "package had been installed\n"
    }
}

expect eof
close $yum_spawn_id
exit 0

B) 그리고mariadb_sec.exp(필요 없음)sudo):

#!/bin/expect -f
set timeout 1
if {[llength $argv] == 0} {
    send_user "Usage: mariadb_sec.exp \[mysql root password\]\n"
    exit 1
}
set PASSWORD [lindex $argv 0];

spawn mysql_secure_installation
set mysql_spawn_id $spawn_id

# optionally, redirect output to log file (silent install)
# log_user 0
# log_file -a "/home/[exec whoami]/mariadb_install.log"

# when there is no password set, this probably should be "\r"
expect -ex "Enter current password for root (enter for none): "
exp_send "$PASSWORD\r"

expect {
    # await an eventual error message
    -ex "ERROR 1045" {
        send_user "\nMariaDB > An invalid root password had been provided.\n"
        close $mysql_spawn_id
        exit 1
    }
    # when there is a root password set
    -ex "Change the root password? \[Y/n\] " {
        exp_send "n\r"
    }
    # when there is no root password set (could not test this branch).
    -ex "Set root password? \[Y/n\] " {
        exp_send "Y\r"
        expect -ex "New password: "
        exp_send "$PASSWORD\r"
        expect -ex "Re-enter new password: "
        exp_send "$PASSWORD\r"
    }
}
expect -ex "Remove anonymous users? \[Y/n\] "
exp_send "Y\r"
expect -ex "Disallow root login remotely? \[Y/n\] "
exp_send "Y\r"
expect -ex "Remove test database and access to it? \[Y/n\] "
exp_send "Y\r"
expect -ex "Reload privilege tables now? \[Y/n\] "
exp_send "Y\r"

expect eof
close $mysql_spawn_id
exit 0

디버깅을 위해 - 또는 답변을 검증하기 위해 실행할 수 있습니다.expect로그 레벨 포함strace 4이것은 아마도 글쓰기에 관한 한 정보원이 얻을 수 있는 최고의 평판일 것이다.expect스크립트는 진행 상황 및 가장 중요한 순서대로 표시되므로 다음과 같이 처리됩니다.

expect -c "strace 4" ./mariadb_yum.exp [linux sudo password]
expect -c "strace 4" ./mariadb_sec.exp [mysql root password]

설명set exp_internal 1는 regex-timeout 출력을 얻기 위해 사용할 수 있습니다.


예를 들어 여러 호스트에서 여러 프로세스를 생성할 수 있기 때문에 혼란을 일으킬 수 있습니다. ssh국소적으로 그리고 그 다음에yum그리고.mysql_secure_installation리모트.추가된$spawn_id대본에 맞게; 맨 아래쪽에 있는 것close콜이 용장화되어 있을 가능성이 있습니다.이는 이미EOF(단순히 설명하겠습니다)spawn&close프로세스:

Thanks for using MariaDB!
 1  close $mysql_spawn_id
 1  exit 0
 2  rename _close.pre_expect close

결론:mariadb_sec.exp스크립트는, 예를 들면, 처음에 패스워드를 송신하지 않고, 다음에 패스워드를 송신했을 때 등, 한층 더 개선될 가능성이 있습니다.ERROR 1045(패스워드가 이미 설정되어 있는 경우).서버 설치 직후에 패스워드를 설정할 필요가 있다고 가정해도 좋을지도 모릅니다(단,yum reinstall같은 결과를 얻을 수 있습니다).모든 케이스를 테스트할 빈 CentOS 컨테이너가 없었습니다.에서 동작하지 않는 한root설치에서 설치 후까지 이를 자동화하려면 두 종류의 비밀번호를 하나의 스크립트로 전달해야 합니다.

주목할 만한 점은 GCE에서sudo패스워드를 요구하지 않습니다.환경에 따라 작은 차이가 있습니다.이러한 Cent는OS 컨테이너 이미지는 다르게 동작합니다.이 경우(없기 때문에)su또는 컨테이너 이미지 검출을 실시합니다).mariadb_yum.exp 수 .30세컨드)


제가 제공할 수 있는 가장 평판이 좋은 소스는 Don Libes @ NIST에 의해 작성된 매뉴얼과 TCL/TK 매뉴얼입니다.또한 SourceForge 프로젝트라고 불리고 있습니다.

괄호는 명령어 대체에 사용될 뿐만 아니라 글로벌 패턴에 대해서도 특수합니다.

하다, 하다, 하다, 하다, 하다, 쓸 수 요.-exact따옴표로 둘러싸인 각 괄호를 이스케이프하면서 전환합니다.

spawn mysql_secure_installation
expect {
    ...
    -exact "Set root password? \[Y/n\] " {send "y\r";exp_continue}
    ...
}

또는 따옴표 대신 중괄호를 사용합니다.

spawn mysql_secure_installation
expect {
    ...
    {Set root password? \[Y/n\] } {send "y\r";exp_continue}
    ...
}

로, 이 하려면 , 「보다 낫다」를 사용합니다.autoexpect:

autoexpect ./mysql_secure_installation

'기다리다'라는 됩니다.script.exp를 참조해 주세요.

언급URL : https://stackoverflow.com/questions/60097125/how-to-write-the-expect-script-to-install-mariadb

반응형