0

I am trying to execute a set of commands over an ssh connection established using a private key. For this I used, JSCh and Session class in java.

Here is the code:

java.util.Properties config = new java.util.Properties();

         String privateKeyPath = conn.getsshArg().getKeyPath();

        config.put("StrictHostKeyChecking", "no");
        JSch jsch = new JSch();

        jsch.addIdentity(privateKeyPath); //

        Session session = jsch.getSession(conn.getsshArg().getUsername(), conn.getVmIpaddress(), 22);
        session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password"); //

        System.out.println("User:" + conn.getsshArg().getUsername() + "Password" + conn.getsshArg().getPassword()
                + "IP:" + conn.getVmIpaddress() + " Port:" + 22);
        session.setPassword(conn.getsshArg().getPassword());
        System.out.println("Command to be fired:" + conn.getsshArg().getCommand());
        session.setConfig(config);
        //session.setTimeout(150);
        session.connect();
        if (session.isConnected()) {
            // LogManager.getLogger().info("VNFLCM: Ssh on the VNF
            // successfull");
            System.out.println("SSH successful");

        } else {
            System.out.println("SSH Connection Failure ");
            return ret;
        }

        Channel channel = session.openChannel("exec");
        String command = conn.getsshArg().getCommand().replace("&curlStart;", "{");
        command = command.replaceAll("&curlEnd;", "}");
        command = command.replaceAll("&jsonQuot;", "\"");
        ((ChannelExec) channel).setCommand(command);
        //channel.setInputStream(null);
        ((ChannelExec) channel).setErrStream(System.err);

        InputStream in = channel.getInputStream();
        channel.connect();

        byte[] tmp = new byte[1024];
        String line = "";

        while(true){
            while(in.available()>0){
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                //LogManager.getLogger().debug(new String(tmp, 0, i));
                line=new String(tmp, 0, i);
                System.out.print(line);

            }
            if(channel.isClosed()){
                //LogManager.getLogger().debug("exit-status: "+channel.getExitStatus());
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
            }
            ret = true;
            try{Thread.sleep(5000);}catch(Exception ee){}
        }

        channel.disconnect();
        session.disconnect();
    }

    catch (Exception e) {
        e.printStackTrace();
    }

I tried logging in using root user over my node, but a prompt for ssh using fedora instead of root was made.

I tried the solution provided in the link : Java JSch changing user on remote machine and execute command , but using su -c "command" prompts for a password. And here I don't have any password for my instance.

All the commands need to be executed using root user only.

Please note: Here, I am trying to ssh using a private key.

I would be grateful if anyone could help me in this.

5
  • Use sudo, Luke. Commented Jul 18, 2017 at 13:34
  • First you have to tell us, how, if at all, are you able to execute those commands manually. What do you do to achieve that? Commented Jul 21, 2017 at 6:36
  • @MartinPrikryl The commands I execute manually are boldsudo echo {\"insecure-registries\":[\"172.16.109.55:5000\"]} >> /etc/docker/daemon.json;sudo systemctl daemon-reload;sudo systemctl restart docker;**bold** which work fine when executed from cli. But when I try to execute it from my code it gives error /etc/docker/daemon.json permission denied. Commented Jul 26, 2017 at 7:02
  • I do not see any sudo in your code! Commented Jul 26, 2017 at 7:44
  • I tried using sudo but my code gets stuck in the while loop. Commented Jul 26, 2017 at 7:56

0

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.