简体   繁体   中英

Copy a file in ubuntu to windows using putty - ssh from windows

I Have to copy a .csv file in my ubuntu machine into my windows machine by standing in windows itself. That is I have to do the copying process by running putty or anything like that from windows machine. I need it as a command because I have to do it with Java.

Call PSCP program from Java:

String[] command = {
    "pscp",
    "-q", // quiet, don't show statistics
    "-pw", // passw login with specified password
    "yourP4ssw0rd", // the user password
    "username@yourhost:file.csv", 
    "c:\\the\\destination\\of\\your\\file.csv"
};

// command == pscp -q -pw yourP4ssw0rd username@yourhost:file.csv c:\\the\\destination\\of\\your\\file.csv
Process p = Runtime.getRuntime().exec(command);
p.waitFor();

BufferedReader reader = 
     new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";           
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}

...

Reference

Take a look at JSch . It provides a Java API to do what you want.

try that:

Process p = Runtime.getRuntime().exec("putty -ssh -2 -P 22 USERNAME@SERVER_ADDR -pw PASS -m command.txt");
p.waitFor();

BufferedReader reader = 
     new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";           
while ((line = reader.readLine())!= null) {
sb.append(line + "\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM