4

shell脚本中实现自动输入密码---expect安装使用

 1 year ago
source link: https://blog.51cto.com/u_13753753/5880356
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

shell脚本中实现自动输入密码---expect安装使用

精选 原创

雍州无名 2022-11-23 10:33:46 博主文章分类:centos7 ©著作权

文章标签 mysql shell脚本 返回结果 文章分类 其它 系统/运维 阅读数149

expect是一个自动交互功能的工具。expect是开了一个子进程,通过spawn来执行shell脚本,监测到脚本的返回结果,通过expect判断要进行的交互输入内容(send),由于linux系统默认不安装此工具,所以使用前先进行安装。

1.安装 expect

yum install expect

2.实现自动输入密码例子

#!/usr/bin/expect
spawn mysql_config_editor set -G lp-mysql56-39-3306 -S /var/lib/mysql/mysql.sock -uroot -p
expect "Enter password:"
send "123456\r"
interact
#!/usr/bin/expect 表示使用expect的shell交互模式
spawn 表示在expect下执行shell脚本
expect 对通过spawn执行的shell脚本的返回进行判断,是否包含""中的字段
send 如果expect监测到了包含的字符串,将输入send中的内容,\r 相当于回车
interact 退出expect返回终端,可以继续输入,否则将一直在expect不能退出到终端

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK