如何使用 Ganymed SSH API 更改目录 (cd)?
我一定在这里遗漏了一些东西,但是我如何使用 Ganymed SSH API 调用类似“cd /root/some/dir/”的东西?
我创建了一个
Connection
对象在创建的第一个会话中,我调用了“cd /root/some/dir “
在创建的第二个会话中,我调用了“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?
I created a
Connection
objectIn the first session created, I called "cd /root/some/dir"
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过一些研究,我找到的唯一好的解决方案是在与“ls”命令相同的代码中调用“cd”命令,就像这样
分号将像在任何 bash 代码中一样分隔这两个命令。
这样就可以查询
cd
和ls
命令的会话结果[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
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 thecd
andls
commands, which is much better then writing the two commands tosession.getStdIn()
(after writing to stdin, you kinda loose all the ability to check for exit status...)Hope this will help the rest
Eyal
根据 Ganymed 常见问题解答 (http://www.ganymed.ethz.ch/ssh2/ FAQ.html),您不能为您生成的每个 Session 对象发送多个命令。这显然是 SSH-2 希望您处理它的方式。您的两个选择是结合两个命令,例如
但是这并不总是有效,并且如果您有多个命令,它会变得非常难看。另一种方法是打开交互式 shell 会话并将命令写入标准输入。这可能如下所示:
请注意最终 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
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:
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
好的,我快速浏览了一下 Ganymed javadoc,虽然我自己没有尝试过,但我认为您应该使用会话的方法
execCommand()
而不是写入 STDIN。我非常确定会话已连接到远程 shell,因此可以处理 shell 状态,包括当前目录、环境变量等。因此,只需执行以下操作:
我希望这对您有用。祝你好运。
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:
I hope this will work for you. Good luck.