java使用shell脚本.docVIP

  • 15
  • 0
  • 约 15页
  • 2016-04-13 发布于浙江
  • 举报
java使用shell脚本

package com.corp.prj.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.log4j.Logger; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; /** * Provides static methods for running SSH, scp as well as local commands. * */ public class CommandRunner { private static final Logger logger = Logger.getLogger(CommandRunner.class); private CommandRunner() { } /** * Get remote file through scp * @param host * @param username * @param password * @param remoteFile * @param localDir * @throws IOException */ public static void scpGet(String host, String username, String password, String remoteFile, String localDir) throws IOException { if (logger.isDebugEnabled()) { logger.debug(spc [ + remoteFile + ] from + host + to + localDir); } Connection conn = getOpenedConnection(host, username, password); SCPClient client = new SCPClient(conn); client.get(remoteFile, localDir); conn.close(); } /** * Put local file to remote machine. * @param host * @param username * @param password * @param localFile * @param remoteDir * @throws IOException */ public static void scpPut(String host, String username, String password, String localFile, String remoteDir) throws IOException { if (logger.isDebugEnabled()) { logger.debug(spc [ + localFile + ] to + host + remoteDir); } Connection conn = getOpenedConnection(host, username, password); SCPClient client = new SCPClient(conn); client.put(localFile, remoteDir); conn.close(); } /** * Run SSH command. * @param host * @param username * @param password * @param cmd * @return exit status * @throws IOException */ public static int runSSH(String host, String username, String password, String cmd) throws IOException { if (logger.isDebugEnabled()) { logger.debug(running SSH cmd [ + cmd + ]); } Connection conn = getOpenedConnection(host, username, password); Session ses

文档评论(0)

1亿VIP精品文档

相关文档