我需要执行以下命令:
scp -r ~/dataIn yatsuk@192.168.1.1:~/dataOut
在 ubuntu (16.04) 终端中,此命令可以正常工作。yatsuk@192.168.1.1 是本地主机。
所以我使用 jcabi 尝试这段代码:
Shell shell = new SSHByPassword("192.168.1.1", 22, "yatsuk", "passw");
String stdout = new Shell.Plain(shell).exec("scp -r ~/dataIn yatsuk@192.168.1.1:~/dataOut");
System.out.println(stdout);
JSch的这段代码:
JSch jsch = new JSch();
JSch.setConfig("StrictHostKeyChecking", "no");
Session session = jsch.getSession("yatsuk", 192.168.1.1, 22);
session.setPassword("passw");
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("scp -r ~/dataIn yatsuk@192.168.1.1:~/dataOut");
((ChannelExec) channel).setErrStream(System.err);
channel.setOutputStream(System.out);
channel.setInputStream(System.in);
channel.connect();
while (!channel.isClosed()) {
Thread.sleep(1000);
}
channel.disconnect();
session.disconnect();
两者都返回我:
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
lost connection
像“echo 1 > 1.txt”这样的简单命令可以完美运行。也许我不明白的东西?