萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> 用expect免密碼進入MySQL命令模式

用expect免密碼進入MySQL命令模式

  一般情況下,我們想要進入MySQL命令模式總是需要按如下交互輸入密碼確認,才能進入命令行模式:

  zhanhailiang@linux-06bq:~> mysql -u sl -p

  Enter password:

  其實我們完全可以使用expect編寫腳本,來通來expect與shell交互通信來實現免密碼登錄:

  zhanhailiang@linux-06bq:~> cat mysql.sh

  #!/usr/local/bin/expect

  spawn /usr/local/services/mysql/bin/mysql -u sl -p

  expect {

  "assword" { send "slr" }

  }

  interact

  其實這段腳本的意思就是說“執行/usr/local/services/mysql/bin/mysql -u sl -p,等待響應,若匹配assword,則發送sl(即MySQL用戶登錄密碼),回車,模擬用戶手工操作”。

  編寫完mysql.sh腳本後,賦予執權限,然後就可以通過執行mysql.sh來免密碼進入MySQL命令行模式。

  zhanhailiang@linux-06bq:~> chmod +x mysql.sh

  zhanhailiang@linux-06bq:~> ./mysql.sh

  spawn /usr/local/services/mysql/bin/mysql -u sl -p

  Enter password:

  Welcome to the MySQL monitor. Commands end with ; or g.

  Your MySQL connection id is 12912

  Server version: 5.5.14-log MySQL Community Server (GPL)

  Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

  Oracle is a registered trademark of Oracle Corporation and/or its

  affiliates. Other names may be trademarks of their respective

  owners.

  Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

  mysql>

  注:expect擁有眾多的參數及豐富的應用場景,有興趣的同學請自行查看man手冊或其它資料。

copyright © 萬盛學電腦網 all rights reserved