Home  >  Article  >  Database  >  用expect免密码进入MySQL命令模式

用expect免密码进入MySQL命令模式

WBOY
WBOYOriginal
2016-06-07 16:16:521842browse

一般情况下,我们想要进入MySQL命令模式总是需要按如下交互输入密码确认,才能进入命令行模式: zhanhailiang@linux-06bq:~ mysql -u sl -p Enter password: 其实我们完全可以使用expect编写脚本,来通来expect与shell交互通信来实现免密码登录: zhanhailia

  一般情况下,我们想要进入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手册或其它资料。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn