java中如何使用jsch远程连接linux服务器并执行命令

前提是需要导入jsch-0.1.54.jar文件

package xxx.xxx.xxx.xxx;


import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;


public class SSHUtils
{
    private static final String ENCODING = "UTF-8";

    public static Session getJSchSession(DestHost destHost)
        throws JSchException
    {
        JSch jsch = new JSch();

        Session session = jsch.getSession(destHost.getUsername(), destHost.getHost(),
            destHost.getPort());
        session.setPassword(destHost.getPassword());
        session.setConfig("StrictHostKeyChecking", "no"); // 第一次访问服务器时不用输入yes
        session.setTimeout(destHost.getTimeout());
        session.connect();

        return session;
    }

    public static String execCommandByJSch(DestHost destHost, String command)
        throws IOException, JSchException
    {
        return execCommandByJSch(destHost, command, ENCODING);
    }

    public static String execCommandByJSch(DestHost destHost, String command,
                                           String resultEncoding)
        throws IOException, JSchException
    {
        Session session = getJSchSession(destHost);
        String result = execCommandByJSch(session, command, resultEncoding);
        session.disconnect();

        return result;
    }

    public static String execCommandByJSch(Session session, String command)
        throws IOException, JSchException
    {
        return execCommandByJSch(session, command, ENCODING);
    }

    public static String execCommandByJSch(Session session, String command, String resultEncoding)
        throws IOException, JSchException
    {
        ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
        InputStream in = channelExec.getInputStream();
        channelExec.setCommand(command);
        channelExec.setErrStream(System.err);
        channelExec.connect();

        String result = IOUtils.toString(in, resultEncoding);

        channelExec.disconnect();

        return result;
    }

    /**
     * 目标登录主机信息
     */
    public static class DestHost
    {
        private String host = "";

        private String username = "";

        private String password = "";

        private int port = 22;

        private int timeout = 60 * 60 * 1000;

        public DestHost(String host, String username, String password)
        {
            this(host, username, password, 22, 60 * 60 * 1000);
        }

        public DestHost(String host, String username, String password, int timeout)
        {
            this(host, username, password, 22, timeout);
        }

        public DestHost(String host, String username, String password, int port, int timeout)
        {
            this.host = host;
            this.username = username;
            this.password = password;
            this.port = port;
            this.timeout = timeout;
        }

        public String getHost()
        {
            return host;
        }

        public void setHost(String host)
        {
            this.host = host;
        }

        public String getUsername()
        {
            return username;
        }

        public void setUsername(String username)
        {
            this.username = username;
        }

        public String getPassword()
        {
            return password;
        }

        public void setPassword(String password)
        {
            this.password = password;
        }

        public int getPort()
        {
            return port;
        }

        public void setPort(int port)
        {
            this.port = port;
        }

        public int getTimeout()
        {
            return timeout;
        }

        public void setTimeout(int timeout)
        {
            this.timeout = timeout;
        }

    }

    public static void main(String[] args)
    {
        try
        {
            SSHUtils.DestHost host = new SSHUtils.DestHost("192.168.1.108", "root", "6411");

            String stdout = "";
            Session shellSession = SSHUtils.getJSchSession(host);
            stdout = SSHUtils.execCommandByJSch(shellSession, "cd ~");
            stdout = SSHUtils.execCommandByJSch(shellSession, "mkdir testtesttest");
            stdout = SSHUtils.execCommandByJSch(shellSession, "whoami");
            shellSession.disconnect();

            // System.out.println(stdout);
            // response.getWriter().println(stdout);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值