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();
}

 

转载于:https://www.cnblogs.com/winniejohn/p/10275277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值