-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaTelnetClient.java
More file actions
160 lines (146 loc) · 4.63 KB
/
JavaTelnetClient.java
File metadata and controls
160 lines (146 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*Created by Diedas 15/06/2014
** version 0.1
** GPL 3
** use! all comments welcome!
** This is an Java telnet client. comes in two flavors: java.net.Socket and org.apache.commons.net.telnet.TelnetClient
*/
import org.apache.commons.net.telnet.TelnetClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
//Sample usage.
//JavaTelnetClient telnet = new AutomatedTelnetClient(addr, login, password, prompt,10000);
//String result = telnet.sendCommand("iwlist " + iface + " scanning");
//telnet.disconnect();
//System.out.println(result);
public class JavaTelnetClient {
private TelnetClient telnet;
private Socket socket;
private final InputStream in;
private final PrintStream out;
private final String login;
private final String password;
private final String prompt;
private long timeout = 1000l;
private final static boolean debug = false;//set to true for debugging
//this constructor is for Apache commons TelnetClient socket
public JavaTelnetClient(String server, String user, String password, String prompt, boolean mode) throws IOException {
telnet = new TelnetClient();
telnet.connect(server, 23);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
this.login = user;
this.password = password;
this.prompt = prompt;
// readUntil("Username : ");
// write(user);
// readUntil("Password : ");
// write(password);
try {
Thread.sleep(200L);
} catch (Exception e) {
}
write(user);
try {
Thread.sleep(200L);
} catch (Exception e) {
}
write(password);
readUntil(prompt);
}
//this constructor is for plain socket. So You do not have to use apache library.
public JavaTelnetClient(String server, String user, String password, String prompt, Integer timeout) throws IOException {
socket = new Socket(server, 23);
in = socket.getInputStream();
out = new PrintStream(socket.getOutputStream());
this.login = user;
this.password = password;
this.prompt = prompt;
if (timeout != null)
this.timeout = timeout;
// readUntil(loginSTR);
// write(user);
// readUntil(passwdSTR);
// write(password);
try {
Thread.sleep(200L);
} catch (Exception e) {
}
write(user);
try {
Thread.sleep(200L);
} catch (Exception e) {
}
write(password);
readUntil(prompt);
}
public String readUntil(String pattern) throws IOException {
long lastTime = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
while (true) {
int c = -1;
byte[] text;
if (in.available() > 0) {
c = in.read(text = new byte[in.available()]);
sb.append(new String(text));
}
long now = System.currentTimeMillis();
if (c != -1) {
lastTime = now;
}
if (now - lastTime > timeout) {
break;
}
if (sb.toString().contains(pattern)) {
return sb.toString();
}
try {
Thread.sleep(50);
} catch (Exception e) {
}
}
return sb.toString();
}
//just sends text to server
public void print(String value) {
try {
out.println(value + ";");
} catch (Exception e) {
e.printStackTrace();
}
}
//this method writes to server, but waits no prompt.
public void write(String value) {
try {
out.println(value);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
//sends command and receives result
public String sendCommand(String command) {
try {
write(command);
String until = readUntil(prompt);
if (debug)
System.out.println("\n command -> \n" + command + " \n ouptut-> \n" + until);
return until;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//closes a this client. you may want to send command "exit" beforehand
public void disconnect() {
try {
if (socket != null)
socket.close();
if (telnet != null)
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}