zoukankan      html  css  js  c++  java
  • TelnetTerminal

    package com.network.telnet;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    import org.apache.commons.net.telnet.TelnetClient;
    
    public class TelnetTerminal extends Thread {
        public static final int DEFAULT_TELNET_PORT = 23;
        /**
         * the TelnetClient of commons-net
         */
        public TelnetClient telnet;
        /**
         * the output of remote
         */
        private StringBuffer remoteInfo = new StringBuffer();
        private InputStream in;
        private PrintStream out;
    
        /**
         * use default port
         * 
         * @param ip
         */
        public TelnetTerminal(String ip) {
            this(ip, DEFAULT_TELNET_PORT);
        }
    
        /**
         * create a telnet terminate
         * 
         * @param ip
         * @param port
         */
        public TelnetTerminal(String ip, int port) {
            try {
                telnet = new TelnetClient();            
                telnet.connect(ip, port);
                //to sure the thread must be stop and socket must be close
                telnet.setSoTimeout(3000);
                in = telnet.getInputStream();
                out = new PrintStream(telnet.getOutputStream());
                // open the thread of received
                start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * wirte to remote
         * 
         * @param command
         */
        public void write(String command) {
            try {
                // the interval of execute command
                Thread.sleep(500);
                // a command end with '
    
    '
                out.println(command);
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * close connect
         */
        public void disconnect() {
            try {
                telnet.disconnect();
                telnet = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            byte[] buff = new byte[1024];
            int len = 0;
            try {
                while ((len = in.read(buff)) != -1) {
                    String str = new String(buff, 0, len);
                    remoteInfo.append(str);
                }
            } catch (IOException e) {
    
            }
        }
    
        /**
         * exceute script
         * 
         * @param scripts
         */
        public void runCMD(String... scripts) {
            for (String cmd : scripts) {
                write(cmd.trim());
            }
        }
    
        /**
         * get output info of the remote, but this method will block 1000ms
         * 
         * @return
         */
        public String getRemoteInfo() {
            try {
                Thread.sleep(1000);
                return remoteInfo.toString();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "";
        }
    }
  • 相关阅读:
    hdu 2141 二分搜索
    什么是你的核心竞争力之一?
    Ubuntu系统开机后显示器提示“不能显示此视频模式,请将电脑显示输入设置为1920×1080@60Hz”
    [置顶] java高级工程师struts的内部运行机制详解
    一步步理解Linux进程(3)内核中进程的实现
    2013年4月19日佳都新太笔试题+解答
    Android 通过Service单独进程模仿离线推送 Server Push
    缓存研究
    解决MDK4以上版本没法对STM32软件仿真
    windows调试器之Visual C++
  • 原文地址:https://www.cnblogs.com/wellla/p/4200821.html
Copyright © 2011-2022 走看看