首先加入依赖

        <dependency>
			<groupId>com.jcraft</groupId>
			<artifactId>jsch</artifactId>
			<version>0.1.55</version>
		</dependency>

windows系统需要安装FreeSSHd

 



import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;
import java.util.Vector;

/**
 * 执行shell命令
 *
 * @author: yg
 * @date: 2020-10-11
 */
@Slf4j
public class ExecuteShellUtil {

	private Vector<String> stdout;

	Session session;

	public ExecuteShellUtil(final String ipAddress, final String username, final String password,int port) {
		try {
			JSch jsch = new JSch();
			session = jsch.getSession(username, ipAddress, port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect(3000);
		} catch (Exception e) {
		log.error(e.getMessage(), e);
		}

	}

	public int execute4linux(final String command) {
		int returnCode = 0;
		ChannelShell channel = null;
		PrintWriter printWriter = null;
		BufferedReader input = null;
		stdout = new Vector<String>();
		try {
			channel = (ChannelShell) session.openChannel("shell");
			channel.connect();
			input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
			printWriter = new PrintWriter(channel.getOutputStream());
			printWriter.println(command);
			printWriter.println("exit");
			printWriter.flush();
			System.out.println("The remote command is: ");
			String line;
			while ((line = input.readLine()) != null) {
				stdout.add(line);
			//	System.out.println(line);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return -1;
		}finally {
			IoUtil.close(printWriter);
			IoUtil.close(input);
			if (channel != null) {
				channel.disconnect();
			}
			close();
		}
		return returnCode;
	}

	public void close(){
		if (session != null) {
			session.disconnect();
		}
	}

	public String execute4LinuxResult(String command) {
		execute4linux(command);
		StringBuilder sb = new StringBuilder();
		for (String str : stdout) {
			sb.append(str);
		}
		return sb.toString();
	}
	
	public int execute4Window(final String command){

    	int returnCode = 0;
        BufferedReader reader = null;
        stdout = new Vector<String>();
        Channel channel = null;
        try {
            if (command != null) {
                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(command);
                channel.connect();
                InputStream in = channel.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in, "GBK"));
                String buf;
                while ((buf = reader.readLine()) != null) {
                	stdout.add(buf);
                }
                return returnCode;
            }
        } catch (Exception e) {
    		log.error(e.getMessage(), e);
            returnCode= -1;
        } finally {       
            if (channel != null) {
                channel.disconnect();
            }
        	close();
        }
        return returnCode;
    
	}
	public String execute4WindowsResult(String command) {
		execute4Window(command);
		StringBuilder sb = new StringBuilder();
		for (String str : stdout) {
			sb.append(str);
		}
		return sb.toString();
	}
	
  public static void main(String[] args) {
	// ExecuteShellUtil  executeShellUtil= new ExecuteShellUtil("192.168.50.44", "root", "****", 22);
	  ExecuteShellUtil  executeShellUtil= new ExecuteShellUtil("192.168.50.189", "root", "123456", 22);
	  
	  System.out.println( executeShellUtil.execute4Window("java -version"));
	
 }
}

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐