`
xiezuoming
  • 浏览: 15996 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

common-net实现telnet客服端

阅读更多
TelnetClientClass.java
package com.abcom;

import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetOption;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
import org.apache.log4j.Logger;

public class TelnetClientClass {
    Logger logger   = Logger.getLogger(TelnetClientClass.class);
    private TelnetClient tc = new TelnetClient();
    private TelnetClientEngine engine;
    private TelnetReceivedHandler handler;
    private OutputStream outstr_ = null;
    private boolean terminal     = false;
    
    private String serverName       = "";
    private String serverIPAdress   = "";
    private int    serverPort       = 23;
    
    private class TelnetClientEngine implements Runnable
    {
        private TelnetClientClass stc;
        private Thread engineThread;
        
        public TelnetClientEngine(TelnetClientClass stc)
        {
            this.stc     = stc;
        }
        
        public void begin()
        {
            engineThread = new Thread(this);
            engineThread.start();
        }
        
        public void irrupt() throws Exception
        {
            engineThread.interrupt();
            engineThread.join();
        }
        
        public boolean end()
        {
            try
            {
                stc.getTC().disconnect();
            }
            catch(Exception e)
            {
                logger.error("TelnetClientEngine:", e);
            }
            
            return false;
        }
        
        public void run()
        {
            outstr_           = stc.getTC().getOutputStream();
            InputStream instr = stc.getTC().getInputStream();
            try
            {
                byte[] buff = new byte[1024];
                int ret_read = 0;
                do
                {
                    ret_read = instr.read(buff);
                    if(ret_read>0)
                    {
                        handler.callback(buff, ret_read);
                    }
                }
                while(ret_read >= 0);
            }
            catch (Exception e)
            {
                logger.warn("Exception: ", e);
            }
            finally
            {
                if(terminal)
                    ;
                else
                {
                    this.begin();
                }
            }
        }
    }
    
    public TelnetClientClass(String name, String address, int port)
    {
        this.serverName         = name;
        this.serverIPAdress     = address;
        this.serverPort         = port;
    }
    
    public void setHandler(TelnetReceivedHandler handler)
    {
        this.handler    = handler;
    }
    
    public boolean start()
    {
        if(!setOption())
            return false;
        if(!connect())
            return false;
        
        engine   = new TelnetClientEngine(this);
        engine.begin();
        
        return true;
    }
    
    public void stop()
    {
        try
        {
            this.terminal   = true;
            this.removeOption();
            engine.end();
            
            return ;
        }
        catch(Exception e)
        {
            logger.error("Exception:", e);
        }
        
        logger.info("Shut down Telnet Client Thread failed, so force to kill.");
        
        try
        {
            engine.irrupt();
        }
        catch(Exception e)
        {
            logger.error("Exception:", e);
        }
    }
    
    private boolean connect()
    {
        logger.info("Connect to " + serverName + ":" + serverIPAdress + ", " + serverPort + ".");
        try
        {
            tc.connect(serverIPAdress, serverPort);
        }
        catch (Exception e)
        {
            logger.warn("Exception:", e);
            return false;
        }
        logger.info("Connected to " + serverName + " successful.");
        return true;
    }
    
    private boolean setOption()
    {
        TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("", false, false, true, false);
        EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
        SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
        
        try
        {
            tc.addOptionHandler(ttopt);
            tc.addOptionHandler(echoopt);
            tc.addOptionHandler(gaopt);
        }
        catch (InvalidTelnetOptionException e)
        {
            logger.warn("Exception: ", e);
            return false;
        }
        return true;
    }
    
    private void removeOption() throws Exception
    {
        tc.deleteOptionHandler(TelnetOption.TERMINAL_TYPE);
        tc.deleteOptionHandler(TelnetOption.ECHO);
        tc.deleteOptionHandler(TelnetOption.SUPPRESS_GO_AHEAD);
    }
    
    private TelnetClient getTC()
    {
        return tc;
    }
    
    public boolean send(byte[] byteArray)
    {
        try
        {
            outstr_.write(byteArray, 0, byteArray.length);
            outstr_.flush();
        }
        catch (Exception e)
        {
            logger.error("Exception:",e);
            return false;
        }
        return true;
    }
}



TelnetSendHandler.java
package com.abcom;

public class TelnetSendHandler {
    private String serverName       = "";
    private String serverIPAdress   = "";
    private int    serverPort       = 23;
    
    private TelnetClientClass telnetclient;
    
    public TelnetSendHandler(String serverName, String serverIPAdress, int serverPort)
    {
    	this.serverName		= serverName;
    	this.serverIPAdress	= serverIPAdress;
    	this.serverPort		= serverPort;
    }
    
    public void initiation()
    {
    	telnetclient	= new TelnetClientClass(this.serverName, this.serverIPAdress, this.serverPort);
    }
    
    public TelnetClientClass getS12TelnetClient()
    {
    	return this.telnetclient;
    }
    
    public boolean send(byte[] byteArray)
    {
    	return this.telnetclient.send(byteArray);
    }

}



TelnetReceivedHandler.java
package com.abcom;

public abstract class TelnetReceivedHandler {
    public abstract void callback(byte[] buff, int length);
}


测试类:Telnet.java
package com.abcom;

public class Telnet extends TelnetReceivedHandler {

	@Override
	public void callback(byte[] buff, int length) {
		// TODO Auto-generated method stub
		System.out.println(new String(buff, 0, length));

	}
	
	public static void main(String[] args)
	{
		try
		{
			TelnetSendHandler telnetSendHandler = new TelnetSendHandler("test", "192.168.8.110", 23);
			telnetSendHandler.initiation();
			telnetSendHandler.getS12TelnetClient().setHandler(new Telnet());
			if(telnetSendHandler.getS12TelnetClient().start())
			{
				telnetSendHandler.send("root\r\n".getBytes());
				Thread.sleep(1000);
				telnetSendHandler.send("123456\r\n".getBytes());
				Thread.sleep(1000);
				telnetSendHandler.send("ls -l\r\n".getBytes());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
分享到:
评论

相关推荐

    common-image-3.1.1-API文档-中文版.zip

    赠送jar包:common-image-3.1.1.jar; 赠送原API文档:common-image-3.1.1-javadoc.jar; 赠送源代码:common-image-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-image-3.1.1.pom; 包含翻译后的API文档:...

    proto-google-common-protos-1.17.0-API文档-中文版.zip

    赠送jar包:proto-google-common-protos-1.17.0.jar; 赠送原API文档:proto-google-common-protos-1.17.0-javadoc.jar; 赠送源代码:proto-google-common-protos-1.17.0-sources.jar; 赠送Maven依赖信息文件:...

    common-io-3.1.1-API文档-中文版.zip

    赠送jar包:common-io-3.1.1.jar; 赠送原API文档:common-io-3.1.1-javadoc.jar; 赠送源代码:common-io-3.1.1-sources.jar; 赠送Maven依赖信息文件:common-io-3.1.1.pom; 包含翻译后的API文档:common-io-...

    hadoop-common-2.2.0-bin-32.rar

    hadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-...

    flink-table-common-1.12.7-API文档-中文版.zip

    赠送jar包:flink-table-common-1.12.7.jar; 赠送原API文档:flink-table-common-1.12.7-javadoc.jar; 赠送源代码:flink-table-common-1.12.7-sources.jar; 赠送Maven依赖信息文件:flink-table-common-1.12.7....

    hadoop-common-2.2.0-bin-master(包含windows端开发Hadoop2.2需要的winutils.exe)

    这个压缩包“hadoop-common-2.2.0-bin-master”是Hadoop 2.2.0版本的公共库二进制版本,包含了在Windows平台上开发和运行Hadoop所需的一些关键工具,特别是对于开发者来说非常重要的`winutils.exe`。 `winutils.exe...

    common-io,common-net打包奉送

    Apache Commons Net库(common-net)主要处理各种网络协议,包括FTP、Telnet、NNTP等。FTP部分提供了丰富的API,使得开发者可以轻松地执行FTP相关的任务。以下是一些关键功能: 1. FTP连接管理:创建和管理FTP连接...

    springfox-swagger-common-3.0.0-API文档-中文版.zip

    赠送jar包:springfox-swagger-common-3.0.0.jar; 赠送原API文档:springfox-swagger-common-3.0.0-javadoc.jar; 赠送源代码:springfox-swagger-common-3.0.0-sources.jar; 赠送Maven依赖信息文件:springfox-...

    weixin-java-common-3.5.0-API文档-中文版.zip

    赠送jar包:weixin-java-common-3.5.0.jar; 赠送原API文档:weixin-java-common-3.5.0-javadoc.jar; 赠送源代码:weixin-java-common-3.5.0-sources.jar; 赠送Maven依赖信息文件:weixin-java-common-3.5.0.pom;...

    netty-transport-native-unix-common-4.1.68.Final-API文档-中文版.zip

    赠送jar包:netty-transport-native-unix-common-4.1.68.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.68.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.68....

    activiti-common-rest-5.21.0-API文档-中英对照版.zip

    赠送jar包:activiti-common-rest-5.21.0.jar; 赠送原API文档:activiti-common-rest-5.21.0-javadoc.jar; 赠送源代码:activiti-common-rest-5.21.0-sources.jar; 赠送Maven依赖信息文件:activiti-common-rest-...

    hadoop-common-2.2.0-bin-master.zip

    hadoop-common-2.2.0-bin-master(包含windows端开发Hadoop和Spark需要的winutils.exe),Windows下IDEA开发Hadoop和Spark程序会报错,原因是因为如果本机操作系统是windows,在程序中使用了hadoop相关的东西,比如写入...

    wildfly-common-1.5.2.Final-API文档-中文版.zip

    赠送jar包:wildfly-common-1.5.2.Final.jar; 赠送原API文档:wildfly-common-1.5.2.Final-javadoc.jar; 赠送源代码:wildfly-common-1.5.2.Final-sources.jar; 赠送Maven依赖信息文件:wildfly-common-1.5.2....

    common-net-ftp

    ftp upfile FTPClient java

    hadoop-common-2.6.0-bin-master.zip

    `hadoop-common-2.6.0-bin-master.zip` 是一个针对Hadoop 2.6.0版本的压缩包,特别适用于在Windows环境下进行本地开发和测试。这个版本的Hadoop包含了对Windows系统的优化,比如提供了`winutils.exe`,这是在Windows...

    sentinel-transport-common-1.8.0-API文档-中文版.zip

    赠送jar包:sentinel-transport-common-1.8.0.jar; 赠送原API文档:sentinel-transport-common-1.8.0-javadoc.jar; 赠送源代码:sentinel-transport-common-1.8.0-sources.jar; 赠送Maven依赖信息文件:sentinel-...

    netty-common-4.1.65.Final-API文档-中英对照版.zip

    赠送jar包:netty-common-4.1.65.Final.jar; 赠送原API文档:netty-common-4.1.65.Final-javadoc.jar; 赠送源代码:netty-common-4.1.65.Final-sources.jar; 赠送Maven依赖信息文件:netty-common-4.1.65.Final....

    hadoop-common-2.7.2.jar

    hadoop-common-2.7.2.jar

    netty-transport-native-unix-common-4.1.73.Final-API文档-中文版.zip

    赠送jar包:netty-transport-native-unix-common-4.1.73.Final.jar; 赠送原API文档:netty-transport-native-unix-common-4.1.73.Final-javadoc.jar; 赠送源代码:netty-transport-native-unix-common-4.1.73....

    redis3.2+jedis2.8.jar+common-pool2.jar+common-pool2-source.rar

    Common-Pool2.jar是Apache Commons Pool库的第二个主要版本,这是一个对象池设计模式的实现,用于提高对象的重用性,减少创建和销毁对象的开销。在Jedis中,它被用于管理Redis连接,通过复用已建立的连接,避免频繁...

Global site tag (gtag.js) - Google Analytics