Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在automated bash脚本中发送存储值而不是别名以进行交互式输入_Mysql_Linux_Bash_Automation_Expect - Fatal编程技术网

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

Mysql 如何在automated bash脚本中发送存储值而不是别名以进行交互式输入,mysql,linux,bash,automation,expect,Mysql,Linux,Bash,Automation,Expect,我正在尝试在远程服务器上自动安装mysql。我使用golang来做各种事情,其中之一就是在bash中执行一个expect脚本,该脚本有几个步骤。其中一个步骤是安装mysql,然后运行mysql\u secure\u安装。它要求用户输入的第一个提示是密码。此密码是在运行时创建的,因此我无法提前知道是否将其作为文本值包含,以便发送。我从日志中获取密码并将其存储在变量中,但是如何获取该值并将其发送到提示符中使用呢。我知道我想发送它,但我认为我一直在发送别名,而不是值,因为我遇到了访问错误。如何发送值而

我正在尝试在远程服务器上自动安装mysql。我使用golang来做各种事情,其中之一就是在bash中执行一个
expect
脚本,该脚本有几个步骤。其中一个步骤是安装mysql,然后运行
mysql\u secure\u安装
。它要求用户输入的第一个提示是密码。此密码是在运行时创建的,因此我无法提前知道是否将其作为文本值包含,以便
发送
。我从日志中获取密码并将其存储在变量中,但是如何获取该值并将其发送到提示符中使用呢。我知道我想
发送
它,但我认为我一直在发送别名,而不是值,因为我遇到了访问错误。如何发送值而不是别名?我是一个新的期待脚本,所以任何见解都是值得赞赏的

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
/usr/bin/expect-c:
繁殖sshroot@a.b.c.d
睡眠3
期望“#”
发送“yum-y更新\r”
睡眠10
期望“#”
发送“yum-y升级\r”
睡眠10
期望“#”
发送“yum-y localinstall”https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm\r“
睡眠8
期望“#”
发送“yum-y--disablerepo=mysql80社区--enablerepo=mysql57社区安装mysql社区服务器\r”
睡眠5
期望“#”
发送“服务mysqld启动\r”
睡眠3
期望“#”
发送“temppass=\$\(grep)”为其生成临时密码root@localhost“/var/log/mysqld.log | tail-l\)\r”
睡眠1
期望“#”
发送“shortpass=\${temppass:\(-12\)}\r”
睡眠1
期望“#”
发送“echo\$shortpass\r”
睡眠1
期望“#”
发送“mysql\u安全\u安装\r”
睡眠10
应为“输入用户根用户的密码:”
发送“\$shortpass\r”
睡眠10
应为“新密码:”
发送“\r”
睡眠10
期望“重新输入新密码:”
发送“\r”
睡眠10
期望“更改root用户的密码”(按y键表示是,按任何其他键表示否):
发送“y\r”
睡眠10
应为“新密码:”
发送“\r”
睡眠10
期望“重新输入新密码:”
发送“\r”
睡眠10
预期为“删除匿名用户”(按y键表示是,按任何其他键表示否):
发送“y\r”
睡眠10
预期“不允许根用户远程登录”(按y键表示是,按任何其他键表示否):
发送“n\r”
睡眠10
期望“删除测试数据库并访问它?”(按y键表示是,按任何其他键表示否):“
发送“y\r”
睡眠10
期望“立即重新加载特权表”(按y键表示是,按任何其他键表示否):
发送“y\r”
睡眠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 ~]#
spawn-sshroot@a.b.c.d
上次登录:2019年4月29日星期一13:25:07从t.x.y.z
[root@rias-e2e-server-a-segment-purv~]#yum-y更新
加载的插件:FastTestMirror
从缓存的主机文件加载镜像速度
没有标记要更新的包
[root@rias-e2e-server-a-segment-purv~]#yum-y升级
加载的插件:FastTestMirror
从缓存的主机文件加载镜像速度
没有标记要更新的包
[root@rias-e2e-server-a-segment-purv~]#yum-y localinstallhttps://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
加载的插件:FastTestMirror
mysql80-community-release-el7-1.noarch.rpm | 25 kB 00:00:00
检查/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:不更新已安装的软件包。
无事可做
[root@rias-e2e-server-a-segment-purv~]#yum-y--disablerepo=mysql80社区--enablerepo=mysql57社区安装mysql社区服务器
加载的插件:FastTestMirror
从缓存的主机文件加载镜像速度
已安装与mysql-community-server-5.7.26-1.el7.x86_64匹配的包。正在检查更新。
无事可做
[root@rias-e2e-server-a-segment-purv~]#服务mysqld启动
重定向到/bin/systemctl启动mysqld.service
[root@rias-e2e-server-a-segment-purv~]#temppass=$(grep'为其生成临时密码root@localhost'/var/log/mysqld.log | tail-l)
[root@rias-e2e-server-a-segment-purv~]#短程=${temppass:(-12)}
[root@rias-e2e-server-a-segment-purv~]#echo$shortpass
e3H-*HGHu_uuu7
[root@rias-e2e-server-a-segment-purv~]#mysql#u安全_安装
保护MySQL服务器部署的安全。
输入root用户的密码:
错误:拒绝用户“root”@“localhost”的访问(使用密码:是)
[root@rias-e2e-server-a-segment-purv~]#
-bash:Mysqlpass1!:未找到命令
[root@rias-e2e-server-a-segment-purv~]#
-bash:Mysqlpass1!:未找到命令
[root@rias-e2e-server-a-segment-purv~]#y
-bash:y:未找到命令
[root@rias-e2e-server-a-segment-purv~]#
-bash:Mysqlpass1!:未找到命令
[root@rias-e2e-server-a-segment-purv~]#
-bash:Mysqlpass1!:未找到命令
[root@rias-e2e-server-a-segment-purv~]#y
-bash:y:未找到命令
[root@rias-e2e-server-a-segment-purv~]#n
-bash:n:未找到命令
[root@rias-e2e-server-a-segment-purv~]#y
-bash:y:未找到命令
[root@rias-e2e-server-a-segment-purv~]#

事实证明,最好的方法是使用编程语言来注入您想要的任何内容。我这样做是通过在我想要的数据打印到屏幕上后退出我想要运行的命令序列。此时,缓冲区中有我要查找的数据,因此我可以从那里获取它,然后继续执行一个新的序列在我想要的地方注入该值。

事实证明,最好的方法是使用编程语言注入任何你想要的内容。我这样做是通过在我想要的数据打印到屏幕上后退出我想要运行的命令序列。此时缓冲区中有我正在寻找的数据,因此我可以从那里获取它,然后继续nue使用一个新序列,将该值注入到我想要的位置