如何使用 Ganymed SSH API 更改目录 (cd)?

发布于 2024-12-07 12:47:45 字数 494 浏览 1 评论 0原文

我一定在这里遗漏了一些东西,但是我如何使用 Ganymed SSH API 调用类似“cd /root/some/dir/”的东西?

  1. 我创建了一个Connection对象

  2. 在创建的第一个会话中,我调用了“cd /root/some/dir “

  3. 在创建的第二个会话中,我调用了“ls”。或“./myApp”

这不起作用,因为 ganymed 可能会使用自己的目录启动每个会话

所以我需要在同一个会话上执行这两个命令吗?比如:

session.getStdin().write("cd /root/somedir \n".getBytes());
session.getStdin().write("ls . ".getBytes());

这是正确的方法吗?如果是这样,为什么我们需要 Session.execCommand ?

I must be missing something here, but how do I call something like "cd /root/some/dir/" with Ganymed SSH API?

  1. I created a Connection object

  2. In the first session created, I called "cd /root/some/dir"

  3. In the second session created, I called "ls ." or "./myApp"

That didnt work, because ganymed probably starts each session with its own directory

So do I need to perform both commands on the same session? something like:

session.getStdin().write("cd /root/somedir \n".getBytes());
session.getStdin().write("ls . ".getBytes());

Is that the correct way?? if so, why do we need Session.execCommand?

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

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

发布评论

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

评论(3

听不够的曲调 2024-12-14 12:47:45

经过一些研究,我找到的唯一好的解决方案是在与“ls”命令相同的代码中调用“cd”命令,就像这样

session.execCommand("cd /root/somedir ; ls .");

分号将像在任何 bash 代码中一样分隔这两个命令。

这样就可以查询cdls命令的会话结果[session.getExitStatus()],即 >比将两个命令写入session.getStdIn()要好得多(写入标准输入后,您有点失去了检查退出状态的所有能力......)

希望这会有所帮助其余的

埃亚尔

After doing some research, the only good solution I managed to find is calling the "cd" command within the same code as the "ls" command, like this

session.execCommand("cd /root/somedir ; ls .");

The semicolon will separate the two commands as in any bash code.

In this way, you can query the session's result [session.getExitStatus()] of both the cd and ls commands, which is much better then writing the two commands to session.getStdIn() (after writing to stdin, you kinda loose all the ability to check for exit status...)

Hope this will help the rest

Eyal

与风相奔跑 2024-12-14 12:47:45

根据 Ganymed 常见问题解答 (http://www.ganymed.ethz.ch/ssh2/ FAQ.html),您不能为您生成的每个 Session 对象发送多个命令。这显然是 SSH-2 希望您处理它的方式。您的两个选择是结合两个命令,例如

session.execCommand("cd /root/somedir ; ls .");

但是这并不总是有效,并且如果您有多个命令,它会变得非常难看。另一种方法是打开交互式 shell 会话并将命令写入标准输入。这可能如下所示:

Session sess = conn.openSession();
sess.requestDumbPTY();
sess.startShell();

OutputStream os = sess.getStdin();
os.write("cd /root/somedir\n".getBytes());
os.write("ls -1\n".getBytes());
os.write("exit\n".getBytes());

InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

//TODO

请注意最终 exit 命令的使用。由于这被视为终端窗口,因此如果您不退出程序,则您读取服务器输出的任何循环将永远不会终止,因为服务器将等待更多输入

According to the Ganymed FAQ (http://www.ganymed.ethz.ch/ssh2/FAQ.html), you are not allowed to send more than one command per Session object you generate. This is how SSH-2 apparently wants you to handle it. Your two options are to either combine the two commands like

session.execCommand("cd /root/somedir ; ls .");

However this wont always work and it get very ugly if you have more than a couple commands. The other way to do this is to open an interactive shell session and write the commands to standard in. This could look something like this:

Session sess = conn.openSession();
sess.requestDumbPTY();
sess.startShell();

OutputStream os = sess.getStdin();
os.write("cd /root/somedir\n".getBytes());
os.write("ls -1\n".getBytes());
os.write("exit\n".getBytes());

InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

//TODO

Note the use of the final exit command. Since this is being treated like a terminal window, if you do not exit from the program any loop you have reading the output of the server will never terminate because the server will be expecting more input

慕烟庭风 2024-12-14 12:47:45

好的,我快速浏览了一下 Ganymed javadoc,虽然我自己没有尝试过,但我认为您应该使用会话的方法 execCommand() 而不是写入 STDIN。我非常确定会话已连接到远程 shell,因此可以处理 shell 状态,包括当前目录、环境变量等。

因此,只需执行以下操作:

session.execCommand("cd /root/somedir \n".getBytes());
session.execCommand("ls . ".getBytes());

我希望这对您有用。祝你好运。

OK, I took a quick look on the Ganymed javadoc and although I did not try it myself I assume that you should use method execCommand() of session instead of writing into the STDIN. I am pretty sure that session is connected to remote shell and therefore handles the shell state including current directory, environment variables etc.

So, just do the following:

session.execCommand("cd /root/somedir \n".getBytes());
session.execCommand("ls . ".getBytes());

I hope this will work for you. Good luck.

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