mysql - 如何在自动 bash 脚本中发送存储值而不是别名以进行交互式输入
问题描述
我正在尝试在远程服务器上自动安装 mysql。我正在使用 golang 做各种事情,其中之一是expect
在 bash 中执行一个包含几个步骤的脚本。其中一个步骤是安装 mysql 然后运行mysql_secure_installation
. 它要求用户输入的第一个提示是密码。这个密码是在运行时创建的,所以我不知道提前将它作为我可以包含的文字值send
。我从日志中获取密码并将其存储在变量中,但是如何获取该值并将其发送以在提示中使用。我知道我想要send
它,但我想我一直在发送别名而不是值,因为我收到了访问错误。如何发送值而不是别名?我对编写脚本有点陌生,因此感谢您的任何见解。
expect
使用 golang 生成的脚本:
/usr/bin/expect -c:
spawn ssh root@a.b.c.d
sleep 3
expect "# "
send "yum -y update\r"
sleep 10
expect "# "
send "yum -y upgrade\r"
sleep 10
expect "# "
send "yum -y localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm\r"
sleep 8
expect "# "
send "yum -y --disablerepo=mysql80-community --enablerepo=mysql57-community install mysql-community-server\r"
sleep 5
expect "# "
send "service mysqld start\r"
sleep 3
expect "# "
send "temppass=\$\(grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log | tail -l\)\r"
sleep 1
expect "# "
send "shortpass=\${temppass:\(-12\)}\r"
sleep 1
expect "# "
send "echo \$shortpass\r"
sleep 1
expect "# "
send "mysql_secure_installation\r"
sleep 10
expect "Enter password for user root: "
send "\$shortpass\r"
sleep 10
expect "New password: "
send "<pass>\r"
sleep 10
expect "Re-enter new password: "
send "<pass>\r"
sleep 10
expect "Change the password for root ? \(\(Press y|Y for Yes, any other key for No\) : "
send "y\r"
sleep 10
expect "New password: "
send "<pass>\r"
sleep 10
expect "Re-enter new password: "
send "<pass>\r"
sleep 10
expect "Remove anonymous users? \(Press y|Y for Yes, any other key for No\) : "
send "y\r"
sleep 10
expect "Disallow root login remotely? \(Press y|Y for Yes, any other key for No\) : "
send "n\r"
sleep 10
expect "Remove test database and access to it? \(Press y|Y for Yes, any other key for No\) : "
send "y\r"
sleep 10
expect "Reload privilege tables now? \(Press y|Y for Yes, any other key for No\) : "
send "y\r"
sleep 10
实际输出:
spawn ssh root@a.b.c.d
Last login: Mon Apr 29 13:25:07 2019 from t.x.y.z
[root@rias-e2e-server-a-segment-purv ~]# yum -y update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No packages marked for update
[root@rias-e2e-server-a-segment-purv ~]# yum -y upgrade
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No packages marked for update
[root@rias-e2e-server-a-segment-purv ~]# yum -y localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
Loaded plugins: fastestmirror
mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00
Examining /var/tmp/yum-root-S1evUc/mysql80-community-release-el7-1.noarch.rpm: mysql80-community-release-el7-1.noarch
/var/tmp/yum-root-S1evUc/mysql80-community-release-el7-1.noarch.rpm: does not update installed package.
Nothing to do
[root@rias-e2e-server-a-segment-purv ~]# yum -y --disablerepo=mysql80-community --enablerepo=mysql57-community install mysql-community-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package matching mysql-community-server-5.7.26-1.el7.x86_64 already installed. Checking for update.
Nothing to do
[root@rias-e2e-server-a-segment-purv ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
[root@rias-e2e-server-a-segment-purv ~]# temppass=$(grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log | tail -l)
[root@rias-e2e-server-a-segment-purv ~]# shortpass=${temppass:(-12)}
[root@rias-e2e-server-a-segment-purv ~]# echo $shortpass
e3H-*HGHu__7
[root@rias-e2e-server-a-segment-purv ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)
[root@rias-e2e-server-a-segment-purv ~]# <pass>
-bash: Mysqlpass1!: command not found
[root@rias-e2e-server-a-segment-purv ~]# <pass>
-bash: Mysqlpass1!: command not found
[root@rias-e2e-server-a-segment-purv ~]# y
-bash: y: command not found
[root@rias-e2e-server-a-segment-purv ~]# <pass>
-bash: Mysqlpass1!: command not found
[root@rias-e2e-server-a-segment-purv ~]# <pass>
-bash: Mysqlpass1!: command not found
[root@rias-e2e-server-a-segment-purv ~]# y
-bash: y: command not found
[root@rias-e2e-server-a-segment-purv ~]# n
-bash: n: command not found
[root@rias-e2e-server-a-segment-purv ~]# y
-bash: y: command not found
[root@rias-e2e-server-a-segment-purv ~]#
解决方案
事实证明,最好的办法是使用一种编程语言来注入你想要的任何东西。我通过在我想要的数据打印到屏幕后退出我想要运行的命令序列来做到这一点。那时缓冲区有我正在寻找的数据,所以我可以从那里得到它,然后继续一个新的序列,在我想要的地方注入那个值。
推荐阅读
- label - Jena:推理规则的标签定义错误
- laravel - 试图获取非对象的属性“image_url”(查看:/app/resources/views/library.blade.php)
- mongodb - 使用 mongoose 在 mongodb 集合中添加带有字符串的电子邮件地址
- r - 重新排列 R 中热图的数据框
- youtube - 为什么我的 YouTube 订阅按钮变暗且无法点击?
- laravel - 在 PDF 上导出希腊字符显示为框
- amazon-web-services - 如何增加 AWS CPU 积分余额降至零?
- javascript - 异步等待在带有回调的函数调用中无法正常工作
- linux - 使用文本文件(包含文件名)将文件从当前目录复制到新目录
- react-native - 如何将 Blob 保存到 React Native 中的临时本地存储中?