TelnetUtil.java

4946 단어 자바

package com.ailk.ess.webapp2.servermng.net;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.telnet.TelnetClient;

public class TelnetUtil {
	
	//telnet     VT220/VT52
	TelnetClient client = new TelnetClient("VT52");
	
	StringBuffer buffer = new StringBuffer();
	InputStream inputStream = null; //    ,          
	OutputStream outputStream = null; //    ,       
	
	private static List<String> defaultPromt = new ArrayList<String>();
	private static List<String> user = new ArrayList<String>();
	private static List<String> pass = new ArrayList<String>();
	//    
	public static int defaultport = 23;
	
	static {
		defaultPromt.add("#");
		defaultPromt.add(">");
		defaultPromt.add("%");
		user.add("ogin:");
		pass.add("assword:");
	}

	/**
	 * @param hostname
	 *               IP  
	 * @param port
	 *            telnet  
	 * @param username
	 *               
	 * @param password
	 *              
	 * @throws Exception
	 */
	public TelnetUtil(String hostname, int port, String username, String password) throws Exception {
		//      
		conn(hostname, port);
		//        
		this.inputStream = this.client.getInputStream();
		//        
		this.outputStream = this.client.getOutputStream();
		
		login(username, password);
	}

	/**
	 *     
	 * 
	 * @throws Exception
	 */
	public void close() throws Exception {
		this.client.disconnect();
	}

	/**
	 *       
	 * 
	 * @param hostname
	 *               IP  
	 * @param port
	 *              
	 * @throws Exception
	 */
	private void conn(String hostname, int port) throws Exception {
		this.client.connect(hostname, port);
	}

	/**
	 *      
	 * 
	 * @param username
	 *               
	 * @param password
	 *              
	 * @throws Exception
	 */
	private void login(String username, String password) throws Exception {
		sendCommand(username, user);
		List<String> temp = new ArrayList<String>();
		temp.add(":");
		String result = getResult(temp);

		if (!(result.trim().endsWith("word:"))) {
			throw new Exception("Invalid user:" + username);
		}
		temp.add("#");
		temp.add(">");
		temp.add("%");
		sendCommand(password, pass);
		result = getResult( temp );

		if ((result.trim().endsWith("word:"))
				|| (result.trim().endsWith("ogin:"))) {
			throw new Exception("Invalid username or password:" + username
					+ " " + password);
		}
	}

	public void sendCommand(String command) throws Exception {
		sendCommand(command, defaultPromt);
	}

	public String getResult() throws Exception {
		return getResult(defaultPromt);
	}

	/**
	 *         
	 * 
	 * @param command
	 *                
	 * @param wantedEndString
	 * @throws Exception
	 */
	public void sendCommand(String command, List<String> wantedEndString)
			throws Exception {
		waitForString(wantedEndString);
		this.buffer.delete(0, this.buffer.length());
		//         
		// System.out.println(command + "
"); this.outputStream.write((command + "
").getBytes()); this.outputStream.flush(); } public String getResult(List<String> endString) throws Exception { waitForString(endString); return this.buffer.toString(); } private void waitForString(List<String> wantedEndString) throws Exception { int aword = 0; boolean matchOne = false; while (!(matchOne)) { for (int i = 0; i < wantedEndString.size(); ++i) { String back = this.buffer.toString().trim(); if ((back.endsWith((String)wantedEndString.get(i))) && (this.inputStream.available() == 0)){ matchOne = true; } } if (matchOne) { return; } aword = this.inputStream.read(); // System.out.print((char) aword); if (aword < 0) { throw new Exception("Connection disconnect..."); } this.buffer.append((char) aword); } } public boolean isClosed() { return (!(this.client.isConnected())); } }

좋은 웹페이지 즐겨찾기