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
|
public class telnet extends HttpServlet {
public static Socket connection;
public static OutputStream out;
public static InputStream in;
public static byte [] b = new byte[] {(byte)10,(byte)247,(byte)126,(byte)244};
public void socketConnect(byte [] b, int intPort){
try {
//create new socket
connection = new Socket( InetAddress.getByAddress( b ), intPort );
} catch (UnknownHostException uhe ) {
System.exit(1);
}
catch (IOException ioe ) {
System.out.println("error : " + ioe);
System.exit(1);
}
}
//This void will create IO streams (required to send data through your socket):
public void createIOStreams(){
try{
out = connection.getOutputStream();
in = connection.getInputStream();
} catch (IOException ioe ) {
System.exit(1);
}
}/*
public void disconnect() {
try {
telnet.disconnect();
}
catch( Exception e ) {
e.printStackTrace();
}
}*/
//Reading from the server:
public void readReply() throws ClassNotFoundException, IOException{
try{
byte[] buffer = new byte[4096];
int bytes_read;
// Read server response and determine response length
bytes_read = in.read(buffer);
//Convert response to string
String strResponse = new String(buffer,0,bytes_read);
System.out.println(strResponse);
}
catch(Exception e){}
}
//Sending to server:
public void sendMessage(String strInstruction){
// send strInstruction to server
try{
// Convert string to array of type byte
byte[] buffer = strInstruction.getBytes();
// Send to server
out.write(buffer);
} catch (IOException ioe ) {
System.exit(1);}//end catch
//end fun
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("t");
telnet t1=new telnet();
t1.socketConnect(b,23);
t1.sendMessage("log");
t1.sendMessage("pass");
t1.sendMessage("display current-configuration");
t1.createIOStreams();
t1.readReply();
}
catch( Exception e )
{
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
} |
Partager