My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
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


/*
File: eqemuTelnet.java
Copyright (C) 2010: Ronny L. Bull (aka: Cubber)
Date: 9-03-2010
Development OS: Gentoo Linux
Java Version: sun-jdk-1.6.0.20
Description: EQEmu Java Server Manager - Telnet Functions

License:

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

/*
Code Adaption Notice:

This code was adapted from the sample found here:
http://www.informit.com/guides/content.aspx?g=java&seqNum=40
*/

/*
Dependency Notice:

This code requires the apache commons package that can be found here:
http://commons.apache.org/net/
*/

package eqemujsm.telnet;

//Imports
import org.apache.commons.net.telnet.*;
import java.io.*;

public class eqemuTelnet
{

//Variables
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '>';


public eqemuTelnet( String server, int port, String user, String password )
{
try
{

//Connect to the server
telnet.connect(server, port);

//Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream( telnet.getOutputStream() );

//Log in user
readUntil( "Username: ");
write( user );
readUntil( "Password: ");
write( password );

//Advance to prompt
readUntil( prompt + " " );

}
catch ( Exception e )
{
e.printStackTrace();
}
}

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;
}

public void write( String value )
{
try
{
out.println( value );
out.flush();
System.out.println( value );
}
catch ( Exception e )
{
e.printStackTrace();
}
}

public String sendCommand( String command )
{
try
{
write( command );
return readUntil( prompt + " " );
}
catch( Exception e )
{
e.printStackTrace();
}
return null;
}

public void disconnect()
{
try
{
telnet.disconnect();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}

Change log

r8 by RonnyBull on Oct 2, 2010   Diff
added EQEmuJSM source
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3152 bytes, 153 lines
Powered by Google Project Hosting