栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

java·使用exec操作Linux

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java·使用exec操作Linux

java·使用exec操作Linux
    • 使用exec操作Linux命令
    • 操作windows下cmd命令

使用exec操作Linux命令

注:exec不支持cd、sed
使用这些操作先将他们写在shell文本中,然后执行shell

package com.rxld.rxld_kettle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SSHHelper {
    
    public static String exec(String host,String user,String psw,int port,String command){
        StringBuffer sb= new StringBuffer();
        Session session =null;
        ChannelExec openChannel =null;
        try {
            JSch jsch=new JSch();
            session = jsch.getSession(user, host, port);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");//跳过公钥的询问
            session.setConfig(config);
            session.setPassword(psw);
            session.connect(5000);//设置连接的超时时间
            openChannel = (ChannelExec) session.openChannel("exec");
            openChannel.setCommand(command); //执行命令
            int exitStatus = openChannel.getExitStatus(); //退出状态为-1,直到通道关闭
            System.out.println(exitStatus);

            // 下面是得到输出的内容
            openChannel.connect();
            InputStream in = openChannel.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String buf = null;
            while ((buf = reader.readLine()) != null) {
                sb.append(buf+"t");
            }
        } catch (JSchException | IOException e) {
            sb.append(e.getMessage()+"n");
        }finally{
            if(openChannel!=null&&!openChannel.isClosed()){
                openChannel.disconnect();
            }
            if(session!=null&&session.isConnected()){
                session.disconnect();
            }
        }
        return sb.toString();
    }
    
    public static void main(String args[]){
        String com = "echo "#!/bin/bashnecho "Hello World !"ncd /devnls" >dd.sh " ;
        System.out.println(com);
        String exec = exec("192.168.100.100", "root", "123", 22,com );
        System.out.println(exec);
    }
}
操作windows下cmd命令
package com.rxld.rxld_kettle;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class execCmd {
    @Test
    public void testCmd() throws Exception {
        String cmd = "/bin/sh -c date"; //命令的前面必须要有/bin/sh -c
        execCmd(cmd);
    }
    public static void execCmd(String cmd) {
        try {
            Runtime rt = Runtime.getRuntime();
            //执行命令, 最后一个参数,可以使用new File("path")指定运行的命令的位置
            Process proc = rt.exec(cmd, null, null);
            InputStream stderr = proc.getInputStream();
            InputStreamReader isr = new InputStreamReader(stderr, "GBK");
            BufferedReader br = new BufferedReader(isr);
            String line = "";
            while ((line = br.readLine()) != null) { // 打印出命令执行的结果
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963919.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号