Created
June 21, 2016 12:35
-
-
Save Mahesha999/b579ef87b2c3d770e7a37523e425ce2e to your computer and use it in GitHub Desktop.
Weird eclipse behavior http://stackoverflow.com/questions/37942845/output-is-not-printed-in-console-in-eclipse-while-running-but-is-printed-in-debu
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.jcraft.jsch.Channel; | |
import com.jcraft.jsch.ChannelExec; | |
import com.jcraft.jsch.ChannelSftp; | |
import com.jcraft.jsch.JSch; | |
import com.jcraft.jsch.Session; | |
public class SSHUtility { | |
private static final Logger LOGGER = LoggerFactory.getLogger(SSHUtility.class); | |
public static void main(String[] args) { | |
executeScript("digitate", "digitate", "127.0.0.1", 22, "/opt/helloworld.sh"); | |
println "done" | |
} | |
public static void executeScript(String pUser, String pPassword, String pServer, int pPort, String pCommand) { | |
System.out.println("Executing on ssh"); | |
try { | |
JSch lJSCH = new JSch(); | |
Session lSession = lJSCH.getSession(pUser, pServer, pPort); | |
lSession.setConfig("StrictHostKeyChecking", "no"); | |
lSession.setPassword(pPassword); | |
lSession.connect(); | |
ChannelExec lChannelExec = (ChannelExec)lSession.openChannel("exec"); | |
lChannelExec.setCommand(pCommand); | |
lChannelExec.setInputStream(null); | |
InputStream stdout = lChannelExec.getInputStream(); | |
InputStream stderr = lChannelExec.getErrStream(); | |
lChannelExec.connect(); | |
waitForChannelClosed(lChannelExec); | |
int lExitStatus = lChannelExec.getExitStatus(); | |
BufferedReader brStdOut = new BufferedReader(new InputStreamReader(stdout)); | |
BufferedReader brStdErr = new BufferedReader(new InputStreamReader(stderr)); | |
int brValue = 0; | |
System.out.println("before1"); | |
while((brValue=brStdOut.read()) != -1) | |
{ | |
char brChar = (char)brValue; | |
System.out.print(brChar); | |
System.out.println("inside1"); | |
} | |
System.out.println("after1"); | |
System.out.println("before2"); | |
while((brValue=brStdErr.read()) != -1) | |
{ | |
System.out.println("inside2"); | |
char brChar = (char)brValue; | |
System.out.print(brChar); | |
} | |
System.out.println("after2"); | |
if(lExitStatus < 0) { | |
LOGGER.info("Done, but exit status not set!"); | |
} | |
else if(lExitStatus > 0) { | |
LOGGER.info("Done, but with error, exitStatus : " + lExitStatus); | |
} | |
else { | |
LOGGER.info("Script successfully executed : " + pCommand); | |
} | |
lChannelExec.disconnect(); | |
lSession.disconnect(); | |
} | |
catch(Exception e) { | |
LOGGER.info("Error executing script : " + pCommand); | |
} | |
} | |
private static void waitForChannelClosed(ChannelExec pChannel) throws IOException | |
{ | |
InputStream lStdOut = pChannel.getInputStream(); | |
byte[] buffer = null; | |
while(true) | |
{ | |
while(lStdOut.available() > 0) | |
{ | |
int i = lStdOut.read(buffer, 0, 1024); | |
if (i < 0) { break; } | |
System.out.print(new String(buffer, 0, i));//print response to console | |
} | |
if (pChannel.isClosed()) {//It is never closed | |
System.out.println("exit-status: " + pChannel.getExitStatus()); | |
break; | |
} | |
try{Thread.sleep(1000);}catch(Exception ee){} | |
} | |
} | |
} | |
/* OUTPUT IN DEBUG MODE | |
Executing on ssh | |
exit-status: 0 | |
before1 | |
Hinside1 | |
einside1 | |
linside1 | |
linside1 | |
oinside1 | |
inside1 | |
Winside1 | |
oinside1 | |
rinside1 | |
linside1 | |
dinside1 | |
inside1 | |
after1 | |
before2 | |
after2 | |
done | |
*/ | |
/* OUTPUT IN RUN MODE (RUN MODE DOES NOT TERMINATE/ENDS EXECUTION) | |
Executing on ssh | |
done | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment