一、前言
由于前些阵子想找个ssh客户端 for android实现在手机上可以远程控制Centos服务器实现在手机上远程关机操作,找了一圈没有合适的,因此自己想通过linux的客户端接入包jsch实现绑定登录服务器,再进行关机操作的在线服务
二、代码示例
1. 命令接口类Command - 实现初始化账号、shell命令的执行及关闭session的简单操作
public abstract interface Command{@b@ @b@ public abstract void init(String paramString1, String paramString2, String paramString3, int paramInt1, int paramInt2);@b@@b@ public abstract boolean exec(String paramString, StringBuffer paramStringBuffer);@b@@b@ public abstract void close();@b@@b@}
2. 接口公共抽象类DefaultCommand - 实现默认及常规接口实现逻辑
import com.jcraft.jsch.Channel;@b@import com.jcraft.jsch.ChannelExec;@b@import com.jcraft.jsch.JSch;@b@import com.jcraft.jsch.JSchException;@b@import com.jcraft.jsch.Session;@b@import com.jcraft.jsch.UserInfo;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import org.apache.log4j.Logger;@b@@b@public abstract class DefaultCommand{@b@ @b@ protected int port;@b@ private int connectionTimeOut;@b@ protected UserInfo userinfo;@b@ private JSch jsch;@b@ private Session session;@b@ private static final Logger log = Logger.getLogger(DefaultCommand.class);@b@@b@ public DefaultCommand()@b@ {@b@ this.port = 22;@b@ this.connectionTimeOut = 30000;@b@@b@ this.jsch = new JSch();@b@ }@b@@b@ public abstract void init(String paramString1, String paramString2, String paramString3, int paramInt1, int paramInt2);@b@@b@ protected void genrateSession(String host, String userName, int port, int connectionTimeOut, UserInfo userinfo) {@b@ try@b@ {@b@ this.session = this.jsch.getSession(userName, host, (port <= 0) ? this.port : port);@b@ this.session.setUserInfo(userinfo);@b@ if (connectionTimeOut <= 0) connect();@b@ else connect(connectionTimeOut);@b@ } catch (JSchException e) {@b@ log.error("get session error:" + e.getMessage());@b@ }@b@ }@b@@b@ private void connect() {@b@ connect(this.connectionTimeOut);@b@ }@b@@b@ private void connect(int connectionTimeOut) {@b@ if (this.session != null)@b@ try {@b@ this.session.connect(connectionTimeOut);@b@ } catch (JSchException e) {@b@ log.error("connection error:" + e.getMessage());@b@ }@b@ }@b@@b@ public boolean exec(String command, StringBuffer res){@b@ Channel channel = null;@b@ try {@b@ channel = this.session.openChannel("exec");@b@ ((ChannelExec)channel).setCommand(command);@b@ channel.setInputStream(null);@b@ ((ChannelExec)channel).setErrStream(System.err);@b@ InputStream is = channel.getInputStream();@b@ channel.connect();@b@ byte[] tmp = new byte[1024];@b@ int i=0;@b@ if (is.available() > 0) {@b@ i = is.read(tmp, 0, 1024);@b@ if (i < 0) {@b@ try {@b@ Thread.sleep(500L);@b@ } catch (InterruptedException e) {@b@ if ((channel != null) && (channel.isConnected()))@b@ channel.disconnect(); @b@ e.printStackTrace();@b@ return false;@b@ } @b@ i = is.read(tmp, 0, 1024);@b@ }@b@ res.append(new String(tmp, 0, i));@b@ }@b@ @b@ } catch (JSchException e) {@b@ log.error("channel error:" + e.getMessage());@b@ } catch (IOException e) {@b@ log.error("channel io error:" + e.getMessage());@b@ } finally {@b@ if ((channel != null) && (channel.isConnected()))@b@ channel.disconnect();@b@ }@b@@b@ return false;@b@ }@b@@b@ public void close() {@b@ if ((this.session != null) && (this.session.isConnected()))@b@ this.session.disconnect();@b@ }@b@@b@ public int getPort()@b@ {@b@ return this.port;@b@ }@b@@b@ public void setPort(int port) {@b@ this.port = port;@b@ }@b@@b@ public int getConnectionTimeOut() {@b@ return this.connectionTimeOut;@b@ }@b@@b@ public void setConnectionTimeOut(int connectionTimeOut) {@b@ this.connectionTimeOut = connectionTimeOut;@b@ }@b@}
3. 默认账户类DefaultUserInfo - 账户密码等用户信息
import com.jcraft.jsch.UIKeyboardInteractive;@b@import com.jcraft.jsch.UserInfo;@b@@b@public class DefaultUserInfo implements UserInfo, UIKeyboardInteractive {@b@@b@ private String userName;@b@ private String password;@b@@b@ public String getPassword()@b@ {@b@ return this.password;@b@ }@b@@b@ public void setPassword(String password) {@b@ this.password = password;@b@ }@b@@b@ public boolean promptPassword(String paramString) {@b@ return true;@b@ }@b@@b@ public boolean promptPassphrase(String paramString) {@b@ return true;@b@ }@b@@b@ public boolean promptYesNo(String paramString) {@b@ return true;@b@ }@b@@b@ public String getPassphrase() {@b@ return null;@b@ }@b@@b@ public void showMessage(String paramString)@b@ {@b@ }@b@@b@ public String[] promptKeyboardInteractive(String paramString1, String paramString2, String paramString3, String[] paramArrayOfString, boolean[] paramArrayOfBoolean)@b@ {@b@ return null;@b@ }@b@@b@ public String getUserName() {@b@ return this.userName;@b@ }@b@@b@ public void setUserName(String userName) {@b@ this.userName = userName;@b@ }@b@@b@}
4. linux命令实现类LinuxCommand - 继承DefaultCommand 并实现私有接口初始化
public class LinuxCommand extends DefaultCommand implements Command {@b@@b@ @Override@b@ public void init(String host, String userName, String password, int port, int connectionTimeOut) {@b@ this.userinfo = new DefaultUserInfo();@b@ ((DefaultUserInfo)this.userinfo).setPassword(password);@b@ ((DefaultUserInfo)this.userinfo).setUserName(userName);@b@ genrateSession(host, userName, port, connectionTimeOut, this.userinfo);@b@@b@ }@b@@b@}
5. 测试用例类PowerOffTest - 通过main方法测试关闭指令
public class PowerOffTest {@b@@b@ public static void main(String[] args) {@b@ LinuxCommand command=new LinuxCommand();@b@ command.init("192.168.1.222", "root", "123456", 22, 30000);@b@ command.exec("poweroff", new StringBuffer(""));@b@ }@b@@b@}