tags:

views:

17

answers:

0

Hey guys,

I'm trying to use the telnetclient class in apache.commons.net and have run into a bit of a problem.

TelnetClient telnet = new TelnetClient();
terminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);
try
{
   telnet.addOptionHandler(ttopt);
   telnet.addOptionHandler(echoopt);
   telnet.addOptionHandler(gaopt);
}
catch (InvalidTelnetOptionException e)
{
   System.err.println("Error registering option handlers: " + e.getMessage());
}

telnet.connect(hostname,port);

InputStream in = telnet.getInputStream();
OutputStream out = telnet.getOutputStream();

String msg = command+"\r\n";
out.write(msg.getBytes());
out.flush();

My problem is this: I am unable to connect, I get an unknown host exception:

java.net.UnknownHostException:  [host]
    [java]     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:158)
    [java]     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
    [java]     at java.net.Socket.connect(Socket.java:546)
    [java]     at org.apache.commons.net.SocketClient.connect(SocketClient.java:176)

for most of the sites, but not all. All of the sites I'm able to telnet to manually. For the sites I am able to telnet to, my command always results in a "command not recognized" response. I've tried a using a printstream and out.print(msg), I've tried with and without the \r and the \n, but neither help. The command works just fine when I telnet from the terminal.

Any ideas of how to increase success connecting, and how to correctly write to the output stream? Thanks in advance!