【发布时间】:2020-06-21 19:08:54
【问题描述】:
我只是在学习 Java 和 Jsch,我可以让它运行其他命令,但不能运行 cd。 SSHManager的sendCommand函数返回的错误码不是null,而是一些每次都不一样的不可读字符串(可能是null,对Java的内部工作不太熟悉)。
知道为什么不?类似的问题在这里JSch - Why doesn't CD work?,但没有回答。
我不会在此处复制和粘贴整个 SSHManager 类 - 此处包含我正在尝试遵循的完整代码的有用答案。 Run a command over SSH with JSch
示例代码如下:
import SSH.SSHManager;
public class src
{
int ERROR = 0;
public static void main(String[] args)
{
String username = "debian";
String password = "temppwd";
String ipadd = "192.168.7.2";
SSHManager ssh = new SSHManager(username, password, ipadd, "");
ssh.connect();
String out = "";
//this doesn't work, printing output as bytes to show how weird it is
out = ssh.sendCommand("cd Desktop");
System.out.println(out.getBytes());
//some other test commands
out = ssh.sendCommand("mkdir test");
System.out.println(out);
out = ssh.sendCommand("ls");
System.out.println(out);
ssh.sendCommand("logout");
}
}
Eclipse 控制台的输出(bin 和 Desktop 已经在根目录中):
[B@b065c63
bin
Desktop
test
【问题讨论】: