【发布时间】:2016-03-01 14:16:35
【问题描述】:
设置:
- 带有 SQL 脚本
script.sql(Postgres) 的本地 *nix 机器。 - 带有 Postgres 的远程计算机
remote(Debian 7)。- 我可以 SSH 登录为
some_user,他是一个 sudoer。 - 使用 Postgres 的任何操作都需要以
postgres用户身份完成。 - 服务器只监听
localhost:5432。
- 我可以 SSH 登录为
如何在remote 上执行script.sql 而不先将其复制到那里?
这很好用:
ssh -t some_user@remote 'sudo -u postgres psql -c "COMMANDS FOO BAR"'
-t 标志意味着sudo 将在本地终端上正确询问some_user 的密码。
还有一件事,就是能够将script.sql 传送到psql。这不起作用:
ssh -t some_user@remote 'sudo -u postgres psql' < script.sql
失败并显示消息:
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified
编辑:简化示例
Postgres 和psql 似乎在这个问题上没有多大作用。下面的代码有同样的问题:
ssh some_user@remote xargs sudo ls < input_file
问题似乎是:我们需要发送 2 个输入到 sudo,使用 tty 的密码和 stdin 传递给 ls。
编辑:更简单
ssh localhost xargs sudo ls < input_file
sudo: no tty present and no askpass program specified
添加-t 不起作用:
$ ssh -t localhost xargs sudo ls < input_file
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified
添加另一个-t 也不起作用:
$ ssh -t -t localhost xargs sudo ls < input_file
<content of input_file>
<waiting on a prompt>
【问题讨论】:
-
抱歉,无法通过简化示例重现您的错误。这仅在您添加 -t 开关时才会失败。不要用棍子打自己,并声称自己很痛苦。我唯一能想到的就是添加 -t -t 看看问题是否消失。
-
添加了更简化的示例。 -t -t 不起作用。
-
对不起,即使是最简单的例子,仍然无法重现您的问题。我是一名PG,我想我从这个角度回答了你的问题。我希望一个壳家伙在这里插话并弄清楚你的配置。
-
非常感谢,谢谢!