Java教程

java连接linux

本文主要是介绍java连接linux,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
 

本地调用

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).append("\n");
    }
    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).append("\n");
    }
    return sb.toString();
}

  

 前端页面展示换行需添加样式

.content{

white-space: pre-wrap;

}

这篇关于java连接linux的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!