Java 通过ssh连接lunix,执行语句
来源:互联网 发布:音频编辑软件中文版 编辑:程序博客网 时间:2024/06/08 19:06
这里总结两种方法。
第一种方法:Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import ch.ethz.ssh2.Connection;import ch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;public class SshCommandExcute { /** * 运行命令 * * @param command * 命令 * @return */ public String run(String command) { String hostname = "127.0.0.1"; int port = 22; String username = "root"; String password = "root"; // 指明连接主机的IP地址 Connection conn = new Connection(hostname,port); Session ssh = null; String result = ""; try { // 连接到主机 conn.connect(); // 使用用户名和密码校验 boolean isconn = conn.authenticateWithPassword(username, password); if (!isconn) { System.out.println("用户名称或者是密码不正确"); } else { System.out.println("已经连接OK"); ssh = conn.openSession(); ssh.execCommand(command); InputStream is = new StreamGobbler(ssh.getStdout()); BufferedReader brs = new BufferedReader(new InputStreamReader(is, "UTF-8")); while (true) { // System.out.println(brs.readLine()); String line = brs.readLine(); if (line == null) { break; } result += line + "\r\n"; } } // 连接的Session和Connection对象都需要关闭 ssh.close(); conn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public static void main(String[] arg) { String command = "ls;hadoop fs -ls /"; String line = new SshCommandExcute().run(command); System.out.println(line); }}
第二种方法:JSch是ssh2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。官网:http://www.jcraft.com/jsch/
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.charset.Charset;import java.util.Properties;import com.jcraft.jsch.ChannelExec;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import com.opensymphony.xwork2.Result;public class SSHConnect { /** * @param args */ public static void main(String[] args) { } public static String SSHConnectAndExecute(String cmd) { Session session=null; ChannelExec channelExec=null; BufferedReader reader=null; String result=""; try { JSch jsch = new JSch(); // 创建JSch对象 String hostname = "127.0.0.1"; int port = 22; String username = "root"; String password = "root"; session = jsch.getSession(username, hostname, port); // 根据用户名,主机ip,端口获取一个Session对象 session.setPassword(password); // 设置密码 Properties config = new Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // 为Session对象设置properties int timeout = 600000000; session.setTimeout(timeout); // 设置timeout时间 session.connect(); // 通过Session建立链接 channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(cmd); channelExec.setInputStream(null); channelExec.setErrStream(System.err); channelExec.connect(); InputStream in = channelExec.getInputStream(); reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8"))); String buf = null; StringBuffer sb = new StringBuffer(); while ((buf = reader.readLine()) != null) { sb.append(buf); result+=buf+"\r\n"; System.out.println(buf);// 打印控制台输出 } } catch (Exception e) { // TODO: handle exception }finally{ if (null != session) { session.disconnect(); } try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } channelExec.disconnect(); } return result; }}
阅读全文
1 0
- Java 通过ssh连接lunix,执行语句
- java通过ssh连接服务器执行shell命令
- java通过ssh连接服务器执行shell命令
- Maven java通过jdbc连接Hive 执行HQL语句
- 纯Java通过SSH执行Linux命令
- Java通过SSH连接Linux服务器
- Linux下通过EXPECT脚本自动执行ssh远程连接
- Linux下通过EXPECT脚本自动执行ssh远程连接
- java连接SQL数据库,执行SQL语句
- Java通过Telnet连接执行shell脚本
- 通过jdbc连接数据库,批量执行多条SQL语句
- Lunix 基本语句
- java连接SSH服务器并执行shell命令
- JAVA实现远程SSH连接linux并执行命令
- java使用ssh连接Linux并执行命令
- java 间断执行多条命令ssh连接,有缺陷
- JAVA使用JSch进行SSH连接Linux并执行命令
- Java实现ssh连接linux并执行shell命令
- 把输入数据类型封装成一个类
- 多对多关系映射
- 221. Maximal Square
- H5data-
- linux下 mysql数据库的备份和还原
- Java 通过ssh连接lunix,执行语句
- Markdown语法
- 使用二级缓存
- Valid Anagram
- 比较难理解的部分:Thread-fork/Data/singal(八)
- 中欧MiniMBA-建设高效团队
- 派生类向基类的自动类型转换
- 本固枝荣--前端体验大会12月23日深圳场
- CodeForces 60 E.Mushroom Gnomes(矩阵快速幂)