Not able to run putty script via JAVA JSch

2019-09-07 03:01发布

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..

标签: java jsch
1条回答
迷人小祖宗
2楼-- · 2019-09-07 03:43

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.

查看更多
登录 后发表回答