How to pass a file from a remote host to another remote host in java?
I have been working on an application that uses JSCH to scp and transfer a file to another directory in the same server. However, I want to be able to transfer a file from one remote host to another remote host in an automated way. Below is the code I use to make the call. I have tried using other scp commands such as: scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2
. It works when testing it on the server itself but when I try to incorporate it into the code below, it passes back an error.
CODE:
public static void main(String args){
String array = {"robert.txt","ops1axv@gaalpltclu00029.linux.us.ams1907.com:/tmp"};
if(array.length!=2){
System.err.println("usage: java ScpTo file1 user@remotehost:file2");
System.exit(-1);
}
FileInputStream fis=null;
try{
String lfile=array[0];
String user=array[1].substring(0, array[1].indexOf('@'));
array[1]=array[1].substring(array[1].indexOf('@')+1);
String host=array[1].substring(0, array[1].indexOf(':'));
String rfile=array[1].substring(array[1].indexOf(':')+1);
System.out.println("RFile: " + rfile );
System.out.println("Lfile: " + lfile);
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File _lfile = new File(lfile);
if(ptimestamp){
command="T"+(_lfile.lastModified()/1000)+" 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command+=(" "+(_lfile.lastModified()/1000)+" 0n");
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize=_lfile.length();
command="C0644 "+filesize+" ";
if(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}
else{
command+=lfile;
}
command+="n";
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
// send a content of lfile
fis=new FileInputStream(lfile);
byte buf=new byte[1024];
while(true){
int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
fis=null;
// send ''
buf[0]=0; out.write(buf, 0, 1); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
out.close();
channel.disconnect();
session.disconnect();
System.exit(0);
}
catch(Exception e){
System.out.println(e);
try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}
java linux unix
|
show 3 more comments
I have been working on an application that uses JSCH to scp and transfer a file to another directory in the same server. However, I want to be able to transfer a file from one remote host to another remote host in an automated way. Below is the code I use to make the call. I have tried using other scp commands such as: scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2
. It works when testing it on the server itself but when I try to incorporate it into the code below, it passes back an error.
CODE:
public static void main(String args){
String array = {"robert.txt","ops1axv@gaalpltclu00029.linux.us.ams1907.com:/tmp"};
if(array.length!=2){
System.err.println("usage: java ScpTo file1 user@remotehost:file2");
System.exit(-1);
}
FileInputStream fis=null;
try{
String lfile=array[0];
String user=array[1].substring(0, array[1].indexOf('@'));
array[1]=array[1].substring(array[1].indexOf('@')+1);
String host=array[1].substring(0, array[1].indexOf(':'));
String rfile=array[1].substring(array[1].indexOf(':')+1);
System.out.println("RFile: " + rfile );
System.out.println("Lfile: " + lfile);
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File _lfile = new File(lfile);
if(ptimestamp){
command="T"+(_lfile.lastModified()/1000)+" 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command+=(" "+(_lfile.lastModified()/1000)+" 0n");
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize=_lfile.length();
command="C0644 "+filesize+" ";
if(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}
else{
command+=lfile;
}
command+="n";
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
// send a content of lfile
fis=new FileInputStream(lfile);
byte buf=new byte[1024];
while(true){
int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
fis=null;
// send ''
buf[0]=0; out.write(buf, 0, 1); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
out.close();
channel.disconnect();
session.disconnect();
System.exit(0);
}
catch(Exception e){
System.out.println(e);
try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}
java linux unix
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44
|
show 3 more comments
I have been working on an application that uses JSCH to scp and transfer a file to another directory in the same server. However, I want to be able to transfer a file from one remote host to another remote host in an automated way. Below is the code I use to make the call. I have tried using other scp commands such as: scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2
. It works when testing it on the server itself but when I try to incorporate it into the code below, it passes back an error.
CODE:
public static void main(String args){
String array = {"robert.txt","ops1axv@gaalpltclu00029.linux.us.ams1907.com:/tmp"};
if(array.length!=2){
System.err.println("usage: java ScpTo file1 user@remotehost:file2");
System.exit(-1);
}
FileInputStream fis=null;
try{
String lfile=array[0];
String user=array[1].substring(0, array[1].indexOf('@'));
array[1]=array[1].substring(array[1].indexOf('@')+1);
String host=array[1].substring(0, array[1].indexOf(':'));
String rfile=array[1].substring(array[1].indexOf(':')+1);
System.out.println("RFile: " + rfile );
System.out.println("Lfile: " + lfile);
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File _lfile = new File(lfile);
if(ptimestamp){
command="T"+(_lfile.lastModified()/1000)+" 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command+=(" "+(_lfile.lastModified()/1000)+" 0n");
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize=_lfile.length();
command="C0644 "+filesize+" ";
if(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}
else{
command+=lfile;
}
command+="n";
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
// send a content of lfile
fis=new FileInputStream(lfile);
byte buf=new byte[1024];
while(true){
int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
fis=null;
// send ''
buf[0]=0; out.write(buf, 0, 1); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
out.close();
channel.disconnect();
session.disconnect();
System.exit(0);
}
catch(Exception e){
System.out.println(e);
try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}
java linux unix
I have been working on an application that uses JSCH to scp and transfer a file to another directory in the same server. However, I want to be able to transfer a file from one remote host to another remote host in an automated way. Below is the code I use to make the call. I have tried using other scp commands such as: scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2
. It works when testing it on the server itself but when I try to incorporate it into the code below, it passes back an error.
CODE:
public static void main(String args){
String array = {"robert.txt","ops1axv@gaalpltclu00029.linux.us.ams1907.com:/tmp"};
if(array.length!=2){
System.err.println("usage: java ScpTo file1 user@remotehost:file2");
System.exit(-1);
}
FileInputStream fis=null;
try{
String lfile=array[0];
String user=array[1].substring(0, array[1].indexOf('@'));
array[1]=array[1].substring(array[1].indexOf('@')+1);
String host=array[1].substring(0, array[1].indexOf(':'));
String rfile=array[1].substring(array[1].indexOf(':')+1);
System.out.println("RFile: " + rfile );
System.out.println("Lfile: " + lfile);
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File _lfile = new File(lfile);
if(ptimestamp){
command="T"+(_lfile.lastModified()/1000)+" 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command+=(" "+(_lfile.lastModified()/1000)+" 0n");
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
}
// send "C0644 filesize filename", where filename should not include '/'
long filesize=_lfile.length();
command="C0644 "+filesize+" ";
if(lfile.lastIndexOf('/')>0){
command+=lfile.substring(lfile.lastIndexOf('/')+1);
}
else{
command+=lfile;
}
command+="n";
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
// send a content of lfile
fis=new FileInputStream(lfile);
byte buf=new byte[1024];
while(true){
int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
out.write(buf, 0, len); //out.flush();
}
fis.close();
fis=null;
// send ''
buf[0]=0; out.write(buf, 0, 1); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
out.close();
channel.disconnect();
session.disconnect();
System.exit(0);
}
catch(Exception e){
System.out.println(e);
try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}
java linux unix
java linux unix
asked Nov 20 '18 at 16:19
tharriotttharriott
205
205
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44
|
show 3 more comments
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397233%2fhow-to-pass-a-file-from-a-remote-host-to-another-remote-host-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397233%2fhow-to-pass-a-file-from-a-remote-host-to-another-remote-host-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the error?
– Nicholas K
Nov 20 '18 at 16:29
@NicholasK Im sorry let me rephrase, there is no error. Though in the beginning of my code, when I try to enter " scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2" into the array. It will not compile, and I just dont know it which way to edit my script in order to get it to work.
– tharriott
Nov 20 '18 at 16:33
Any reason you are using an array?
– Nicholas K
Nov 20 '18 at 16:35
@NicholasK I found it easier to break apart and identify each part. Also, when I tried using a string variable and setting the command to the variable.I didn't get the desired result. It just failed to compile.
– tharriott
Nov 20 '18 at 16:41
Your code is compiling fine. Not really sure what the issue is, if you could do elaborate further.
– Nicholas K
Nov 20 '18 at 16:44