blob: c8441ac251301b7918d25bfe0a14bdfa17f26883 (
plain)
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
|
/***********************************************************************
*
* Copyright (c) 2006 Manuel Traut and Volker Dahnke
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Manuel Traut and Volker Dahnke
*
************************************************************************/
package YalpOutputs.YalpVlcTelnetOutput;
import java.net.Socket;
import java.io.*;
import org.apache.commons.net.telnet.*;
/*******************************************************************************
*
* Class TelnetInterface
*
* <em>handels telnet connection</em>
*
* @author Volker Dahnke / Manuel Traut
*
* @version 0.1 20-11-2005<br>
*
* @see VlcStreamer
*
******************************************************************************/
public class TelnetInterface {
private PrintStream out;
private InputStream in;
private TelnetClient tc = new TelnetClient();
/**
* creates socket, logs on
*
* @param host
* hostname of telnetserver
* @param port
* destination port
* @param pass
* password
*
*/
public TelnetInterface(String host, int port, String pass){
try{
this.tc.connect(host,port);
this.out = new PrintStream(this.tc.getOutputStream());
this.in = this.tc.getInputStream();
} catch(IOException e){
System.out.println("server.TelnetInterface.java: Telnet connection "+host+":"+port+" failed");
return;
}
readUntil( "Password:" );
write(pass);
// Advance to a prompt
readUntil("> ");
}
/**
* write to TelnetInterface
* @param value
* String to write to Interface
*/
public void write( String value ) {
try {
out.println( value );
out.flush();
System.out.println( value );
}
catch( Exception e ) {
e.printStackTrace();
}
}
/**
* reads output of telnet client
* @param pattern
* for stop reading
* @return String
*/
public String readUntil( String pattern ) {
try {
char lastChar = pattern.charAt( pattern.length() - 1 );
StringBuffer sb = new StringBuffer();
char ch = ( char )in.read();
while( true ) {
System.out.print( ch );
sb.append( ch );
if( ch == lastChar ) {
if( sb.toString().endsWith( pattern ) ) {
return sb.toString();
}
}
ch = ( char )in.read();
}
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
}
/**
* executes command
* @param cmd
* any telnet command
* @return String
* telnet output caused by the command
* null if failed
*/
public String exec(String cmd){
try {
write(cmd);
return readUntil( "> " );
}
catch( Exception e ) {
e.printStackTrace();
}
return null;
}
/**
* close telnet connection
*
* @return boolean
* true: connection closed
* false: connection close failed
*/
public boolean close(){
try {
this.tc.disconnect();
} catch(IOException e){
return false;
}
return true;
}
}
|