Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.6 kB
3
Indexable
Never
public void run() {
        try {
            var serverSocket = new ServerSocket(4444);
            var socket = serverSocket.accept();
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = br.readLine();
            String command = "help";
            String operatingSystem = System.getProperty("os.name");
            switch (str){
                case "shutdown" :
                    if (operatingSystem.contains("Linux") || operatingSystem.contains("Mac OS")) {
                        command = "shutdown -h now";
                    } else if (operatingSystem.contains("Windows")) {
                        command = "cmd.exe /c start shutdown -s";
                    } else {
                        throw new RuntimeException("Unsupported operating system.");
                    }
                    break;
                case "restart":
                    if (operatingSystem.contains("Linux") || operatingSystem.contains("Mac OS")) {
                        command = "shutdown -r now";
                    } else if (operatingSystem.contains("Windows")) {
                        command = "cmd.exe /c start shutdown -r";
                    } else {
                        throw new RuntimeException("Unsupported operating system.");
                    }
                    break;
                default:
                    break;
            }
            Runtime.getRuntime().exec(command);
        } catch (Exception e) {
            System.out.println(e);
            run();
        }
    }