Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

How to Execute tcpdump Linux Command using Java Process Class and Capture TCP/IP Packets

$
0
0

Lets take a look at a problem in which you may want to capture Tcpdump output in your Java Program. You may have There are N number of possibilities you may need to capture Tcpdump to analyse customer data.

Tcpdump is very basic command and everybody I believed may have used it by one or other way. Tcpdump is a common packet analyzer that runs under the command line. It allows the user to intercept and display TCP/IP and other packets being transmitted or received over a network.

Tcpdump needs root user permission and you may not be able to run it using Eclipse IDE. In this tutorial we will write simple steps to capture Tcpdump output in Java and we will run it on Mac OS X.

Let’s start.

  • Create Java file: CrunchifyExecuteTCPDUMP.java
  • Command we will use: /usr/sbin/tcpdump -c 2 -v -A dst port 80
  • -c option: Exit after receiving 2 packets
  • -v option: verbose output
  • -A dst option: Print each packet in ASCII
  • dst port PortNumber: True if the IP destination field of the packet is host, which may be either an address or a name

This program will also help you to run any other linux / windows / mac terminal commands.

  • How to run external programs by using Java ProcessBuilder class?
  • tcpdump: Using TCPDUMP from Java
  • How to Run Tcpdump From Java
  • Java exec – execute system processes with Java
  • java + tcpdump = problem – let’s solve it

We are using java.lang.ProcessBuilder class which is required to create operating system processes. This class is not synchronized.

package com.crunchify.tutorial;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyExecuteTCPDUMP {
	public static void main(String[] args) {
		String tcpDumpCmd = "/usr/sbin/tcpdump -c 2 -v -A dst port 80";
		String tcpDumpResult = runTCPDUmp(tcpDumpCmd, true);
		System.out.println(tcpDumpResult);
	}

	public static String runTCPDUmp(String crunchifyCmd, boolean waitForResult) {
		System.out.println("inside runTCPDUmp()");
		String tcpdumpCmdResponse = "";
		ProcessBuilder crunchifyProcessBuilder = null;

		// Find OS running on VM
		String operatingSystem = System.getProperty("os.name");

		if (operatingSystem.toLowerCase().contains("window")) {
			// In case of windows run command using "crunchifyCmd"
			crunchifyProcessBuilder = new ProcessBuilder("cmd", "/c", crunchifyCmd);
		} else {
			// In case of Linux/Ubuntu run command using /bin/bash
			crunchifyProcessBuilder = new ProcessBuilder("/bin/bash", "-c", crunchifyCmd);
		}

		crunchifyProcessBuilder.redirectErrorStream(true);

		try {
			Process process = crunchifyProcessBuilder.start();
			if (waitForResult) {
				InputStream crunchifyStream = process.getInputStream();
				tcpdumpCmdResponse = getStringFromStream(crunchifyStream);
				crunchifyStream.close();
			}

		} catch (Exception e) {
			System.out.println("Error Executing tcpdump command" + e);
		}
		return tcpdumpCmdResponse;
	}

	private static String getStringFromStream(InputStream crunchifyStream) throws IOException {
		System.out.println("inside getStringFromStream()");
		if (crunchifyStream != null) {
			Writer crunchifyWriter = new StringWriter();

			char[] crunchifyBuffer = new char[2048];
			try {
				Reader crunchifyReader = new BufferedReader(new InputStreamReader(crunchifyStream, "UTF-8"));
				int count;
				while ((count = crunchifyReader.read(crunchifyBuffer)) != -1) {
					crunchifyWriter.write(crunchifyBuffer, 0, count);
				}
			} finally {
				crunchifyStream.close();
			}
			return crunchifyWriter.toString();
		} else {
			return "";
		}
	}
}

As you wont be execute it via Eclipse, we will run from Mac Terminal. Eclipse will give you this error message.

TCPDUMP in Eclipse Permission Denied How to Execute tcpdump Linux Command using Java Process Class and Capture TCP/IP Packets

Now let’s run it from Command Prompt / Mac Terminal. Create file CrunchifyExecuteTCPDUMP.java and save it under ~/Documents and run below commands.

bash-3.2# javac CrunchifyExecuteTCPDUMP.java 
bash-3.2# java CrunchifyExecuteTCPDUMP

Run TCPDUMP command from Mac Terminal Crunchify How to Execute tcpdump Linux Command using Java Process Class and Capture TCP/IP Packets

Have anything to add to this article? Please chime in and join the conversion.


The post How to Execute tcpdump Linux Command using Java Process Class and Capture TCP/IP Packets appeared first on Crunchify.
Author: Arpit Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles