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
| package telnetapp;
import org.apache.commons.net.telnet.TelnetNotificationHandler;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetNotificationHandler;
import org.apache.commons.net.telnet.SimpleOptionHandler;
import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import java.util.StringTokenizer;
import org.apache.commons.net.telnet.TelnetCommand;
/**
*
* @author sanfour
*/
public class Telnet implements TelnetNotificationHandler, Runnable
{
InputStream in;
OutputStream out;
String s="";
public Telnet()
{
try
{
TelnetClient tc = new TelnetClient();
tc.connect("172.16.100.67", 23);
in = tc.getInputStream();
out = tc.getOutputStream();
} catch (SocketException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void receivedNegotiation(int arg0, int arg1) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void run()
{
char a;
while(true)
{
try {
a = (char) in.read();
System.out.print(a);
s += a;
if (String.valueOf(a).equals("\n")) {
s = "";
}
if (s.contains("login:")) {
s = "";
//System.out.println("log");
out.write("sanfour\n".getBytes());
out.flush();
}
if (s.contains("Password:")) {
s = "";
//System.out.println("log");
out.write("onmyhome\n".getBytes());
out.flush();
}
} catch (IOException ex) {
Logger.getLogger(Telnet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} |
Partager