通讯设备EPON+EOC网管,系统与设备对接怎么做
小弟在某地广电网络工作,因为历史及其他原因导致机房数据设备厂商较多,且相互之间网管不兼容。为了工作便利便谋发了自己开发一套管理系统的想法,目前WEB前台已完成,但在设备对接这块卡住了。
------------------------------------华丽的分割线【暗号就不对了】--------------------------------------------
1.需要管理的设备如下
OLT+EOC
2.实现方法
查阅了很多信息,目前市面上的网管都是用snmp协议来做的,一线厂商如26、28都是公开了设备的mib库,所以可以很轻松实现。但是现实很骨感,我们的几家设备厂商mib库被他们视为传家宝,拒绝提供。小弟人微言轻这条路基本没什么希望了。
然后我就想到了telnet,因为设备的指令和返回信息格式都是固定的,所以我就想通过telnet方式来实现,虽然实现起来相关困难(需要针对不同设备厂商编写不同的解析程序),但好处也比较明显。在不需要厂商mib的情况下,只有设备支持telnet操作就可以了。
3.遇到的困难
项目使用apache conmens net包,该包提供了telnet/SSH操作类,参照官网例子写了一个telnet工具类。
目前遇到的困难就是,在遇到返回信息过长需要继续输入空格键继续输入下一业列表是进入red线程死循环,请问有没有大神做过类似的功能?或者提供相应的解决方法
package com.gd.olt.util.kt; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.net.SocketException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.net.telnet.TelnetClient; import com.gd.olt.pojo.OnuEntity; public class KtOnuList { Object lock = new Object(); TelnetClient telnet = null; private InputStream in; private PrintStream out; private static final String ORIG_CODEC = "ISO8859-1"; private static final String TRANSLATE_CODEC = "GBK"; List<OnuEntity> objs = new ArrayList<OnuEntity>();// 鍒涘缓ONU鍒楄〃瀵硅薄闆嗗悎 private String oltId; private String oltTypeCode; private String oltTypeName; private void connect(String ip) throws SocketException, IOException { telnet = new TelnetClient("VT100");// VT100 VT52 VT220 VTNT ANSI telnet.connect(ip, 23); in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream()); } private void readToEnd() { ReadThread readThread = new ReadThread(); readThread.start(); try { readThread.join(); } catch (Exception e) { } readThread = null; } private void readUntil(String str) { char last = str.charAt(str.length() - 1); String[] ss; try { StringBuffer sb = new StringBuffer(); char c; int code = -1; boolean ansiControl = false; boolean start = true; while ((code = (in.read())) != -1) { c = (char) code; if (c == '33') {// vt100鎺у埗鐮侀兘鏄互33寮�澶寸殑銆� ansiControl = true; int code2 = in.read(); char cc = (char) code2; if (cc == '[' || cc == '(') { } } if (!ansiControl) { if (c == 'r') { String outstr = new String(sb.toString().getBytes( ORIG_CODEC), TRANSLATE_CODEC); // 姝e垯鍖归厤 String regEx = "^\d+\/\d+\:"; Pattern pat = Pattern.compile(regEx); Matcher mat = pat.matcher(outstr.trim()); if (mat.find()) { OnuEntity entity = setOnu(outstr); if (entity != null) { objs.add(entity); } } if (sb.lastIndexOf(str) != -1) { break; } sb.delete(0, sb.length()); } else if (c == 'n') { // break; } else sb.append(c); if (sb.lastIndexOf(str) != -1) { break; } } if (ansiControl) { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '"') { ansiControl = false; } } } System.out.println(new String(sb.toString().getBytes(ORIG_CODEC), TRANSLATE_CODEC)); } catch (Exception e) { e.printStackTrace(); } } private void write(String s) { try { out.write(s.getBytes()); out.flush(); } catch (Exception e) { } } private void write(String s, int sleep) { write(s); try { Thread.sleep(sleep); } catch (Exception e) { } } /** * 瀹屾垚涔嬪悗蹇呴』鍏抽棴 */ private void close() { if (out != null) out.close(); if (in != null) try { in.close(); } catch (IOException e1) { } if (telnet != null) try { telnet.disconnect(); } catch (IOException e) { } } /** * 璇诲彇涓荤嚎绋嬶紝璐熻矗绠$悊瀛愮嚎绋嬨�傞槻姝㈣鍙栨椂涓嶅姩浜嗭紝杩欐椂灏辨姏寮冭鍙栧瓙绾跨▼ * * @author chruan * */ private class ReadThread extends Thread { public void run() { synchronized (lock) {// 鍙兘涓�涓鍙� SubReadThread sub = new SubReadThread(); sub.start(); int last = sub.count; while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { } if (last == sub.count) { sub.stop(); break; } else { last = sub.count; } } String s = sub.sb.toString(); try { System.out.println(new String(s.getBytes(ORIG_CODEC), TRANSLATE_CODEC)); } catch (UnsupportedEncodingException e) { System.out.println(s); } sub = null; } // System.out.println("===========ReadThread end============="); } } /** * 璇诲彇瀛愮嚎绋嬶紝瀹屾垚瀹為檯璇诲彇 * * @author chruan * */ private class SubReadThread extends Thread { int count = 0; StringBuffer sb = new StringBuffer(2048); public void read() { try { char c; int code = -1; boolean ansiControl = false; boolean start = true; while ((code = (in.read())) != -1) { count++; c = (char) code; if (c == '33') { ansiControl = true; int code2 = in.read(); char cc = (char) code2; count++; if (cc == '[' || cc == '(') { } } if (!ansiControl) { if (c == 'r') { String olds = new String(sb.toString().getBytes( ORIG_CODEC), TRANSLATE_CODEC); sb.delete(0, sb.length()); } else if (c == 'n') ; else sb.append(c); } if (ansiControl) { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '"') { ansiControl = false; } } } } catch (Exception e) { } } public void run() { read(); } } /*** * * @param entity浼犲叆OLT * 锛屼富瑕両P锛岀敤鎴峰悕瀵嗙爜 * @return 杩斿洖ONUentity鐨凩IST闆嗗悎 */ public List<OnuEntity> loadOnuListInfo(String id, String ip, String oltName, String oltPwd, String oltTypeCode, String oltTypeName) { this.oltId = id; this.oltTypeCode = oltTypeCode; this.oltTypeName = oltTypeName; try { this.connect(ip); // -----------------鎵ц鍛戒护寮�濮�------------------- readUntil("olt login: ");//登录名 write(oltName);//命令输出 write("n"); readUntil("Password: ");//输入密码 write(oltPwd); write("n"); readUntil("> "); write(" enable ");//进入配置模式 write("n"); readUntil("# "); write(" configure terminal ");//进入全局配置模式 write("n"); readUntil("(config)# "); write("show onu-list-info ");//查询ONU列表 write("n");// 鍙戦�佺‘璁ら敭 // for (int i = 0; i < 5; i++) { // write(" ");// // } // readUntil("--More--");//如果返回结尾为--More-- ******就表示列表未完成,输入空格下一页 // write(" "); // write("n"); // readUntil("# "); // -----------------鎵ц鍛戒护缁撴潫------------------- this.readToEnd(); } catch (Exception e) { e.printStackTrace(); } finally { this.close(); } return objs; } private OnuEntity setOnu(String str) { // 1/1: 1 鍦ㄧ嚎 4920 1 0 192.168.10.1 000F.1EA0.5747 String[] ss = str.split(" "); StringBuffer sb = new StringBuffer(); for (int i = 0; i < ss.length; i++) { String tempStr = ss[i]; if (null == tempStr || "".equals(tempStr.trim())) { } else { sb.append("#" + tempStr); } } String e = sb.toString(); String[] en = e.split("#"); // for (int i = 0; i < en.length; i++) { // String tempStr = en[i]; // System.out.println(tempStr.trim() + "...." + i); // } String llid = en[1].replaceAll(" ", "0");// LLID String status = en[2].trim();// 鐘舵�� String dist = en[3];// 璺濈 String[] ipmac = en[6].split(" "); String mac = ipmac[1]; return new OnuEntity(llid, status, dist, mac, null, oltId, oltTypeCode, oltTypeName); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道能不能顶上去,大家帮帮忙行吗