使用deno通过ssh挂在p.Output()上

发布于 2025-02-02 06:13:44 字数 839 浏览 3 评论 0原文

我希望创建一个SSH子过程,然后与服务器进行交互。我挂了一个基本步骤,即只需等到SSH过程连接。我知道此SSH命令连接正常,因为当我使用sashit而不是piped运行它时,SSH Shell按预期显示。

如果我正确理解,p.Output() lisk lick lick in lat,直到达到eof。我假设当SSH连接时,它会流式传输stdout,但不eof,因此p.Output()永远不会被调用。

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const p=Deno.run({
     cmd: ["ssh", "root@mywebsite"],
     stdout: "piped",
     stderr: "piped",
     stdin: "piped"
});

const command = (cmd : string) => p.stdin.write(encoder.encode(cmd))
const getOutput = async () => decoder.decode(await p.output())

await p.output() // <----- Hangs here
await command("cd /home/dev/www")
await command("ls -la")
console.log(await getOutput())

await p.status()
console.log("done")

I'm looking to create an SSH sub-process and then interact with the server. I'm hung up on a basic step which is to simply wait until the SSH process has connected. I know that this ssh command connects fine because when I run it with inherit instead of piped, the ssh shell shows up as expected.

If I understand correctly, p.output() listens for stdout until it reaches EOF. I'm assuming that when SSH has connected, it streams the stdout, but does not EOF, and so p.output() never gets called.

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const p=Deno.run({
     cmd: ["ssh", "root@mywebsite"],
     stdout: "piped",
     stderr: "piped",
     stdin: "piped"
});

const command = (cmd : string) => p.stdin.write(encoder.encode(cmd))
const getOutput = async () => decoder.decode(await p.output())

await p.output() // <----- Hangs here
await command("cd /home/dev/www")
await command("ls -la")
console.log(await getOutput())

await p.status()
console.log("done")

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

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

发布评论

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

评论(1

桃酥萝莉 2025-02-09 06:13:44

它之所以悬挂,是因为.output一旦读取了整个过程输出,这意味着ssh命令完成之前,它将无法解决。

还要记住,您需要在每个命令的末尾添加\ n,否则永远不会触发。

await command("cd /home\n");
await command("ls -la\n");
// if you don't finish the ssh session, .output will never resolve
await command("exit\n");
// now it will work correctly
console.log(await getOutput());

无论如何,如果您不想关闭会话以读取给定命令的输出,则需要做的是p.stdout.able.able.ablep.stdout.read.read.read.read.read (BUF)而不是。

for await const(const chunk of p.stdout.readable) {
   // parse chunk and do something
}

It hangs because .output will resolve once the entire process output has been read, meaning that until ssh command has finished, it will not resolve.

Also have in mind that you need to add \n at the end of each command, otherwise it'll never be triggered.

await command("cd /home\n");
await command("ls -la\n");
// if you don't finish the ssh session, .output will never resolve
await command("exit\n");
// now it will work correctly
console.log(await getOutput());

In any case if you don't want to close the session to read the output of a given command, what you need to do is use p.stdout.readable or p.stdout.read(buf) instead.

for await const(const chunk of p.stdout.readable) {
   // parse chunk and do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文