我遇到了一些问题,我的脚本应该是SSHs进入我的服务器,运行一个mysql查询,并返回一个特定的值。
我的问题是,当我使用命令替换$()和时,我会得到一个拒绝访问的错误--但是如果没有命令替换,我就无法将结果保存到变量中。
我有完全的特权,密码是正确的。
ssh root@SERVER.com << EOFMARK
example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")
echo "Variable is $example"
EOFMARK
echo "My goal is to read the variable here: $example"发布于 2014-03-07 08:05:01
你可能会想
这条路:
example=$(ssh root@SERVER.com mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e '"SELECT now();"')
echo "My goal is to read the variable here: $example"会是个好去路。
如果您在两个地方都确实需要它,您可能希望在服务器上有一个脚本,或者您可以尝试。
example=$(ssh root@SERVER.com 'example=$(mysql -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"为了坚持下去,你不妨试试。
example=$(ssh root@SERVER.com << EOFMARK
example=$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")
echo "Variable is $example"
EOFMARK)
echo "My goal is to read the variable here: $example"以便在客户端也具有该变量。
不管怎么说,我无法理解这个例子。
但其他人应该看起来像
example=$(ssh root@SERVER.com 'mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"')
echo "My goal is to read the variable here: $example",它准确地将输出输入到局部变量,或者
example=$(ssh root@SERVER.com 'example=$(mysql -NB -h localhost -u root -p"$MYSQL_PASSWORD" myDatabase -e "SELECT now();"); echo "Variable is $example")
echo "My goal is to read the variable here: $example"但是您应该知道,在这里,本地输出看起来像(例如)。
My goal is to read the variable here: Variable is 2014-03-07 20:42:23当子字符串Variable is也得到输出时。
发布于 2014-03-07 07:27:49
您应该将必须在远程主机上执行的替换转义:
尝试:
ssh root@SERVER.com << EOFMARK
example=\$(mysql -h localhost -u root -p$MYSQL_PASSWORD myDatabase -e "SELECT now();")
echo "Variable is \$example"
EOFMARK您可以遵循@glglgl建议,并将ssh命令的标准输出捕获到局部变量中。但我会使用本地mysql客户端,并授予访问数据库的权限。如果您有一个防火墙,阻止您远程访问3306,我将ssh转发端口。
example=$(mysql -h SERVER.com -u root -p myDatabase -e "SELECT now();") 这是正确的方法。没有ssh,交互密码,不能通过ps -ef| grep mysql访问
https://stackoverflow.com/questions/22243890
复制相似问题