我正在尝试使用来自 JAVA 的 tshark 捕获数据包。[请在下面找到]
如果我尝试从终端(Ubuntu)的命令它的工作。但是从JAVA它的抛出错误tshark:
无效的 -o 标志 "column.format:""source","
我尝试了以下替代方法[错误消息相同]
String[] cmdArray = {"tshark -i any -o column.format:\"source, %s, srcport, %uS\" -f \"port 80 or port 443\""};
和这个:
String[] cmdArray = {"tshark -i any -o column.format:'source, %s, srcport, %uS' -f 'port 80 or port 443'"};
//CommandExc.java
import java.io.*;
import java.util.*;
public class CommandExc
{
public static void main (String args[])
{
//This worked Succcessfully
//String[] cmdArray = {"tshark -D","sudo tshark -D"};
String[] cmdArray = {"tshark -i any -o column.format:\"\"source\", \"%s\",\"srcport\", \"%uS\"\" -f \"port 80 or port 443\""};
for (String cmd: cmdArray)
{
System.out.println("Executing command : "+cmd);
try
{
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String str = in.readLine();
while(str != null)
{
System.out.println(str);
// res += str + "\n";
str = in.readLine();
}
in.close();
BufferedReader in2 = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String str1 = in2.readLine();
while(str1 != null)
{
System.out.println("Inside getErrorStream "+str1);
// res += str + "\n";
str1 = in2.readLine();
}
in2.close();
proc.waitFor();
System.out.println("Exit value is : "+proc.exitValue());
proc.destroy();
}
catch(IOException | InterruptedException e)
{
System.out.println("Exception on CommandExc class :" + e.toString());
e.printStackTrace();
}
} //end of for
}
}