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

neogeek23

我正在尝试在远程服务器上自动安装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 ~]#
neogeek23

事实证明,最好的方法是使用编程语言来注入您想要的任何东西。为此,我退出了想要的数据打印到屏幕后要运行的命令序列。那时,缓冲区中有我要查找的数据,因此我可以从那里获取数据,然后继续执行新的序列,该序列将值插入到所需的位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用bash脚本自动回答交互式cli程序(不是脚本)?

如何在交互式 shell 中设置来自 bash 脚本的输入?

如何在交互式shell之前发送击键以使用GNU屏幕自动进行Linux串行端口登录?

交互式bash脚本

Bash交互式脚本

在Shell中自动进行交互式输入

接收用户输入的交互式 bash 脚本

Bash:如何仅在完整的行中复制交互式脚本的输入/输出?

如何在bash中记录所有终端输入,包括交互式输入?

非交互式bash中的别名

如何在Linux上的python脚本中使用matplotlib 1.5.1进行交互式绘图?

如何在网页上交互式运行python或bash脚本?

如何从 bash 脚本打开交互式 gnuplot 窗口

如何在Java的交互式命令行过程中输入值?

如何在Python中以被动方式将值传递给交互式输入?

没有进行任何更改时,如何在git交互式rebase中发生冲突?

如何在xserver关闭之前运行交互式脚本?

为什么在交互式bash流程中源自脚本的别名没有扩展?

如何在Chrome插件上进行交互式通知

用于备份的交互式 bash 脚本

如何在交互式环境中使用bash命令?

脚本编制:如何在具有正确权限的bash脚本中非交互式调用setreuid()/ setregid()?

如何使交互式命令自动可用?

如何在没有交互式编辑器的情况下使用Bash自动创建cron作业?

如何在非交互式 bash 脚本及其 git 子程序中禁用 ctrl-c

将Whatsapp bash脚本的输出重定向到交互式文件以实现自动化

如何使用Github Actions自动执行(通常)交互式构建脚本?

如何显示交互式报表的报表别名

如何使用多个“输入”函数测试交互式 Python 脚本