View Javadoc

1   /**
2    * 
3    */
4   package net.sf.provisioner.connectors;
5   
6   import net.sf.provisioner.config.NetworkElement;
7   
8   import org.sadun.util.UnixLoginHandler;
9   import org.sadun.util.UnixLoginHandler.LoginIncorrectException;
10  import org.sadun.util.TelnetInputStreamConsumer;
11  import org.sadun.util.OperationTimedoutException;
12  
13  
14  import java.net.*;
15  import java.io.*;
16  
17  /**
18   * Esta clase .
19   * <p>
20   * 
21   * .
22   * <p>
23   * 
24   *             
25   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:26 $
26   * @author Gonzalo Espert
27   */
28  public class TelnetConnector extends Connector {
29  
30  	public TelnetInputStreamConsumer inputConsumer;
31  	
32  	public PrintWriter writer;
33  	
34  	private NetworkElement ne;
35  	
36  	private UnixLoginHandler endPoint;
37  	
38  	/**
39       * .
40       * <p>
41       * 
42       * .
43       * <p>
44       * 
45       * 
46       * 
47       * @param 
48       *            
49       * @throws 
50       *            
51       *           
52       */
53  	public TelnetConnector(NetworkElement ne) { this.ne = ne; }
54  	
55  	/**
56       * .
57       * <p>
58       * 
59       * .
60       * <p>
61       * 
62       * 
63       * 
64       * @param 
65       *            
66       * @throws 
67       *            
68       *           
69       */
70  	public void Connect() throws Exception {
71  
72  		 try {
73  			 // Create a socket to the host
74  			 Socket s = new Socket(this.ne.name, Integer.parseInt(this.ne.port));
75  			 // Create a UnixLoginHandler object over the socket to login to the host
76  			 this.endPoint = new UnixLoginHandler(s);
77  			 this.endPoint.setTimeout(300000);
78  			 this.endPoint.setLoginPromptSequence(this.ne.loginPrompt);
79  			 this.endPoint.setLoginIncorrectSequence(this.ne.loginErrorSecuence);
80  			 this.endPoint.setSendInitialCRLF(this.ne.sendInitialCRLF);
81  			 // Create a TelnetInputStreamConsumer object over the socket by logging in to the host
82  			 logger.info("Iniciando sesion en: " + this.ne.name + " .............");
83  			 this.inputConsumer = this.endPoint.doLogin(this.ne.user,this.ne.password);
84  			 logger.info("Sesion iniciada exitosamente!");
85  			 // Create a stream writer
86  			 this.writer = new PrintWriter(new OutputStreamWriter(s.getOutputStream()), true);
87  		 } catch (UnknownHostException e) {
88  			 Exception ex = new Exception("Host desconocido.");
89  	    	 throw ex;
90  	     } catch (IOException e) {
91  	    	 Exception ex = new Exception("Error de I/O en la secuencia de login.");
92  	    	 throw ex;
93  	     } catch (LoginIncorrectException e) {
94  	    	 Exception ex = new Exception("Login o password incorrectos.");
95  	    	 throw ex;
96  	     } catch (OperationTimedoutException e) {
97  	    	 Exception ex = new Exception("Se produjo un timeout en la secuencia de login.");
98  	    	 throw ex;
99  	     }
100 	}
101 
102 	/**
103      * .
104      * <p>
105      * 
106      * .
107      * <p>
108      * 
109      * 
110      * 
111      * @param 
112      *            
113      * @throws 
114      *            
115      *           
116      */
117 	public void Disconnect() {
118 	
119 		this.endPoint.doLogout();
120 
121 	}
122 
123 }