1

I am trying to use this code for running unix command from java:

        // Initialize a ConnBean object, parameter list is ip, username, password
        ConnBean cb = new ConnBean("servername", "username","");

        // Put the ConnBean instance as parameter for SSHExec static method getInstance(ConnBean) to retrieve a singleton SSHExec instance
        SSHExec ssh = SSHExec.getInstance(cb);

        // Connect to server
        Boolean isConnected = ssh.connect();

            String[] commands = new String[]{"sudo echo 'Hello command!.' > ChenTest.txt"};
            CustomTask sampleTask = new ExecCommand(commands);

            try {
                Result result = ssh.exec(sampleTask);
            } catch (TaskExecFailException e) {
                System.out.println(e);
            }
            catch (Exception e) {
                System.out.println(e);
            }

When i debug my app i get "Auth fail".

I know that my server and username are correct, but not sure why can`t i connect. I have a key on this unix server ..do i need to add it somewhere in my code to make the connection succeed?

8
  • I dont see port no in code. Which port you are connecting to? Commented Jun 17, 2014 at 11:30
  • ssh is 22 port so Im dont think is necessary to prove it. Commented Jun 17, 2014 at 11:43
  • can you connect to that server by ssh tertminal? Commented Jun 17, 2014 at 11:45
  • I can connect to this server using putty or winSCP. Commented Jun 17, 2014 at 11:48
  • What i wanted to know if there is maybe a way to tell the java code i have a key..maybe even add the file path there somehow. Commented Jun 17, 2014 at 13:16

1 Answer 1

0

It looks like you are connecting with a public key instead of using a password.

To do that I think you need to configure SSHExec to know where your key file is located.

I'm not a java guy but I think you need something like this:

SSHExec.setOption("keyfile", "/home/user/.ssh/id_dsa.pub");

Your key file will be different.

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

1 Comment

It could be a good idea, but there is a list of IOptionName.<SomeConstOption> and it does not contain the key file path. It is very strange that there is no documentation for such a thing i have to say.

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.