Skip to content

Instantly share code, notes, and snippets.

@sanymakam
Created August 9, 2018 18:01
Show Gist options
  • Save sanymakam/ac089b25c4fded8cfdf12b233abda068 to your computer and use it in GitHub Desktop.
Save sanymakam/ac089b25c4fded8cfdf12b233abda068 to your computer and use it in GitHub Desktop.
public class StagingDeploy {
private static String username = "ec2-user";
private static String hostname = "<pre prod ip>";
private void initSshClient() throws Exception {
JSch jsch = new JSch();
jsch.addIdentity("~/.ssh/id_rsa");
sshSession = jsch.getSession(username, hostname, 22);
Properties config = new Properties();
config.setProperty("StrictHostKeyChecking", "no");
sshSession.setConfig(config);
sshSession.connect(1000);
connectWithStreams();
}
private void connectWithStreams() throws Exception {
sshChannel = (ChannelShell) sshSession.openChannel("shell");
PipedInputStream channelIn = new PipedInputStream();
sshChannel.setInputStream(channelIn);
OutputStream sshClientInput = new PipedOutputStream(channelIn);
inputWriter = new PrintWriter(sshClientInput);
sshClientOutput = new ByteArrayOutputStream(1500);
sshChannel.setOutputStream(sshClientOutput);
sshChannel.connect(1500000);
if (dev == null || dev.isEmpty()) {
sendTextToServer("ssh " + username + "@" + ConfigUtils.getConfigValue("STAGINGQA") + "\n");
log.info("Inside QA BOX!!");
} else {
sendTextToServer("ssh " + username + "@" + ConfigUtils.getConfigValue("STAGINGDEV") + "\n");
log.info("Inside DEV BOX!!");
}
System.out.println(sshClientOutput.toString());
}
private void sendTextToServer(final String text) throws Exception {
inputWriter.write(text);
inputWriter.flush();
log.info("COMMAND RUNNING:" + text);
Thread.sleep(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment