ssh - Keypair login to EC2 instance with JSch -


i want able use jsch java ssh library connect ec2 instance. how use .pem keypair aws jsch? how deal unknownhostkey error when attempting connect?

the groovy code use jsch library connect ec2 instance, run whoami , hostname commands, print results console:

@grab(group='com.jcraft', module='jsch', version='0.1.49')  import com.jcraft.jsch.*  jsch jsch=new jsch(); jsch.addidentity("/your path pem/gateway.pem"); jsch.setconfig("stricthostkeychecking", "no");  //enter own ec2 instance ip here session session=jsch.getsession("ec2-user", "54.xxx.xxx.xxx", 22); session.connect();  //run stuff string command = "whoami;hostname"; channel channel = session.openchannel("exec"); channel.setcommand(command); channel.seterrstream(system.err); channel.connect();  inputstream input = channel.getinputstream(); //start reading input executed commands on shell byte[] tmp = new byte[1024]; while (true) {     while (input.available() > 0) {         int = input.read(tmp, 0, 1024);         if (i < 0) break;         print(new string(tmp, 0, i));     }     if (channel.isclosed()){         println("exit-status: " + channel.getexitstatus());         break;     }     sleep(1000); }  channel.disconnect(); session.disconnect(); 

here's example of how make same connection, through gateway ssh tunnel (nat bastion): https://gist.github.com/scoroberts/5605655


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -