package com.pasier.xxx.util; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.ethz.ssh2.ChannelCondition; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; public class RmtShellExecutor { private static final Logger LOG = LoggerFactory.getLogger(RmtShellExecutor.class); private Connection conn; private String ip; private String usr; private String psword; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; public RmtShellExecutor(String ip, String usr, String ps) { this.ip = ip; this.usr = usr; this.psword = ps; } private boolean login() throws IOException { conn = new Connection(ip); conn.connect(); return conn.authenticateWithPassword(usr, psword); } public String exec(String cmds) throws IOException { InputStream stdOut = null; InputStream stdErr = null; String outStr = ""; String outErr = ""; int ret = -1; try { if (login()) { Session session = conn.openSession(); session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout()); outStr = processStream(stdOut, charset); LOG.info("caijl:[INFO] outStr=" + outStr); stdErr = new StreamGobbler(session.getStderr()); outErr = processStream(stdErr, charset); LOG.info("caijl:[INFO] outErr=" + outErr); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); ret = session.getExitStatus(); } else { LOG.error("caijl:[INFO] ssh2 login failure:" + ip); throw new IOException("SSH2_ERR"); } } finally { if (conn != null) { conn.close(); } if (stdOut != null) stdOut.close(); if (stdErr != null) stdErr.close(); } return outStr; } private String processStream(InputStream in, String charset) throws IOException { byte[] buf = new byte[1024]; StringBuilder sb = new StringBuilder(); while (in.read(buf) != -1) { sb.append(new String(buf, charset)); } return sb.toString(); } public static void main(String[] args) { String usr = "root"; String password = "12345"; String serverIP = "11.22.33.xx"; String shPath = "/root/ab.sh"; RmtShellExecutor exe = new RmtShellExecutor(serverIP, usr, password); String outInf; try { String[] cmd = {"/bin/sh", "-c", shPath +" "+paramater}; outInf = exe.exec(cmd); System.out.println("outInf= " + outInf); } catch (IOException e) { e.printStackTrace(); } } }
相关推荐
java连接linux,执行shell命令的工具类 sshxcute.jar
sshxcute 就是这样一个框架工具集,它基于 JSCH 构建,允许工程师利用Java 代码通过 SSH 连接远程批量执行 Linux/UNIX 系统上的命令或者脚本,同时加入了判断成功与否,取回输出等多种实用功能。sshxcute 不管是针对...
Java实现远程执行SHELL, JAVA实现远程登陆执行shell脚本。上传文件到远程服务器,下载远程服务器资源。 ShellExecutor exe = new ShellExecutor("192.168.1.1", 22, "USERNAME", "PASSWORD"); if (exe.login()) {...
LINUX SHELL命令练习 题
java-telnet连接远程服务器并执行shell命令 具体代码 java-telnet
本地cmd命令, 输入命令, 并在控制台输出命令执行结果 jsch 远程ssh到linux服务器, 执行结果并显示输出
shell查看返回值,shell查看返回值,shell查看返回值,shell查看返回值,
最牛B的 Linux Shell 命令,最牛B的 Linux Shell 命令,
linux shell命令tr的用法
java远程登录Linux,并执行shell命令。
linux shell命令sed的使用方法
基于sshConnect的linux服务器的shell命令调用,并获取命令返回结果的实例。亲测有效!
linux shell命令 有利于学习研究
linuxshell命令 unixshell命令教程
Linux主要shell命令详解 Linux主要shell命令详解 Linux主要shell命令详解
Linux下Shell命令解释步骤,用C语言实现
linux命令大全介绍了linux shell的一些常用命令
用java语言,sokcet远程连接执行shell
该工具包可实现远程调用Shell脚本,可帮助您轻松实现java远程调用Shell脚本
通过linux的SSH端口使用java代码远程对指定的linux服务器上执行shell命令,实测有用