1

我在linux终端中的数据包捕获命令..

sudo tcpdump -w kbh-ns.pcap -i lo greater 106 and less 106

当我按下时,进程将停止并保存捕获的数据包

^c

我有如下代码,它使用 BufferedReader 来完成

public void SaveCapture() throws IOException
{
    List<String> command1 = new ArrayList<String>();
    //perintah untuk mecari koneksi ( SIP DIP SPort DPort )
    command1.clear();
    command1.add("sudo"); command1.add("tcpdump");
    command1.add("-w"); command1.add("kbh-ns.pcap"); //write
    command1.add("-i"); command1.add("vmnet1"); //interface
    command1.add("greater");command1.add("106"); //packet length
    command1.add("and");
    command1.add("less"); command1.add("106");//packet length
    command1.add("-c"); command1.add("20");

    ProcessBuilder pb = new ProcessBuilder(command1);
    process = pb.start();
    ProcessBuilder PB = new ProcessBuilder(command1);
    Process TerminalTask = PB.start();

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

    String s;
    s = stdInput.readLine();
}

如果我使用它就可以了

command1.add("-c"); command1.add("5");//number of packets to capture

我的问题:如何使用 java 使用 ^c 命令停止进程?

如果我使用process.destroy()

private void ButtonStopScanActionPerformed(java.awt.event.ActionEvent evt) {                                               
    if (process != null)
    {
        process.destroy();
        TxtAreaKet.setText(TxtAreaKet.getText() + "Complete.. \n");
    }
    end = System.currentTimeMillis();
    try {
        ReadPacket();
    } catch (IOException ex) {
        Logger.getLogger(Receiver1.class.getName()).log(Level.SEVERE, null, ex);
    }
    TxtAreaKet.setText(TxtAreaKet.getText() + "waktu : "+ ((end - start) / 1000.0) + " ms");        
} 

private void ReadPacket() throws IOException {
    List<String> command1 = new ArrayList<String>();
    //perintah untuk mecari koneksi ( SIP DIP SPort DPort )
    command1.clear();
    command1.add("sudo"); command1.add("tcpdump");
    command1.add("-r"); command1.add("kbh-ns.pcap"); //read
    command1.add("-n"); 

    ProcessBuilder pb = new ProcessBuilder(command1);
    process2 = pb.start();
    ProcessBuilder PB = new ProcessBuilder(command1);
    Process TerminalTask = PB.start();

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(process2.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(process2.getErrorStream()));

    String s;
    long a = 0;
    while ((s = stdInput.readLine()) != null)
    {
        a++;
    }
    TxtAreaKet.setText(TxtAreaKet.getText() + "Captured Packet : " + a + "\n");
    while ((s = stdError.readLine()) != null)
    {
        TxtAreaKet.setText(TxtAreaKet.getText() + s + "\n");
    }
}

这并没有真正停止我的进程,因为..

在此处输入图像描述

当我Stop Scan再次按下

在此处输入图像描述

4

1 回答 1

0

java - 如何使用java使用^c命令停止进程?

只要打电话Process.destroy()

于 2013-03-01T04:03:50.613 回答