Remote Command Execution

rceServer

import java.io.*;

import java.net.*;

public class rceserver

{

public static void main(String a[])throws IOException

{

ServerSocket s = new ServerSocket(8001);

System.out.println(“Started: “+ s);

try

{

Socket scr = s.accept();

try

{

System.out.println(“Connection Accepted” + scr);

BufferedReader in = new BufferedReader(new

InputStreamReader(scr.getInputStream()));

PrintWriter out = new PrintWriter(new BufferedWriter(new

OutputStreamWriter(scr.getOutputStream())),true);

String str= in.readLine();

out.println(str);

Process p = Runtime.getRuntime().exec(str);

}

finally

{

System.out.println(“Closing..”);

scr.close();

}

}

finally

{

s.close();

}

}

}


rceClient

import java.io.*;

import java.net.*;

public class rceclient

{

public static void main(String a[]) throws IOException

{

InetAddress adr = InetAddress.getByName(“localhost”);

System.out.println(“Address=” + adr);

Socket scr = new Socket(adr, 8001);

try

{

System.out.println(“Server =”+scr);

BufferedReader in = new BufferedReader(new

InputStreamReader(System.in));

PrintWriter out = new PrintWriter(new BufferedWriter(new

OutputStreamWriter(scr.getOutputStream())),true);

System.out.println(“Enter the command to be executed remotely”);

String str = in.readLine();

Process p = Runtime.getRuntime().exec(str);

System.out.println(str);

out.println(str);

}

finally

{

System.out.println(“Closing…”);

scr.close();

}

}

}

Advertisements