当前位置: 首页 > 工具软件 > jail-shell > 使用案例 >

Java代码执行 Linux-shell命令

訾高明
2023-12-01

package com.bigdata.pushdata;

import java.io.*;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;

public class SshClientUtils {
    private static String IP = "11.211.112.192";
    private static int PORT = 10033;
    private static String USER = "user";         //远程Linux服务器的用户名
    private static String PASSWORD = "1234";  //远程Linux服务器的登录密码

    public static boolean isOK = false;
    public static String putFile(String localFile, String remoteTargetDirectory)
    {
        Connection connection = new Connection(IP, PORT);
        try {
            connection.connect();
            boolean isAuthed = isAuthedWithPassword(USER,PASSWORD,connection);
            if (isAuthed) {
                isOK = false;
                SCPClient scpClient = connection.createSCPClient();
                scpClient.put(localFile, remoteTargetDirectory);
                isOK = true;
                return "===>"+localFile+" \n 文件上传成功!!! \n 服务器目录:"+remoteTargetDirectory;
            } else {
                System.out.println("认证失败!");
                return "ssh认证失败!!!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return "ssh 认证异常!!!";
        } finally {
            connection.close();
        }
    }

    public static boolean isAuthedWithPassword(String user, String password,Connection connection) {
        try {
            return connection.authenticateWithPassword(user, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static Connection cmdConn = null;
    public static Connection getCmdConn(){
        try{
            if(cmdConn != null){
                return cmdConn;
            }else{
                cmdConn = new Connection(IP, PORT);
                cmdConn.connect();
                boolean isAuthed = isAuthedWithPassword(USER,PASSWORD,cmdConn);
                if(isAuthed){
                    return cmdConn;
                }
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /** 关闭连接 */
    public static void close(){
        if (cmdConn != null){
            cmdConn.close();
            cmdConn = null;
        }
    }

    /** 执行Linux 命令 */
    private static StringBuffer sbuffer = new StringBuffer();
    public static String excuteLinuxCmd(String cmd,Connection cmdConn){
        Session sess = null;
        SequenceInputStream msgStream = null;
        String line = null;
        try {
            sbuffer.delete(0,sbuffer.length());
            sess = cmdConn.openSession();
            sess.execCommand(cmd);
            sess.waitForCondition(ChannelCondition.EXIT_STATUS, 1000);
            msgStream = new SequenceInputStream(sess.getStdout(), sess.getStderr());
            BufferedReader buffer = new BufferedReader(new InputStreamReader(msgStream));
            while ((line = buffer.readLine()) != null) {
                //System.out.println(line);
                sbuffer.append(line);
            }
            return sbuffer.toString();
        } catch (Exception var11) {
            var11.printStackTrace();
            return null;
        } finally {
            if (sess != null) {
                sess.close();
            }
        }
    }

    /** windows 根据文件路径获取文件大小 */
    public static double getFileSize(String filepath){
        File file = new File(filepath);
        if (file.exists() && file.isFile()) {
            String fileName = file.getName();
            double filesize = Math.ceil(file.length()/1048576)+1.0;
            System.out.println("文件"+fileName+"的大小是:"+filesize);
            return filesize;
        }
        return 0;
    }

    public static void main(String[] args) {
        try {
            //      E://datatst/xxx.txt
            //      C://Users/admin/Desktop/xxx.jar
            putFile("C://Users/pradmin/Desktop/xxx.jar","/home/user/data/");

            Connection conn = getCmdConn();
            String s = excuteLinuxCmd("ls -hl /home/user/data/xxx.jar | awk '{print $5}'",conn);
            //System.out.println(s);

            getFileSize("C://Users/admin/Desktop/xxx.jar");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

 类似资料: