I am trying to transfer a file from local to remote with scp
. Local is windows(OpenSSH is running as service) and remote is FreeBSD(rsa key are setup).
For this i am using Jsch library.
try {
if (ses == null) ses = fsaTo.getSession();
channel = ses.openChannel("exec");
String cmd = "scp "+ UserHostIdentity.getTransferUser(fsaFrom.getSystem()) + "@"
+ UserHostIdentity.getTransferHost(fsaFrom.getSystem()) + ":"
+ from + " "
+ to;
((ChannelExec)channel).setCommand(cmd);
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The command cmd
I tested it manually executing it and it worked. This is how I start up the session:
JSch jsch = new JSch();
try {
sshSession = jsch.getSession(UserHostIdentity.getTransferUser(fs), UserHostIdentity.getTransferHost(fs),22);
UserInfo lui = UserHostIdentity.getTransferUserInfo(fs);
sshSession.setUserInfo(lui);
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
} catch (JSchException e) {
logger.error("Could not access fileserver.");
throw new RuntimeException(e);
}
The issue is after calling channel.connect()
nothing happens, no error, nothing.
In the same context the following code executes "sha256 -q " + filePath
and returns correct results:
public String doCommand(String cmd) {
if (sshSession == null) {
initiateConnection();
}
Channel channel;
String result = "";
try {
channel = sshSession.openChannel("exec");
((ChannelExec)channel).setCommand(cmd);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream input = channel.getInputStream();
channel.connect();
InputStreamReader inputReader = new InputStreamReader(input);
BufferedReader bufferedReader = new BufferedReader(inputReader);
String line = null;
while((line = bufferedReader.readLine()) != null){
result = line;
}
bufferedReader.close();
inputReader.close();
channel.disconnect();
} catch (JSchException e) {
logger.error("Problem with communication to Fileserver.");
throw new RuntimeException(e);
} catch (IOException e) {
logger.error("Problem with command stream.");
throw new RuntimeException(e);
}
return result;
}
My question is, why doesnt it work with the scp
command.