简体   繁体   English

如何在自动bash脚本中发送存储值而不是别名进行交互式输入

[英]How to send stored value rather than the alias in automated bash script for interactive input

I'm trying to automate the installation of mysql on a remote sever. 我正在尝试自动在远程服务器上安装mysql。 I'm using golang to do various things, one of which is executing an expect script in bash that has several steps. 我使用golang做各种事情,其中之一就是执行的expect在几个步骤bash脚本。 One of the steps is installing mysql and then running mysql_secure_installation . 步骤之一是安装mysql,然后运行mysql_secure_installation The first prompt it asks the user for is a password. 它要求用户输入的第一个提示是密码。 This password is created at runtime so I don't know it ahead of time to include it as a literal value that I can send . 这个密码是在运行时创建的,所以我不知道它是否可以作为我可以send的文字值。 I get the password from the log and store that in a variable, but how do I take that value and send it to be used within a prompt. 我从日志中获取密码并将其存储在变量中,但是如何获取该值并将其发送以在提示中使用。 I know I want to send it but I think I keep sending the alias and not the value as I am getting an access error. 我知道我想send它,但是我在收到访问错误时一直发送别名而不是值。 How do I send the value and not the alias? 如何发送值而不是别名? I'm kind of new to expect scripting so any insight is appreciated. 我是新来的期待脚本编写的人,因此感谢您提供任何见识。

expect script as produced with golang: 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

Actual output: 实际输出:

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 ~]#

Turns out the best thing to do is to use a programming language to inject whatever you want. 事实证明,最好的方法是使用编程语言来注入您想要的任何东西。 I did this by exiting the sequence of commands that I wanted to run after the data I wanted was printed to the screen. 为此,我退出了想要的数据打印到屏幕后要运行的命令序列。 At that point the buffer has the data I'm looking for, so I can get it from there and then continue with a new sequence that injects that value where I want. 那时,缓冲区中有我要查找的数据,因此我可以从那里获取数据,然后继续执行新的序列,将其插入我想要的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM