使用deno通过ssh挂在p.Output()上
我希望创建一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它之所以悬挂,是因为
.output
一旦读取了整个过程输出,这意味着ssh
命令完成之前,它将无法解决。还要记住,您需要在每个命令的末尾添加
\ n
,否则永远不会触发。无论如何,如果您不想关闭会话以读取给定命令的输出,则需要做的是
p.stdout.able.able.able
或p.stdout.read.read.read.read.read (BUF)
而不是。It hangs because
.output
will resolve once the entire process output has been read, meaning that untilssh
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.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
orp.stdout.read(buf)
instead.