SharpSSH 与持久 ShellExec 连接

发布于 2024-09-07 11:17:13 字数 419 浏览 3 评论 0原文

我正在使用 SharpSSH 连接到 SSH 服务器,并且尝试使用 SshShell 和 SshExec。 我需要能够接受一系列命令并按顺序将它们发送到服务器,因此 SshShell 并没有真正满足我的需要,因为我必须一直处理流,而且似乎有点麻烦一个拼凑品。所以我尝试过 SshExec 但发现它有一个问题,每次我发送命令时,它似乎都会建立一个新连接并丢失上一个命令的上下文。例如,如果我运行以下命令:

pwd
cd .ssh
pwd

我希望它输出

/home/adam

/home/adam/.ssh

但是,它只是两次输出“/home/adam”,这意味着目录更改在中间丢失了。

有没有一种方法可以配置它,以便它保持与 SSH 服务器的持续连接,直到我告诉它断开连接?

I'm using SharpSSH to connect to an SSH server and I've tried using both SshShell and SshExec.
I need to be able to take a series of commands and send them to the server in order, so SshShell doesn't really do what I need since I would have to wrangle streams the whole time and it seems that it would be a bit of a kludge. So I've tried SshExec but found one problem with it, every time I send a command it seems to be making a new connection and losing the context of the last command. For example if I ran the following commands:

pwd
cd .ssh
pwd

I would expect it to output

/home/adam

/home/adam/.ssh

But, instead it just ouputs "/home/adam" both times, meaning that the directory change was lost in between.

Is there a way I can configure this so that it maintains a constant connection to the SSH server until I tell it to disconnect?

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

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

发布评论

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

评论(2

夕嗳→ 2024-09-14 11:17:13

这样做:

exec.RunCommand("pwd; cd Desktop; pwd")

我不知道如何执行高级命令,但我尝试过,它输出:

/Users/MyUser
/Users/MyUser/Desktop

Do this:

exec.RunCommand("pwd; cd Desktop; pwd")

I am not sure how to do advanced commands, but I tried that and it outputs:

/Users/MyUser
/Users/MyUser/Desktop
洋洋洒洒 2024-09-14 11:17:13

要 cd 到隐藏目录(任何以点 (.) 字符开头的目录),您需要将该值括在引号中。

根据文档

4) 如果目录操作数的第一个组成部分是点或点-点,则继续步骤 6。

6) 将 curpath 设置为由 PWD 的值、斜杠字符和操作数串联而成的字符串。

简而言之,cd '.ssh' 应该可以解决问题。

To cd to a hidden directory (any directory beginning with a dot (.) character), you need to enclose the value in quotes.

According to the documentation:

4) If the first component of the directory operand is dot or dot-dot, proceed to step 6.

6) Set curpath to the string formed by the concatenation of the value of PWD , a slash character, and the operand.

In short, cd '.ssh' should do the trick.

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