-
-
Save sanymakam/ac089b25c4fded8cfdf12b233abda068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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