0

I have written a java code that can connects using Jsch, and can Upload and download files between the local machine and server.

But I am not sure how to execute commands like 'mv' to move files between folders withing the server using java. To list the files ls command will be used in putty application, is possible with Jsch or any other library has to be used ?

JSch jsch = null;
Session session = null;
Channel channel = null;
ChannelSftp c = null;
try {
    jsch = new JSch();
    System.out.println("Setting Up SFTP Connection...");
    session = jsch.getSession(username, host, 22);
    session.setPassword(pass);
    System.out.println("SFTP Configration Complete..!");  
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    System.out.println("Attempting to Connect..!"); 
    session.setConfig("PreferredAuthentications","publickey,keyboard-interactive,password");
    session.connect();
    System.out.println("Session Connected."); 
    channel = session.openChannel("sftp");
    channel.connect();
    System.out.println("Channel Connected."); 
    c = (ChannelSftp) channel;
    System.out.println("Connection Established\n");
} catch (Exception e) { 
    e.printStackTrace();    
}

try {
    String tempFolderInLocal = "C:/Users/591705/Desktop/Test";
    String Destiantion = "/hta1/home/pinDap75a/DestinationDemo";
    String ServerTempDestination="/hta1/home/pinDap75a/TempDestinationDemo";
    String DestiantionT = "/hta1/home/pinDap75a/DestinationDemo/Demo.txt";
    String Source = "C:/Users/591705/Documents/Demo.txt";  
    System.out.println("Starting Upload..");
    c.put(Source,Destiantion);
    System.out.println("**Upload Finished**");  
    System.out.println("Starting Downlaod...");
    c.get(DestiantionT, tempFolderInLocal);

    System.out.println("File Transfer Complete! \n");
} 
catch (Exception e) {   e.printStackTrace();    }

c.disconnect();
session.disconnect();

To move the file between ServerDestinationTemp and DestinationT I am able to achieve it

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46

2 Answers2

-1

Use a Shell channel

shchannel = session.openChannel("shell");
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    But how do i execute a command ? – Ragesh Kr Dec 15 '14 at 09:26
  • Instead of shchannel = session.openChannel("sftp"); I have given it as shchannel = session.openChannel("shell"); now.. – Ragesh Kr Dec 15 '14 at 09:30
  • I am getting this error while running the code as session.openChannel("shell"); **java.lang.ClassCastException: com.jcraft.jsch.ChannelShell cannot be cast to com.jcraft.jsch.ChannelSftp** – Ragesh Kr Dec 15 '14 at 09:34
  • @RageshKr Well, why do you cast it in the first place? The "shell" channel cannot by used as "sftp" channel. If you need both, you need to open two channels (you can have multiple channels over one SSH connection). – Martin Prikryl Dec 15 '14 at 10:23
  • I basically need to transfer files and execute commands like MV , ls, etc.. Transfering files part i have done and is working fine. But regarding excuting commands I am not able to :( can u help me with a simple example code ? – Ragesh Kr Dec 15 '14 at 10:29
  • For File transfer we ll need SFTP, to execude commands we ll have to use EXEC.. If I have to do both and what of I have multiple commands to execute ?? – Ragesh Kr Dec 15 '14 at 13:30
-2

Yes you can use below code to execute shell command using java without interacting with shell terminal.

 Runtime runtime = Runtime.getRuntime();
    Process proc = null;
    String retStr = "";
    InputStreamReader insReader = null;
    char[] tmpBuffer = new char[1024];
    int nRet = 0;
    String cmd = "mv abc.txt /home/abct.txt";
    try {
        proc = runtime.exec(cmd);
        insReader = new InputStreamReader(proc.getInputStream());

        while ((nRet = insReader.read(tmpBuffer, 0, 1024)) != -1) {
            retStr += new String(tmpBuffer, 0, nRet);
        }

        insReader.close();
        System.out.println(retStr);
    } catch (Exception e) {
        System.out.println("Bad");
    } finally {
        System.out.println("Done");
        }
Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24