我很生气。我正在尝试编写一个Java类来与Telnet进行交互。我看到Apache Commons和Jsacpe有API。我正在使用Jscape的Sinetfactory。我连接的Telnet会在telnet.connect()发生时立即发送输入“用户名?:”的提示。我需要验证这个提示是否真的发生了所以我不会在其他事情发生时写下答案。我对此缺乏经验,并确信有一个简单的答案,只是想知道是否有人可以提供帮助。
这就是我所拥有的,它有点草率,因为我已经玩了一段时间不确定如何实际读取流中的最后一个字符。
import com.jscape.inet.telnet.*;
public class TelnetTest extends TelnetAdapter {
private final static String USER = "xxx\r";
private final static String PWORD = "yyy\r";
private final static String COMMAND = "zzz\r";
private final static byte[] USER_BYTE = USER.getBytes();
private final static byte[] PWORD_BYTE = PWORD.getBytes();
private final static byte[] COMMAND_BYTE = COMMAND.getBytes();
private Telnet telnet = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private boolean connected = false;
private String hostname = "qqq";
//TelnetInputStream tis = null; NOT IN USE AS OF NOW
public TelnetTest() throws IOException, TelnetException {
// create new Telnet instance
telnet = new Telnet(hostname);
// register this class as TelnetListener
telnet.addTelnetListener(this);
// establish Telnet connection
telnet.connect();
connected = true;
output = telnet.getOutputStream();
// HERE IS WHERE I NEED HELP, NOT SURE HOW TO CHECK STREAM
String str = null;
if ((str = reader.readline()).equals("User name?:")) {
telnet.getOutputStream().write(USER_BYTE);
}
// SAME CHECK WOULD HAPPEN HERE FOR "Password"
telnet.getOutputStream().write(PWORD_BYTE);
// ANOTHER SIMILAR CHECK HERE
telnet.getOutputStream().write(COMMAND_BYTE);
// sends all data entered at console to Telnet server
String input = null;
while ((input = reader.readLine()) != null) {
if (connected) {
((TelnetOutputStream) output).println(input);
} else {
break;
}
}
}
public boolean streamContainsString(Reader reader, String searchString)
throws IOException {
Scanner streamScanner = new Scanner(reader);
if (streamScanner.findWithinHorizon(searchString, 0) != null) {
return true;
} else {
return false;
}
}
// Invoked when Telnet socked is connected.
public void connected(TelnetConnectedEvent event) {
System.out.println("Connected");
}
// Invoked when Telnet socket is disconnected. Disconnect can
public void disconnected(TelnetDisconnectedEvent event) {
connected = false;
System.out.print("Disconnected. Press enter key to quit.");
}
// Invoked when Telnet server requests that the Telnet client begin performing specified TelnetOption.
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
// Invoked when Telnet server offers to begin performing specified TelnetOption.
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
// Invoked when data is received from Telnet server.
public void dataReceived(TelnetDataReceivedEvent event) {
// print data recevied from Telnet server to console
System.out.print(event.getData());
}
public Telnet getTelnet() {
return telnet;
}
// starts console program
public static void main(String[] args) {
try {
// create BufferedReader to read data from console
reader = new BufferedReader(new InputStreamReader(System.in));
// create new TelnetExample instance
TelnetTest example = new TelnetTest();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
答案 0 :(得分:4)
如果您正在阅读/撰写字符串,则应始终使用Reader
和Writer
。 BufferedReader
允许您进行直线操作。因此BufferedReader
缠绕Reader
(InputStreamReader
左右)将允许您进行readLine()
调用以获取连接的输入线:
BufferedReader reader =
new BufferedReader(new InputStreamReader(telnet.getInputStream()));
要写入连接,您可以在Writer
附近使用OutputStreamWriter
:
Writer writer = new OutputStreamWriter(telnet.getOutputStream()));
我不确定这是否适用于来自Telnet
的流,但它适用于原始Socket
。然后你可以做类似下面的伪代码:
while (true) {
read a line from the server
some sort of if/then/else to test for the output
write your username/password or whatever is appropriate for the connection
repeat until some logout or IOException...
}
Apache Telnet
类有许多有趣的侦听器和其他处理程序,如果您愿意,可以使用它们,但学习曲线可能更多。这是使用TelnetClient
的一个很好的示例应用程序:
http://www.java2s.com/Code/Java/Network-Protocol/ExampleofuseofTelnetClient.htm