java连接linux的三种方式(附执行命令)
Posted 维尼先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java连接linux的三种方式(附执行命令)相关的知识,希望对你有一定的参考价值。
# 本地调用
使用JDK自带的RunTime类和Process类实现
public static void main(String[] args){ Process proc = RunTime.getRunTime().exec("cd /home/winnie; ls;") // 标准输入流(必须写在 waitFor 之前) String inStr = consumeInputStream(proc.getInputStream()); // 标准错误流(必须写在 waitFor 之前) String errStr = consumeInputStream(proc.getErrorStream()); int retCode = proc.waitFor(); if(retCode == 0){ System.out.println("程序正常执行结束"); } } /** * 消费inputstream,并返回 */ public static String consumeInputStream(InputStream is){ BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s ; StringBuilder sb = new StringBuilder(); while((s=br.readLine())!=null){ System.out.println(s); sb.append(s); } return sb.toString(); }
# 远程调用(一)
导入ganymed-ssh2包
import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; public static void main(String[] args){ String host = "210.38.162.181"; int port = 22; String username = "root"; String password = "root"; // 创建连接 Connection conn = new Connection(host, port); // 启动连接 conn.connection(); // 验证用户密码 conn.authenticateWithPassword(username, password); Session session = conn.openSession(); session.execCommand("cd /home/winnie; ls;"); // 消费所有输入流 String inStr = consumeInputStream(session.getStdout()); String errStr = consumeInputStream(session.getStderr()); session.close; conn.close(); } /** * 消费inputstream,并返回 */ public static String consumeInputStream(InputStream is){ BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s ; StringBuilder sb = new StringBuilder(); while((s=br.readLine())!=null){ System.out.println(s); sb.append(s); } return sb.toString(); }
# 远程调用(二)
使用jsch实现
import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public static void main(String[] args){ String host = "210.38.162.181"; int port = 22; String username = "root"; String password = "root"; // 创建JSch JSch jSch = new JSch(); // 获取session Session session = jSch.getSession(username, host, port); session.setPassword(password); Properties prop = new Properties(); prop.put("StrictHostKeyChecking", "no"); session.setProperties(prop); // 启动连接 session.connect(); ChannelExec exec = (ChannelExec)session.openChannel("exec"); exec.setCommand("cd /home/winnie; ls;"); exec.setInputStream(null); exec.setErrStream(System.err); exec.connect(); // 消费所有输入流,必须在exec之后 String inStr = consumeInputStream(exec.getInputStream()); String errStr = consumeInputStream(exec.getErrStream()); exec.disconnect(); session.disconnect(); } /** * 消费inputstream,并返回 */ public static String consumeInputStream(InputStream is){ BufferedReader br = new BufferedReader(new InputStreamReader(is)); String s ; StringBuilder sb = new StringBuilder(); while((s=br.readLine())!=null){ System.out.println(s); sb.append(s); } return sb.toString(); }
以上是关于java连接linux的三种方式(附执行命令)的主要内容,如果未能解决你的问题,请参考以下文章
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载
Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载