欺骗从 shell 脚本中运行的程序,使其认为它正在从终端读取

发布于 2024-10-31 19:08:39 字数 313 浏览 4 评论 0原文

我想编写一个 shell 脚本,执行如下所示的操作,

while read line; do
echo $line 
done<input.txt | ssh > output.txt

目前这有点伪代码(原始版本正在工作),但您应该能够知道它在做什么。对于简单的应用程序来说,这很不错,但是 ssh 会检查输入以查看它的标准输入是否是终端。

有没有办法让 ssh 认为我的管道循环的内容是终端而不是管道?

编辑:抱歉最初没有添加此内容,这是为了允许 ssh 通过 shell 脚本登录(回答密码提示)

I'd like to write a shell script that does something like the following

while read line; do
echo $line 
done<input.txt | ssh > output.txt

This is a bit pseudo codey at the moment (the original is at work), but you should be able to tell what it's doing. For simple applications this works a treat, but ssh checks the input to see whether it's stdin is a terminal.

Is there a way to fool ssh into thinking that the contents of my piped loop are a terminal rather than a pipe?

EDIT : Sorry for not adding this originally, this is intended to allow ssh to log in via the shell script (answering the password prompt)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海蓝天 2024-11-07 19:08:39

ssh -t -t 将执行您想要的操作 - 这告诉 ssh 分配一个伪终端,无论它是否实际在其中运行。

ssh -t -t will do what you want - this tells ssh to allocate a pseudo terminal no matter whether it is actually running in one.

绝情姑娘 2024-11-07 19:08:39

更新

这个问题(在更新您的问题和各种评论后,很明显您正在寻找一种方便地实现公钥加密的方法)也许可以通过“颠倒思考”来解决。

您可以尝试从该服务器接收经过身份验证的身份(私钥),而不是努力将客户端公钥发送到尚未对客户端进行身份验证的服务器上。

简单来说:在服务器而不是客户端生成密钥对,然后想办法在客户端获取密钥对。服务器可以提前将公钥放入其authorized_keys中,以便客户端可以立即连接。

可能

  • 如果选择安全性较低的机制(方便性优于安全性),那么获取密钥的问题
  • 会更容易解决(您甚至可以使用“组”密钥从不同的客户端进行访问) ,至少只考虑到密钥的安全性。客户端减少了,但没有服务器减少那么多(直接)。

原答案:

简短回答:不。 (这将是 ssh 的安全漏洞,因为 ssh “信任”tty 来输入密码,并且仅信任 tty)

长答案,您可以尝试颠覆/创造性地使用终端模拟器(查看 script/scriptreplay 以获得灵感)。

你为什么要这么做?

Update

This problem (after updating your question and various comments, it became clear you are looking for a way to conveniently get public key encryption into place) could perhaps be solved by 'thinking upside down'.

Instead of trying very hard to get your clients public key onto a server that doesn't yet authenticate the client, you can try to receive an authenticated identity (private key) from that server.

Simple terms: generate a keypair on the server instead of the client, and then find a way to get the keypair on the client. The server can put the public key in it's authorized_keys in advance, so the client can connect right away.

Chances are that

  • the problem of getting the key across is more easily solved (you could even use a 'group' key for access from various clients)
  • if a less-secure mechanism is chosen (convenience over security) at least only the security of the client is reduced, not as-much that of the server (directly).

Original answer:

Short answer: Nope. (it would be a security hole for ssh, because ssh 'trusts' the tty for password entry, and the tty only)

Long answer, you could try to subvert/creatively use a terminal emulator (look at script/scriptreplay for inspiration).

Why would you want to do it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文