사용자 도구

사이트 도구


java:ssh

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판 양쪽 다음 판
java:ssh [2015/08/18 16:27]
kwon37xi
java:ssh [2015/08/18 16:30]
kwon37xi [가장 기본 exec 채널]
줄 6: 줄 6:
   * [[http://www.journaldev.com/246/java-program-to-run-shell-commands-on-ssh-enabled-system|Java Program to run shell commands on SSH enabled System]]   * [[http://www.journaldev.com/246/java-program-to-run-shell-commands-on-ssh-enabled-system|Java Program to run shell commands on SSH enabled System]]
  
-===== 가장 기본 exec 채널 ===== 
-Private Key를 통해 접속하여 명령을 실행하고 그 결과를 현재 출력/에러 스트림으로 보내는 가장 간단한 예제. 
- 
-<code java> 
-JSch jsch = new JSch(); 
- 
-String host = "[hostname]"; 
-String username = "[username]"; 
-jsch.addIdentity("/path/to/.ssh/id_rsa"); 
- 
-Session session = jsch.getSession(username, host, 22); 
- 
-Properties sessionConfig = new Properties(); 
-sessionConfig.put("StrictHostKeyChecking", "no"); 
-session.setConfig(sessionConfig); 
-session.connect(); 
- 
-ChannelExec channel = (ChannelExec) session.openChannel("exec"); 
-channel.setCommand("/bin/ls /etc"); 
- 
-// setInputStream을 호출하면 InputStream을 닫아주는 작업도 올바로 해야함. 
-channel.setInputStream(null); 
- 
-// 출력 내용을 System 스트림으로 보내게 함. 
-channel.setOutputStream(System.out); 
-channel.setErrStream(System.err); 
- 
-// 출력 스트림을 호출하면 출력 스트림을 읽고 닫아주는 작업도 해야함. 
-// 여기서는 System.out과 System.err이 처리하게 둠. 
-// channel.getInputStream(); // <- 일반 출력 스트림 
-// channel.getErrStream(); // <- 일반 에러 스트림 
- 
-channel.connect(); 
- 
-while(true) { 
-    if (channel.isClosed()) { 
-        break; 
-    } 
- 
-    TimeUnit.MILLISECONDS.sleep(100); 
-} 
- 
-channel.disconnect(); 
-session.disconnect(); 
-</code> 
  
 ===== Channels ===== ===== Channels =====
java/ssh.txt · 마지막으로 수정됨: 2015/08/20 15:04 저자 kwon37xi