This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

Issue with tshark -o

0

Hai,

I am trying to capture packets using tshark from JAVA (OS: Ubuntu).

From terminal i can run below command successfully. tshark -i any -o column.format:""source", "%s", "srcport", "%uS"" -f "port 80 or port 443"

But from JAVA its throwing below message tshark: Invalid -o flag "column.format:""source","

But i can invoke commands like "tshark -i any" from java with out any issues. Is there any other thing i need to correct to get the exact output from JAVA. Please Help...

asked 22 Jan '13, 22:59

Krishnaprasad's gravatar image

Krishnaprasad
16114
accept rate: 0%


2 Answers:

0

The problem why your Java application reports an invalid -o flag is because the version of the Runtime.exec method is responsible for splitting the supplied string into separate command line arguments. By default, this method uses the space, tab, newline, carriage-return and the form-feed characters. So as you can see, because there is a white space character between [column.format:\"\"source\",] [\"%s\",], this version of the exec method treats them as 2 distinct arguments.

The solution will be to use the other version of the Runtime.exec method which takes the system command and arguments in as an array parameter. You can therefore manually split out the command line arguments and define them as separate strings in the array parameter as follows:

Process proc = Runtime.getRuntime().exec(new String[] { "tshark", "-i", "any", "-o", "column.format:\"source\", \"%s\", \"srcport\", \"%uS\"", "-f", "port 80 or port 443"});

answered 24 Jan '13, 01:29

Jeff%20Moszuti's gravatar image

Jeff Moszuti
361
accept rate: 100%

Excellent, It worked..... Thanks a ton :)

(24 Jan '13, 04:27) Krishnaprasad

Its not related to tshark but if some one can help me with this issue that will be great...

http://stackoverflow.com/questions/14680942/how-to-pass-the-terminal-output-to-java-text-area-while-its-apperaing-in-termina

(03 Feb '13, 21:40) Krishnaprasad

0

But from JAVA its throwing below message tshark: Invalid -o flag "column.format:""source","

That's a problem with the quotes. You need to escape them with \.

.exec("tshark -i any -o column.format:\"\" ...

Regards
Kurt

answered 22 Jan '13, 23:20

Kurt%20Knochner's gravatar image

Kurt Knochner ♦
24.8k1039237
accept rate: 15%

edited 22 Jan '13, 23:56

@Kurt

Thanks for your reply.

I did the same here is the complete code, its not working. If i tried the same from terminal its working..

====

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</code></pre><p>====</p><p>Thanks</p></div><div id="comment-17885-info" class="comment-info"><span class="comment-age">(22 Jan '13, 23:35)</span> <span class="comment-user userinfo">Krishnaprasad</span></div></div><span id="17889"></span><div id="comment-17889" class="comment"><div id="post-17889-score" class="comment-score"></div><div class="comment-text"><p>O.K. So do you get the same <strong>error</strong> now, or can't you read the output of the command?</p><p>If it's the same error, please try this:</p><p><code>String[] cmdArray = {"tshark -i any -o column.format:\"source, %s, srcport, %uS\" -f \"port 80 or port 443\""};</code><br />

or this:

String[] cmdArray = {"tshark -i any -o column.format:'source, %s, srcport, %uS' -f 'port 80 or port 443'"};

(22 Jan '13, 23:45) Kurt Knochner ♦

Hai Kurt,

No Luck I tried both i.e

String[] cmdArray = {"tshark -i any -o column.format:\"source, %s, srcport, %uS\" -f \"port 80 or port 443\""};

AND this:

String[] cmdArray = {"tshark -i any -o column.format:'source, %s, srcport, %uS' -f 'port 80 or port 443'"};

== Message

[email protected]:~/Desktop/java/pgms/cmd$ javac CommandExc.java [email protected]:~/Desktop/java/pgms/cmd$ java CommandExc

Executing command : tshark -i any -o column.format:"source, %s, srcport, %uS" -f "port 80 or port 443"

Inside getErrorStream tshark: Invalid -o flag "column.format:"source,"

Exit value is : 1

[email protected]:~/Desktop/java/pgms/cmd$ javac CommandExc.java [email protected]:~/Desktop/java/pgms/cmd$ java CommandExc

Executing command : tshark -i any -o column.format:'source, %s, srcport, %uS' -f 'port 80 or port 443'

Inside getErrorStream tshark: Invalid -o flag "column.format:'source,"

Exit value is : 1

[email protected]:~/Desktop/java/pgms/cmd$

(23 Jan ‘13, 00:48) Krishnaprasad