Zum Inhalt springen
Kategorien:

Java – Telnetsession

Beitrags Datum:
Autor:
Anzahl Kommentare: keine Kommentare

n

nExample for a automated telnet-session. You need the org.apache.commons.net.telnet librabry!nnn

public class TelnetServices implements Runnable, TelnetNotificationHandler {nnprivate static TelnetServices INSTANCE = null;nstatic TelnetClient tc = null;n//connection datanprivate static String remoteip = "ip";nprivate static int remoteport = xx;nn/**n* Just to make sure, nobody creates an instancen* @author m.schwarzn*/nprivate TelnetServices() {n    //.......n}n/**n* get Instance of Class. If no instance exists, create newn* @return INSTANCEn*/npublic static TelnetServices getInstance() {n    if (INSTANCE == null) {n        INSTANCE = new TelnetServices();n    } return INSTANCE; }nn/**n* connect to target and execute your commandsn*/npublic void executeCommands(List commands) throws Exception {nnboolean endSession = false;nFileOutputStream fout = null;ntry{n    fout = new FileOutputStream ("spy.log", true);n} catch (IOException e) {n    System.err.println( "Exception while opening the spy file: " + e.getMessage());n}ntc = new TelnetClient();nTerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler("VT100", false, false, true, false);nEchoOptionHandler echoopt = new EchoOptionHandler(true, false, true, false);nSuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true);ntry {n    tc.addOptionHandler(ttopt);n    tc.addOptionHandler(echoopt);n    tc.addOptionHandler(gaopt);n} catch (InvalidTelnetOptionException e) {n    System.err.println("Error registering option handlers: " + e.getMessage());n}nwhile (true) {n//disconnectnif (endSession) {n    break;n}nboolean end_loop = false;ntry {n    tc.connect(remoteip, remoteport);n    if (tc.isConnected()) {n		//reads the commandlinen		Thread reader = new Thread (new TelnetServices());n		tc.registerNotifHandler(new TelnetServices());n		reader.start();n		OutputStream outstr = tc.getOutputStream();n		byte[] buff = new byte[1024];n		int ret_read = 0;n		int inputOrder = 0;n		int NumberOfCommands = commands.size();n		String scmd = null;n        //command to send via telnetn        do {n            if (inputOrder == NumberOfCommands) {n                //no more inputn                Thread.sleep(5000);n                endSession = true;n                reader.stop();n                break;n            } else {n                Thread.sleep(2200);n                //gives the server time to execute the last commandn                scmd = commands.get(inputOrder);n            }n            buff = getPreparedCommand(scmd);n            ret_read = scmd.getBytes().length+2;n            if (endSession) {n                ret_read = 0;n                end_loop = true;n            }n            if(ret_read > 0) {n                if((new String(buff, 0, ret_read)).startsWith("AYT")) {n                    try {n                        System.out.println("Sending AYT");n                        System.out.println("AYT response:" + tc.sendAYT(5000));n                    } catch (IOException e) {n                        System.err.println("Exception waiting AYT response: " + e.getMessage());n                    }n                } else if((new String(buff, 0, ret_read)).startsWith("OPT")) {n                    System.out.println("Status of options:");n                    for(int ii=0; ii 0) && (end_loop == false));n                    try { tc.disconnect();n                        System.out.println("-----------------> Telnetsession closed");n                    }catch (IOException e) {n                    System.err.println("Exception while connecting:" + e.getMessage()); }n                }n            } catch (IOException e) {n                System.err.println("Exception while connecting:" + e.getMessage());n                System.exit(1);n            }n        }n    }nn    /** * Prepares the command to fit 1024 byte-Array */n    private static byte[] getPreparedCommand(String s) {n    byte[] bytecode = new byte[1024];n    byte[] bString = s.getBytes();n    int stringLength = bString.length;n    for (int i=0; xxxxxx) {n        String command = null;n        if(negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {n            command = "DO";n        } else if(negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {n            command = "DONT";n        } else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {n            command = "WILL"; } else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) { command = "WONT"; } System.out.println("Received " + command + " for option code " + option_code); }nn/**n*n* Do not call! * This is only for the Reader!n*n* Reader thread. * Reads lines from the TelnetClient and echoes them * on the screen. *n**/npublic void run() {n    InputStream instr = tc.getInputStream();n    try {n        byte[] buff = new byte[1024];n        int ret_read = 0;n        do { ret_read = instr.read(buff);n                if(ret_read > 0) {n                    System.out.print(new String(buff, 0, ret_read));n                }n        } while (ret_read >= 0);n    } catch (IOException e) {n            System.err.println("Exception while reading socket:" + e.getMessage()); }n            try { tc.disconnect();n                System.out.println("tc is disconnected");n            } catch (IOException e) {n                System.err.println("Exception while closing telnet:" + e.getMessage());n            }n}nn/**n* returns the login and the command to delete screenshots in /tmpn*/npublic static List getSpecialCommand() {nList cmds = new ArrayList();ncmds.add(0, "root");ncmds.add(1, "sh blabla.sh");nreturn cmds;n}nnpublic void setRemoteIp(String ip) {nthis.remoteip = ip;n}nnpublic void setRemotePort(int port) {nthis.remoteport = port;n}nn}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert