Remote Command Execution
Remote Command Execution
AIM:
To create a program for executing a command in the system from a remote point.
ALGORITHM:
CLIENT: 1. Start and import all the necessary packages. 2. Create a client side socket with local socket address. 3. Get the command from user and put it on output stream of socket. 4. Close the socket and stop. SERVER: 1. Start and import all the necessary packages. 2. Create a client and server socket and accept the incoming client request. 3. Using runtime , create runtime environment. 4. Using process execution to remote command and stop.
PROGRAM: CLIENT:
import java.io.*; import java.net.*; public class remclient {
public static void main(String args[])throws IOException { try { Socket s=new Socket("127.0.0.1",8081); PrintWriter out=new PrintWriter(s.getOutputStream(),true); String cmd; DataInputStream in=new DataInputStream(System.in); System.out.println("Enter the command to execute on server : "); cmd=in.readLine(); out.println(cmd); } catch(Exception e) { System.out.println(e); } } }
SERVER:
import java.io.*; import java.net.*; public class remserver { public static void main(String args[])throws IOException { ServerSocket ss=new ServerSocket(8081); Socket s=ss.accept(); String cmd; BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream())); cmd=in.readLine(); try { Runtime r=Runtime.getRuntime(); Process a=r.exec(cmd); System.out.println("Executing command : "+cmd); } catch(Exception e) {
System.out.println("Error"+e); } s.close(); } }
OUTPUT:
C:\jdk1.3\bin>javac remclient.java Note: remclient.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\jdk1.3\bin>javac remserver.java C:\jdk1.3\bin>java remserver
2nd C:\jdk1.3\bin>java remclient Enter the command to execute on server : notepad C:\jdk1.3\bin> 1st C:\jdk1.3\bin>java remserver Executing command : notepad C:\jdk1.3\bin>