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的三种方式(附执行命令))