-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer
More file actions
84 lines (56 loc) · 2.96 KB
/
Server
File metadata and controls
84 lines (56 loc) · 2.96 KB
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
package compNetworksProject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
public class server {
public static ArrayList<String> getRuntimeInfo(String userInput) { // class to run runtime commands and return results
ArrayList<String> response = new ArrayList<>();
String s = null;
Process p;
try {
p = Runtime.getRuntime().exec(userInput);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = br.readLine()) != null) {
System.out.println(s);
response.add(s);
}
p.waitFor();
System.out.println ("exit: " + p.exitValue());
p.destroy();
} catch (Exception e) {}
return response;
} // end getRunTimeInfo()
public static void main(String[] args) throws IOException {
// Socket setup and accept
ServerSocket listener = new ServerSocket(4444);
System.out.println("[SERVER] Waiting for client connection...");
while (!listener.isClosed()) {
Socket client = listener.accept();
System.out.println("[SERVER] A connection has been established.");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
while (client.isConnected()) {
String request = in.readLine(); // (A) from clients out.println(userInput)
if (request.contains("1")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("date")}));
else if (request.contains("2")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("uptime")}));
else if (request.contains("3")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("free")}));
else if (request.contains("4")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("netstat")}));
else if (request.contains("5")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("who")}));
else if (request.contains("6")) out.println(Arrays.toString(new ArrayList[]{getRuntimeInfo("ps -ef")}));
else if (request.contains("7")) {
System.out.println("test");
out.println("Test");
}
else if (request.contains("exit")) {
System.out.println("exiting loop");
break;
}
}
}
} // end main
} // end server class