0

I have created REST API to create EC2 instance using AWS JAVA SDK provided. Now I am trying to connect to created EC2 instance and then need to install software's in the instance again through java. I didn't find any appropriate article for this. Is there any possible way to do this? I don't want to use SSH client like putty.. Thanks..

Nithyananth
  • 329
  • 2
  • 5
  • 17

2 Answers2

0

Sounds like you're looking for a java ssh client. You should set up key authentication and use the ssh client library from java to execute the installation for you.

See this post: for a list of solutions

ApriOri
  • 2,618
  • 29
  • 48
0
public static void connectToEC2(){

    try{
        JSch jsch=new JSch();

        String user = "User-name";
        String host = "host";
        int port = 22;
        File directory = new File(".");
        String privateKey = directory.getCanonicalPath() + File.separator + "pem file path";

        jsch.addIdentity(privateKey);
        System.out.println("identity added ");

        Session session = jsch.getSession(user, host, port);
        System.out.println("session created.");
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect();

        Channel channel=session.openChannel("shell");
        channel.setInputStream(System.in);
        channel.setOutputStream(System.out);
        channel.connect(3*1000);
    }
    catch(Exception e){
        System.out.println(e);
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35