View Javadoc

1   /**
2    * 
3    */
4   package net.sf.provisioner.adapters;
5   
6   import java.io.IOException;
7   
8   import net.sf.provisioner.config.NetworkElement;
9   import net.sf.provisioner.connectors.TelnetConnector;
10  
11  
12  /**
13   * .
14   * <p>
15   * 
16   * .
17   * <p>
18   * 
19   *             
20   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:24 $
21   * @author Gonzalo Espert
22   */
23  public class TelnetAdapter extends Adapter{
24  	
25  	private NetworkElement ne;
26  	
27  	/**
28       * .
29       * <p>
30       * 
31       * .
32       * <p>
33       * 
34       * 
35       * 
36       * @param 
37       *            
38       * @throws 
39       *            
40       *           
41       */
42  	public TelnetAdapter(NetworkElement ne) { this.ne = ne; }
43  	
44  	/**
45       * .
46       * <p>
47       * 
48       * .
49       * <p>
50       * 
51       * 
52       * 
53       * @param 
54       *            
55       * @throws 
56       *            
57       *           
58       */
59  	public String ExecuteCommand(String command) throws Exception {
60  		
61  		String trash;
62  		String result = null;
63  		
64  		/* Obtener una conexion telnet */
65  		TelnetConnector connection = new TelnetConnector(this.ne);
66  		connection.Connect();
67  		
68  		/* Vaciamos el stream de entrada */
69  		try {
70  			
71  			trash = connection.inputConsumer.consumeInput(3000);
72  			logger.info("stdin trash: " + trash);
73  			
74  		} catch (IOException e) {
75  	    	throw e;
76  	    }
77  		
78  		/* Enviamos el comando */
79  		command = command + "\r\n";
80  		logger.info("Sending command: \"" + command + "\"");
81  		// usamos prinln + \r\n porque sino cuando enviamos desde Unix\Linux no funciona
82  		connection.writer.println(command);
83  
84  	    /* Recibimos la respuesta */
85  	    try {
86  	    		    	
87  	    	result = connection.inputConsumer.consumeInput(10000);
88  	    	logger.info("Consumption: " + result);
89  	    	
90  	    } catch (IOException e) {
91  	    	throw e;
92  	    }
93  	    
94  	    connection.Disconnect();
95  	    
96  	    return result;
97  	}
98  }