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
19
20
21
22
23
24
25
26
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
41
42
43
44
45
46
47
48
49
50
51
52
53 public TelnetConnector(NetworkElement ne) { this.ne = ne; }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public void Connect() throws Exception {
71
72 try {
73
74 Socket s = new Socket(this.ne.name, Integer.parseInt(this.ne.port));
75
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
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117 public void Disconnect() {
118
119 this.endPoint.doLogout();
120
121 }
122
123 }