Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
Linux 未使用ssh在远程计算机上执行postgresql查询_Linux_Bash_Postgresql_Ssh_Sh - Fatal编程技术网

Linux 未使用ssh在远程计算机上执行postgresql查询

Linux 未使用ssh在远程计算机上执行postgresql查询,linux,bash,postgresql,ssh,sh,Linux,Bash,Postgresql,Ssh,Sh,我在远程机器上从本地执行postgresql查询时遇到问题 我正在本地计算机上运行的Shell脚本: [postgres@local:~]$ cat main.sh #!/bin/bash REMOTE="postgres@192.168.1.200" PKEY="/home/postgres/pr-key.ppk" SCRIPT_LOCATION=/home/postgres/scripts CRONLOG_LOCATION=/home/postgres/project/cronlogs PG

我在远程机器上从本地执行postgresql查询时遇到问题

我正在本地计算机上运行的Shell脚本:

[postgres@local:~]$ cat main.sh
#!/bin/bash
REMOTE="postgres@192.168.1.200"
PKEY="/home/postgres/pr-key.ppk"
SCRIPT_LOCATION=/home/postgres/scripts
CRONLOG_LOCATION=/home/postgres/project/cronlogs
PGBIN=/usr/pgsql-9.6/bin
PGUSER=postgres
PGDATA=/opt/PostgreSQL/9.6/data
PGDATABASE=postgres
PGPORT=5432
PGHOST=192.168.1.200
BLOG=/home/postgres/blogfile
query=`ssh -i $PKEY $REMOTE $PGBIN/psql -U $PGUSER -h $PGHOST -p $PGPORT -d $PGDATABASE -Atc "select datname from pg_database where datname not in ('template0','template1');"`
echo $query
执行:

[postgres@local:~]$ ./main.sh
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `/usr/pgsql-9.6/bin/psql -U postgres -h 192.168.1.200 -p 5432 -d postgres -Atc select datname from pg_database where datname not in ('template0','template1');'

ssh允许您运行存储在ssh客户端上的脚本文件,该客户端在远程机器上运行,就好像它实际上在远程机器上一样,这使得脚本编写更加容易。如果可以让progree.sql.sh文件在本地上下文中工作,那么

ssh user@hostname 'bash -s' < sql.script_on_this_machine.sh 
sshuser@hostname“bash-s”

我使用钥匙,所以我不必担心这里的密码

正如我在引号和
bash
string的引号解释中所想的那样

请尝试下一步:

...
QUERY='"select datname from pg_database where datname not in ('\'template0\'', '\'template1\'');"'
RESULT=$(ssh ... -Atc "${QUERY}")
echo "${RESULT}"
...

解释后查询没有被引用,请查看输出的最后一行
bash:-c:-Atc选择datname…
即使我引用,我仍然面临问题。如果您有任何建议,请告诉我。为什么要将结果分配给
query
,然后展开该变量而不加引号?考虑运行你的代码并修复它所发现的。@ SivaKrishna,你能发布固定的代码和错误(或者是相同的错误)吗?您可以在调试模式下运行脚本(使用
-x
标志)并发布最后一行吗?为什么要通过
ssh
(postgres只在本地机器上可用)?@SivaKrishna尝试用单引号括住查询,即
-Atc''select…('template0','template1');“
感谢您的快速回复,如果我在远程机器上创建了sql文件,并将输入与psql一起提供给ssh,那么这是可行的。正如我所警告的,远程服务器正在生产中。我不应该创建任何类型的文件。上面脚本(示例)中提到的所有变量值都将动态更改,我理解并执行了r&d,我只对单引号(')和双引号(“”)有问题。有没有不在远程服务器上创建文件的解决方案。谢谢你advance@SivaKrishna,此答案中的代码不要求脚本是远程的。建议您不要让脚本本身运行
ssh-i…
,而是使用
ssh
在远程系统上调用解释器,并向其提供脚本文本以在stdin上执行。@Charles,谢谢您的建议。我在上面粘贴的代码是示例代码,脚本中有ssh命令,如上面所示。正如sKwa建议的,代码现在正在运行。非常感谢您花费您的时间。非常感谢,这是完美的工作,我做了一些小的更改,即RESULT=$(ssh…-Atc${QUERY})。对于$“{QUERY}”,它不起作用意味着抛出错误。无论如何。请您在这一行提供一些小信息,查询='从pg_数据库中选择datname,其中datname不在('\'template0\'','\'template1\'';')。因为我理解单引号中的双引号和“\”和“\”。再次感谢您提供的解决方案。我没有*,typo@SivaKrishna,打字错误应该是“${QUERY}”(对不起),关于引号,我稍后会更新答案。