JAVA開発メモ

 

[リロード]   [新規|編集|差分|添付]   [トップ|一覧|単語検索|最終更新|バックアップ|ヘルプ]

サイトメニュー

import java.io.IOException; import org.apache.commons.io.IOUtils; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; public class SshConnection { private final static String HOSTNAME = "hostname"; private final static String USERNAME = "userid"; private final static String PASSWORD = "pass"; private final static String ENCODE = "UTF-8"; public SshConnection() { } /** * */ public String execCommand(String command) throws AuthenticationException, ExitStatusException, IOException { String out = ""; Connection connection = null; Session session = null; try { connection = new Connection(HOSTNAME); connection.connect(); boolean isAuthenticated = connection.authenticateWithPassword(USERNAME, PASSWORD); if (!isAuthenticated) { throw new AuthenticationException(); } session = connection.openSession(); session.execCommand(command); out = IOUtils.toString(new StreamGobbler(session.getStdout()), ENCODE); if (session.getExitStatus() != 0) { throw new ExitStatusException("ExitCode: " + session.getExitStatus()); } System.out.println("ExitCode: " + session.getExitStatus() + "\n" + out); } finally { if (session != null) { session.close(); } if (connection != null) { connection.close(); } } return out; } } import java.io.IOException; /** * */ public class AuthenticationException extends IOException { public AuthenticationException() { super("Authentication failed."); } } import java.io.IOException; public class ExitStatusException extends IOException { public ExitStatusException() { super(); } /** * */ public ExitStatusException(String msg) { super(msg); } }

リロード   新規 編集 差分   トップ 一覧 検索 最終更新 バックアップ   ヘルプ   最終更新のRSS
Last-modified: Wed, 12 Jan 2011 00:55:07 UTC (4235d)

Modified by MT22(Moriwaki Takashi)

"PukiWiki" 1.3.7 Copyright © 2001,2002,2003 PukiWiki Developers Team. License is GNU/GPL.
Based on "PukiWiki" 1.3 by sng
Powered by PHP 7.4.30

HTML convert time to 0.015 sec.