標籤:
/** *@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 blocke1.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
@Testpublic 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-engineidtelnet.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();}}
Java 通過telnet協議 操作遠程交換器