【发布时间】:2019-08-25 06:54:25
【问题描述】:
我正在尝试将文件从一台远程服务器复制到另一台远程服务器。我尝试使用 scp。它是通过腻子复制文件,而不是通过代码。我目前正在使用 echo 来复制文件。使用 echo 我正在将字符串 finalStr 写入 abc.bcc ,它工作正常。但是在 Jsch 中使用下面的命令时它不起作用。
scp /home/abc.bcc user@host:/folder1/folder2/abc.bcc
我尝试添加 ssh 公钥,但没有成功。我尝试使用channel.setPty(true) 来避免密码提示并通过bufferedWriter 设置密码。但仍然无法复制文件。请提出需要更改的内容。
JSch jsch = new JSch();
Session session;
try {
session = jsch.getSession("user", "host", 22);
session.setPassword("password");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
logger.info("Connection status: " + session.isConnected());
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(
"echo \"" + finalStr + "\" >> /folder1/folder2/abc.bcc");
((ChannelExec) channel).setPty(false);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
channel.connect();
logger.info("Channel status : " + channel.isConnected());
out.write("\n".getBytes());
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
while ((line = br.readLine()) != null) {
logger.info(++index + " : " + line);
}
channel.disconnect();
session.disconnect();
}catch(....
调试输出:
debug1: Trying private key: /home/build_path/.ssh/id1
debug3: no such identity: /home/build_path/.ssh/id1: No such file or directory
debug1: Trying private key: /home/build_path/.ssh/id2
debug3: no such identity: /home/build_path/.ssh/id2: No such file or directory
debug1: Trying private key: /home/build_path/.ssh/id3
debug3: no such identity: /home/build_path/.ssh/id_3: No such file or directory
【问题讨论】:
-
你能添加一些日志或控制台输出吗?您是否还在日志等中看到任何错误。
-
将
-vvv切换到scp用于两种情况,并向我们展示输出。 -
1) “对于这两种情况”,请 2) 这几乎不是一个完整的输出。 3)删除您不需要的评论。谢谢。