首页 > 代码库 > SshTerminal

SshTerminal

package com.network.ssh;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.net.InetSocketAddress;import org.apache.sshd.ClientChannel;import org.apache.sshd.ClientSession;import org.apache.sshd.SshClient;import org.apache.sshd.client.future.AuthFuture;import org.apache.sshd.common.util.NoCloseInputStream;import org.apache.sshd.common.util.NoCloseOutputStream;public class SshTerminal {    public static final int DEFAULT_SSH_PORT = 22;    public static final String ENTER = "\r";    public static final String EXIT = "exit";    private SshClient client;    private ClientSession session;    private ClientChannel channel;    private ByteArrayInputStream input;    private ByteArrayOutputStream output;    private StringBuffer remoteInfo = new StringBuffer();    private boolean isLogin = false;    public SshTerminal() {        client = SshClient.setUpDefaultClient();        client.start();    }    public boolean login(String ip,String userName,String password) throws InterruptedException, IOException{        return login(ip, DEFAULT_SSH_PORT, userName, password);    }        public boolean login(String ip,int port,String userName,String password) throws InterruptedException, IOException{        closeSession();        session = client.connect(userName, new InetSocketAddress(ip, port)).await().getSession();        session.addPasswordIdentity(password);        AuthFuture future = session.auth().await();        isLogin = future.isSuccess();        return isLogin;    }        public void runCMD(String...scripts) throws Exception{        if(!isLogin) throw new Exception("not login!");        StringBuffer buffer = new StringBuffer();        for(String cmd : scripts){            buffer.append(cmd.trim()).append(ENTER);        }        /**         * createShellChannel() create a blocking channel ,use exit release         */        buffer.append(EXIT).append(ENTER);        closeChannel();        input = new ByteArrayInputStream(buffer.toString().getBytes());        channel = session.createShellChannel();        channel.setIn(new NoCloseInputStream(input));        open();    }        public void runCMD(String cmd) throws Exception{        if(!isLogin) throw new Exception("not login!");        closeChannel();        channel = session.createExecChannel(cmd);        open();    }    private void open() throws IOException{        //session will close,so need to new        output = new ByteArrayOutputStream();        channel.setOut(new NoCloseOutputStream(output));        channel.setErr(new NoCloseOutputStream(output));        channel.open();        channel.waitFor(ClientChannel.CLOSED, 0);    }        private void closeStream(){        try{            if(input != null){                input.close();                input = null;            }            if(output != null){                remoteInfo.append(output.toString());                output.close();                output = null;            }        }catch(IOException e){            e.printStackTrace();        }    }        private void closeChannel(){        closeStream();        if(channel != null){            channel.close(true);            channel = null;        }        }        private void closeSession(){        closeChannel();        if (session != null) {            session.close(true);            session = null;        }    }        public void destory() {        closeSession();        client.stop();        client = null;    }    public String getRemoteInfo() throws Exception{        //the end od if the output not close        if(output != null) remoteInfo.append(output.toString());        return remoteInfo.toString();    }}

 

SshTerminal