0

I had to move a file from one location to another and run script named "Irel_Wrapper"

in putty I did /home/location mv filename.

So in Java i used channel(Exec) and I was able to execute the above scenario ie moving the file as "mv" command is a putty command.

But I am not able to run the script Irel_Wrapper (my guess is as its not a putty native command i am not able to do that in java with channel(Exec).

I basically need to open a location with cd command and run the irel_wrapper script. I tried a method , but i was not able to achieve it. Error that i got : Ksh Irel not found.

My code:

public ArrayList<String> deployIrelWrapper() throws JSchException, IOException {
    ConnectAndCreateSession();
    String GrepComandConsole = null;
    StringBuilder sb = new StringBuilder();
    Channel channel1 = session.openChannel("exec");
    String command = "cd /pre/d02/pinDap75a/opt/ifw/vf/cdr/p3/out/irel && irel_wrapper";
    BufferedReader br = null;
    java.io.InputStream in = channel1.getInputStream();

    ((ChannelExec) channel1).setCommand(command);
    ((ChannelExec) channel1).setErrStream(System.err);
    System.out.println("Connect to Channel...");
    channel1.connect();
    System.out.println("****Channel Connected****");
    System.out.println();
    String line;
    try {
        br = new BufferedReader(new InputStreamReader(in));
        while ((line = br.readLine()) != null) {
            sb.append(line + " ");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    ArrayList<String> ResultSet = new ArrayList<>();
    ArrayList<String> ResultSetOutput = new ArrayList<>();
    System.out.println("Expected output");
    String words = sb.toString();
    String[] result = words.split(" ");
    for (String ss : result) {
        ResultSet.add(ss);
    }
    for (int i = 0; i < ResultSet.size(); i++) {
        GrepComandConsole = ResultSet.toArray()[i].toString();
        ResultSetOutput.add(ResultSet.toArray()[i].toString());
        System.out.println(ResultSetOutput);
    }
    channel1.disconnect();
    session.disconnect();

    return ResultSetOutput;
}

Kindly do not make this question duplicate. I am able to execute commands like mv command cd.. or SFTP transfer.. but to execute a script after opening a path with CD command I am not able to get it. I am using Jsch lib.

How its done manually:
I login into putty.
Open the path: cd home/apt/cdr/irel/
run irel_wrapper : irel_wrapper
then list of few other details will get populated like, GSM3A , Voic, NET3B etc..
Next final command: irel_wrapper GSM3A

this is how we do it manually via putty, I tried automating it using java and Jsch. SO SFTP and other simple commands like mv and ls I was able to achieve.. except this Irel wrapper.. but when I do it manually its working fine. Thus No spelling mistake I guess..

Cœur
  • 37,241
  • 25
  • 195
  • 267
T Kanagaraj
  • 245
  • 5
  • 20
  • `Ksh Irel not found` means that the shell which is executing `irel_wrapper` on the remote system can't find this "Irel" program. You may have misspelled the name of the command, or it may not actually exist on the system, or it may not be in your command path. Could you edit your question to include the text of this irel_wrapper script, and tell us where this "Irel" program is actually stored on the remote system? – Kenster Dec 25 '14 at 18:39
  • It worked properly ! but I am not able to iterate between the outputs.. I mean i was the output of cd command alone to be printed on the console.. but the code which you refered prints out everything right from login in a way how putty displays its ouput.. can u help me with a model code to display commander's output alone ?? – Ragesh Kr Dec 26 '14 at 09:17

1 Answers1

0

The problem is that ssh connections do not start a shell by default. In order to execute a script in a shell you'll need to have a terminal started.

Channel channel = session.openChannel("shell");

You're starting it as "exec" which just lets you run programs, but not shell scripts. More information about the differences between them are discussed here:

What is the difference between the 'shell' channel and the 'exec' channel in JSch

Sorry you searched so long for that. Happy Holidays.

Community
  • 1
  • 1
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • This isn't correct. The exec channel causes the server to run `$SHELL -c 'command-requested-by-client'`. "command-requested-by-client" could be any valid shell command, such as the `cd /path && run-something` form which the OP is requesting. – Kenster Dec 25 '14 at 18:29
  • 1
    It worked properly ! but I am not able to iterate between the outputs.. I mean i was the output of cd command alone to be printed on the console.. but the code which you refered prints out everything right from login in a way how putty displays its ouput.. can u help me with a model code to display commander's output alone ?? – T Kanagaraj Dec 26 '14 at 05:46