Kategorien: Snippets
Java – Telnetsession
Beitrags Datum:
Autor: Martin
Anzahl Kommentare: keine Kommentare
Example for a automated telnet-session. You need the org.apache.commons.net.telnet librabry!
public class TelnetServices implements Runnable, TelnetNotificationHandler {
private static TelnetServices INSTANCE = null;
static TelnetClient tc = null;
//connection data
private static String remoteip = "ip";
private static int remoteport = xx;
/**
* Just to make sure, nobody creates an instance
* @author m.schwarz
*/
private TelnetServices() {
//.......
}
/**
* get Instance of Class. If no instance exists, create new
* @return INSTANCE
*/
public static TelnetServices getInstance() {
if (INSTANCE == null) {
INSTANCE = new TelnetServices();
} return INSTANCE; }
/**
* connect to target and execute your commands
*/
public void executeCommands(List commands) throws Exception {
boolean endSession = false;
FileOutputStream fout = null;
try{
fout = new FileOutputStream ("spy.log", true);
} catch (IOException e) {
System.err.println( "Exception while opening the spy file: " + e.getMessage());
}
tc = 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 {
tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
} catch (InvalidTelnetOptionException e) {
System.err.println("Error registering option handlers: " + e.getMessage());
}
while (true) {
//disconnect
if (endSession) {
break;
}
boolean end_loop = false;
try {
tc.connect(remoteip, remoteport);
if (tc.isConnected()) {
//reads the commandline
Thread reader = new Thread (new TelnetServices());
tc.registerNotifHandler(new TelnetServices());
reader.start();
OutputStream outstr = tc.getOutputStream();
byte[] buff = new byte[1024];
int ret_read = 0;
int inputOrder = 0;
int NumberOfCommands = commands.size();
String scmd = null;
//command to send via telnet
do {
if (inputOrder == NumberOfCommands) {
//no more input
Thread.sleep(5000);
endSession = true;
reader.stop();
break;
} else {
Thread.sleep(2200);
//gives the server time to execute the last command
scmd = commands.get(inputOrder);
}
buff = getPreparedCommand(scmd);
ret_read = scmd.getBytes().length+2;
if (endSession) {
ret_read = 0;
end_loop = true;
}
if(ret_read > 0) {
if((new String(buff, 0, ret_read)).startsWith("AYT")) {
try {
System.out.println("Sending AYT");
System.out.println("AYT response:" + tc.sendAYT(5000));
} catch (IOException e) {
System.err.println("Exception waiting AYT response: " + e.getMessage());
}
} else if((new String(buff, 0, ret_read)).startsWith("OPT")) {
System.out.println("Status of options:");
for(int ii=0; ii 0) && (end_loop == false));
try { tc.disconnect();
System.out.println("-----------------> Telnetsession closed");
}catch (IOException e) {
System.err.println("Exception while connecting:" + e.getMessage()); }
}
} catch (IOException e) {
System.err.println("Exception while connecting:" + e.getMessage());
System.exit(1);
}
}
}
/** * Prepares the command to fit 1024 byte-Array */
private static byte[] getPreparedCommand(String s) {
byte[] bytecode = new byte[1024];
byte[] bString = s.getBytes();
int stringLength = bString.length;
for (int i=0; xxxxxx) {
String command = null;
if(negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
command = "DO";
} else if(negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
command = "DONT";
} else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
command = "WILL"; } else if(negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) { command = "WONT"; } System.out.println("Received " + command + " for option code " + option_code); }
/**
*
* Do not call! * This is only for the Reader!
*
* Reader thread. * Reads lines from the TelnetClient and echoes them * on the screen. *
**/
public void run() {
InputStream instr = tc.getInputStream();
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do { ret_read = instr.read(buff);
if(ret_read > 0) {
System.out.print(new String(buff, 0, ret_read));
}
} while (ret_read >= 0);
} catch (IOException e) {
System.err.println("Exception while reading socket:" + e.getMessage()); }
try { tc.disconnect();
System.out.println("tc is disconnected");
} catch (IOException e) {
System.err.println("Exception while closing telnet:" + e.getMessage());
}
}
/**
* returns the login and the command to delete screenshots in /tmp
*/
public static List getSpecialCommand() {
List cmds = new ArrayList();
cmds.add(0, "root");
cmds.add(1, "sh blabla.sh");
return cmds;
}
public void setRemoteIp(String ip) {
this.remoteip = ip;
}
public void setRemotePort(int port) {
this.remoteport = port;
}
}