expect登录mysql

#!/usr/bin/expect

# 登录mysql

set user "root"
set host "localhost"
set password "****"
set db "wp-"
set port "3306"
set timeout 3000
spawn mysql -h $host -P $port -u $user -p
expect {
  "*Enter password:*" {
    send "$password\r";
    expect {
      "*mysql>*" {
        send  "SHOW DATABASES;\r";
        expect {
          "*mysql>*" {
            send  "use $db;\r";
            expect {
              "*Database changed*" {
                send  "show tables;\r";
                interact
              }
              "*ERROR*" {
                send  "SHOW DATABASES;\r";
                interact
              }
            }
          }
        }
      }
    }
  }
}
This entry was posted in Linux, shell. Bookmark the permalink.

发表回复