我正在尝试将文件从一个远程服务器复制到另一个远程服务器。我试过用scp。它通过putty而不是通过代码复制文件。我目前正在使用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
日志看起来像用户主页设置不正确!所以它找不到关键文件!
一般的做法:
我很确定一旦你解决了这个问题,你就会得到其他一切。当涉及代理时,这可能变得非常复杂,因为代理设置必须在所有3台机器之间正常工作。我反复面对这些问题,而且可能非常混乱。