`
征途2010
  • 浏览: 243312 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

远程采集服务器指标信息(二)通过telnet执行远程命令

阅读更多

远程采集服务器信息,比如说磁盘信息、内存信息。

现介绍java通过telnet执行命令采集服务器信息,比如说执行df、ls、top。

这里在linux环境通过root或者其他用户登录执行解析方式有点出入,下面是代码

/**
 * Telnet远程会话
 * 
 */
public class TelnetSession implements IRemoteSession {
    
    private TelnetClient telnet = new TelnetClient("VT220");
    
    private InputStream in;
    
    private NodeInfoVO nodeInfoVO;
    
    private PrintStream out;
    
    private char prompt = '$';// 普通用户结束
    
    /**
     * 
     * 构造函数
     * @param serverBean
     */
    public TelnetSession(NodeInfoVO nodeInfoVO) {
        try {
            telnet.connect(nodeInfoVO.getServerIp(), DeviceConstants.TELNET_DEFAULT_PORT);
            in = telnet.getInputStream();
            out = new PrintStream(telnet.getOutputStream());
            // 根据root用户设置结束符
            this.prompt = nodeInfoVO.getServerUserName().equals("root") ? '#' : '>';
            login(nodeInfoVO.getServerUserName(), nodeInfoVO.getServerPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * @return 获取 serverBean属性值
     */
    public NodeInfoVO getNodeInfoVO() {
        return nodeInfoVO;
    }
    
    /**
     * 登录
     * 
     * @param user
     * @param password
     */
    public void login(String user, String password) {
        readUntil("login:");
        write(user);
        readUntil("assword:");
        write(password);
        readUntil(prompt + "");
    }
    
    /**
     * 读取分析结果
     * 
     * @param pattern
     * @return
     */
    public String readUntil(String pattern) {
        try {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();
            char ch = (char) in.read();
            while (true) {
                sb.append(ch);
                if (ch == lastChar) {
                    if (sb.toString().endsWith(pattern)) {
                        byte[] temp = sb.toString().getBytes("iso8859-1");
                        return new String(temp, "GBK");
                    }
                }
                ch = (char) in.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 读取分析结果
     * 
     * @param pattern
     * @return
     */
    public String readResultUntil(String pattern) {
        BufferedReader inputStream = null;
        try {
            // char lastChar = pattern.charAt(pattern.length() - 1);
            // StringBuffer sb = new StringBuffer();
            // Thread.sleep(1000);
            // final Scanner sc = new Scanner(in, "iso8859-1");
            //            
            // while (sc.hasNextLine()) {
            // String ss=sc.nextLine();
            // if(ss.contains("mmc")){
            // System.out.println("---------");
            // //return null;
            // }
            // byte[] temp = ss.toString().getBytes("iso8859-1");
            // System.out.println(new String(temp, "GBK"));
            // }
            //            
            // sc.close();
            inputStream = new BufferedReader(new InputStreamReader(in));
            String line = inputStream.readLine();
            while (inputStream.ready()) {
                System.out.println(line);
                line = inputStream.readLine();
            }
            inputStream.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    /**
     * 写操作
     * 
     * @param value
     */
    public void write(String value) {
        try {
            out.println(value);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 向目标发送命令字符串
     * 
     * @param command
     * @return
     */
    public String execCommand(String command) {
        try {
            write(command);
            return readUntil(prompt + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 关闭连接
     */
    @Override
    public void close() {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        try {
            telnet.disconnect();
        } catch (Exception e) {
            telnet = null;
        } finally {
            telnet = null;
        }
    }
    
    public static void main(String[] args) {
//        TelnetSession she = null;
//        try {
//            she = new TelnetSession(new ServerBean("10.10.5.183", 23, "comtop", "comtophello"));
//            System.out.println(she.execCommand("wmic path win32_operatingsystem get  TotalVisibleMemorySize,"
//                + "TotalVirtualMemorySize ,FreePhysicalMemory,FreeVirtualMemory"));
//        } catch (Exception e) {
//            she.disconnect();
//        } finally {
//            she.disconnect();
//        }
    }


}

 

分享到:
评论
1 楼 joinmition 2017-12-28  
兄弟,问一下,这个从服务器返回数据的时候老是阻塞怎么解决?

相关推荐

Global site tag (gtag.js) - Google Analytics