0

I have got one requirement where i have to access a unix server and on that server i have to run a shell script with some parameter from my java application. Please suggest some solution with example.

i have tried something but it is not working.

SshWrapper ssh = new SshWrapper();
 try {  
        ssh.connect("10.206.19.80", 22);  
        ssh.login("*****","*****");  

        ssh.setPrompt("c898vqz:~");  
        ssh.waitfor("#");
        System.out.println("PWD**********"+ssh.send("pwd"));  

        ssh.disconnect();
        System.out.println(ssh.getClass());
    } catch (java.io.IOException e) {  
        e.printStackTrace();  
    }

getting null from ssh.send("pwd")

4
  • possible duplicate of Executing a shell script with an empty argument from Java Commented Aug 23, 2013 at 11:06
  • Does anybody have any solution for it instead of down voting this question. If you required any other information i am ready provide. Commented Aug 23, 2013 at 11:09
  • @AniketThakur: please refer to my updates. Commented Aug 23, 2013 at 11:14
  • @sanbhat does not look like a duplicate . The code in link run on the same server where command needs to be run while this question is asking how to connect to a different unix server . Commented Dec 13, 2020 at 6:54

2 Answers 2

2

You could use SSH component JCraft for remote connection and shell commands invocations:

import com.jcraft.jsch.*

Exerp from my old code:

JSch jsch = new JSch();

String command = "/tmp/myscript.sh";
Session session = jsch.getSession(user, host, 22);
session.connect();

Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
channel.connect();

byte[] tmp = new byte[1024];
while (true) {
  while (in.available() > 0) {
      int i = in.read(tmp, 0, 1024);
      if (i < 0) {
          break;
      }
      System.out.print(new String(tmp, 0, i));
  }
  if (channel.isClosed()) {
      if (channel.getExitStatus() == 0) {
          System.out.println("Command executed successully.");
      }
      break;
  }
}
channel.disconnect();
session.disconnect();

You can also easily transfer files via session.openChannel("sftp").

ooph.. in java it is too wordy, than e.g. in ruby or python :)

Sign up to request clarification or add additional context in comments.

1 Comment

I an trying to run FNDLOAD utility (already installed on server) using this code. But It gives me command not found error. Can you help in running FNDLOAD command.
1
JSch js = new JSch();
Session s = js.getSession("username", "ip", port);
s.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
System.out.println("connection ");
Channel c = s.openChannel("sftp");
ChannelSftp ce = (ChannelSftp) c;

ce.connect();
System.out.println("connection ");
ce.disconnect();
s.disconnect(); 

1 Comment

please, do not just post the code without specify the code i details in able to clarify your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.