Java 执行 ipconfig ,客户端-服务器

标签 java shell

我正在制作一个程序,您在客户端中键入 ifconfig(Linux) 或 ipconfig(Windows),然后它发送到服务器并在那里执行 shell。

执行正常,在服务器中我可以正确看到ipconfig,但我不知道如何将服务器的结果返回给客户端。

客户端:

public class ShellClient {  
public static void main(String[] args) throws IOException {
        String comand;
        Socket server =null;
        server = new Socket("localhost", 1234);
        while(true){
        System.out.println("Insert ipconfig(windows)/ ifconfig(Linux): ");
        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);

        comando = keyboard.next();

        try{
             System.out.println("Conecting...");
             DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
             DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

                out.writeUTF(comand);
                out.flush();
                String result = in.readUTF();
                System.out.println(result);


                if (comand == "0"){server.close();
                System.out.println("Finish. Thank You!");
                System.exit(0);}

        }catch(IOException ioe){
            System.err.println(ioe);
        }
      } 
    }
  }

服务器端:

public class ShellServer {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        String result;
        String comand ;
        String s = null;
        socket = new ServerSocket(1234);  

        System.out.println("The server keep working...");
        client = socket.accept();
        System.out.println("The client is connected");
        DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));

            comand = in.readUTF();
            result = Shell.CheckCommand(comand);
            //out.writeUTF(result);
            //out.flush();

         // Ejcutamos el comando
            Process p = Runtime.getRuntime().exec(result);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                            p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
        }

            if(comand == "0"){client.close();
                         socket.close();}
            }
        }

提前谢谢你们!

最佳答案

如果您不想使用任何威胁,那么您需要为每个请求建立一个新连接,并且每次只能处理一个连接

试试这个

客户端

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;

public class ShellClient {

    public static void main(String[] args) throws IOException {
        String comando;

        while (true) {
            System.out.println("Introduce ipconfig(windows)/ ifconfig(Linux): ");
            @SuppressWarnings("resource")
            Scanner keyboard = new Scanner(System.in);

            comando = keyboard.next();

            System.out.println("Conectando...");
            // We need a new connection for every request
            Socket server = new Socket("localhost", 1234);
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
            DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));

            out.writeUTF(comando);
            out.flush();
            // We close the output stream so the server knows we have finished the request
            server.shutdownOutput();
            if ("0".equals(comando)) {
                System.out.println("Finalizado. Gracias!");
                System.exit(0);
            }
            BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream(), "UTF-8"));
            String line;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            // See above - we need a new connection for every request
            server.close();
        }
    }
}

服务器

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ShellServer {

    public static void main(String[] args) throws IOException {
        ServerSocket socket = null;
        Socket client = null;
        String resultado;
        String comando;
        String s = null;
        socket = new ServerSocket(1234);

        System.out.println("El servidor sigue funcionando...");
        while (true) {
            client = socket.accept();
            System.out.println("El cliente se ha conectado");
            DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));

            comando = in.readUTF();
            if ("0".equals(comando)) {
                System.exit(0);
            }
            resultado = Shell.CheckCommand(comando);

            // Ejcutamos el comando
            Process p = Runtime.getRuntime().exec(comando);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));

            while ((s = stdInput.readLine()) != null) {
                out.write(s.getBytes("UTF-8"));
                out.write('\n');
                // we want to see each line as fast as we can
                out.flush();
                System.out.println(s);
            }
            client.close();            
        }
    }
}

关于Java 执行 ipconfig ,客户端-服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23324009/

相关文章:

c - Unix C Shell - 作业控制问题!

java - ServletContext.getRealPath ("pictures") 返回 tmp 目录??老板

java - 设置 CriteriaBuilder

java - 包 org.apache.commons.io 不存在错误

linux - 在 bash 脚本中连接输入

linux - 从文件中删除带有该字符的行

java - IOException : Could not initialize SSL context - Apache Commons Net - Android

java - 忽略归因并比较集合

shell - 无法通过管道将 docker pid 传递给 bash 命令

java - 命令在终端中有效,但不适用于 Runtime.exec