/** *@date 2015年2月4日 下午3:15:13 *@author XUQIANG *@filename TelnetConnection.java *@package com.merit.monitor.device.parameter.sdk */ package com.merit.monitor.device.parameter.sdk; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.SocketException; import org.apache.commons.net.telnet.TelnetClient; import com.merit.monitor.device.parameter.logs.ILog; import com.merit.monitor.device.parameter.logs.LogImpl; /** * @author XUQIANG * */ public class TelnetConnection { private static ILog log = new LogImpl(); private TelnetClient telnet = null; // private String username = ""; private String password = ""; private String prompt = "#"; private InputStream in; private PrintStream out; public TelnetConnection(String host, int port) { if(telnet == null) { telnet = new TelnetClient(); try { telnet.connect(host, port); in = telnet.getInputStream(); out = new PrintStream(telnet.getOutputStream()); } catch (SocketException e) { log.logForException(TelnetConnection.class, e.getMessage()); } catch (IOException e) { log.logForException(TelnetConnection.class, e.getMessage()); } } } /** * @return the prompt */ public String getPrompt() { return prompt; } /** * @param prompt the prompt to set */ public void setPrompt(String prompt) { this.prompt = prompt; } /** * 关闭打开的连接 * @param telnet */ public void close(TelnetClient telnet) { if(telnet != null) { try { telnet.disconnect(); } catch (IOException e) { log.logForException(TelnetConnection.class, e.getMessage()); } } if(this.telnet != null) { try { this.telnet.disconnect(); } catch (IOException e) { log.logForException(TelnetConnection.class, e.getMessage()); } } } /** * 登录到远程机器中 */ public void login(String username, String password, String prompt) { //处理命令行的提示字符 if(prompt == null || "".equals(prompt)) { if(this.prompt.equals("#")) prompt = ("root".equals(username)) ? "#" : "$"; } else { this.prompt = prompt; } this.password = password; readUntil("Username:"); write(username); readUntil("Password:"); write(this.password); readUntil(this.prompt + ""); // 其它交换机登录后如果有提示信息也需要添加 if(this.prompt.indexOf("Quidway") != -1) readUntil("login"); } /** * 读取分析结果 * * @param pattern * @return */ public String readUntil(String pattern) { StringBuffer sb = new StringBuffer(); try { int len = 0; while((len = in.read()) != -1) { sb.append((char)len); if(sb.toString().endsWith(pattern)) { return sb.toString(); } } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return ""; } /** * 写操作 * * @param value */ public void write(String value) { try { out.println(value); out.flush(); } catch (Exception e) { log.logForException(TelnetConnection.class, e.getMessage()); } } /** * 向目标发送命令字符串 * * @param command * @return */ public String sendCommand(String command) { try { write(command); return readUntil(prompt + ""); } catch (Exception e) { log.logForException(TelnetConnection.class, e.getMessage()); } return ""; } /** * 关闭连接 */ public void disconnect() { try { telnet.disconnect(); } catch (Exception e) { log.logForException(TelnetConnection.class, e.getMessage()); } } }
Junit
@Test public void testTelnet() { try { System.out.println("启动Telnet..."); String ip = "191.168.*.*"; int port = 23; String user = "admin"; String password = "****"; TelnetConnection telnet = new TelnetConnection(ip, port); telnet.setPrompt("<Quidway>"); telnet.login(user, password, ""); telnet.setPrompt("[Quidway]"); String r1 = telnet.sendCommand("system-view");//display snmp-agent local-engineid telnet.setPrompt("[Quidway-Ethernet1/0/5]"); String r2 = telnet.sendCommand("interface Ethernet1/0/5"); String r3 = telnet.sendCommand("undo shutdown"); System.out.println("显示结果"); System.out.println(r1); System.out.println(r2); System.out.println(r3); System.out.println((r3.indexOf(telnet.getPrompt()) != -1) && r3.indexOf("^") == -1); telnet.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
时间: 2024-10-06 06:28:59