diff --git a/TES_1/pom.xml b/TES_1/pom.xml new file mode 100644 index 0000000..b8d321c --- /dev/null +++ b/TES_1/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + TES + TES_1 + 0.0.1-SNAPSHOT + Sample_1 + This is sample + + + + com.jcraft + jsch + 0.1.53 + + + + \ No newline at end of file diff --git a/TES_1/src/main/java/sample/sample_1.java b/TES_1/src/main/java/sample/sample_1.java new file mode 100644 index 0000000..b29a5a0 --- /dev/null +++ b/TES_1/src/main/java/sample/sample_1.java @@ -0,0 +1,61 @@ +package sample; + +import java.io.InputStream; + +import com.jcraft.jsch.Channel; +import com.jcraft.jsch.ChannelExec; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.Session; + +public class sample_1 { + + public static void main(String[] args) { + + // TODO Auto-generated method stub + + String host="ssh.journaldev.com"; + String user="sshuser"; + String password="sshpwd"; + String command1="ls -ltr"; + try{ + + java.util.Properties config = new java.util.Properties(); + config.put("StrictHostKeyChecking", "no"); + JSch jsch = new JSch(); + Session session=jsch.getSession(user, host, 22); + session.setPassword(password); + session.setConfig(config); + session.connect(); + System.out.println("Connected"); + + Channel channel=session.openChannel("exec"); + ((ChannelExec)channel).setCommand(command1); + channel.setInputStream(null); + ((ChannelExec)channel).setErrStream(System.err); + + InputStream in=channel.getInputStream(); + channel.connect(); + byte[] tmp=new byte[1024]; + while(true){ + while(in.available()>0){ + int i=in.read(tmp, 0, 1024); + if(i<0)break; + System.out.print(new String(tmp, 0, i)); + } + if(channel.isClosed()){ + System.out.println("exit-status: "+channel.getExitStatus()); + break; + } + try{Thread.sleep(1000);}catch(Exception ee){} + } + channel.disconnect(); + session.disconnect(); + System.out.println("DONE"); + }catch(Exception e){ + e.printStackTrace(); + } + + } + } + +