我正在尝试在远程服务器上自动安装mysql。我正在使用golang做各种事情,其中之一是expect
在bash中执行一个脚本,该脚本有几个步骤。步骤之一是安装mysql,然后运行mysql_secure_installation
。它要求用户输入的第一个提示是密码。该密码是在运行时创建的,所以我不会提前知道将它作为我可以包含的文字值send
。我从日志中获取密码并将其存储在变量中,但是如何获取该值并将其发送以在提示中使用。我知道我想要send
它,但是我认为我一直在发送别名而不是值,因为我遇到访问错误。如何发送值而不是别名?我是新来的期待脚本编写的人,因此感谢您提供任何见识。
expect
用golang生成的脚本:
/usr/bin/expect -c:
spawn ssh [email protected]
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 [email protected]
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 ~]#
事实证明,最好的方法是使用编程语言来注入您想要的任何东西。为此,我退出了想要的数据打印到屏幕后要运行的命令序列。那时,缓冲区中有我要查找的数据,因此我可以从那里获取数据,然后继续执行新的序列,该序列将值插入到所需的位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句