Last active
March 29, 2017 17:32
-
-
Save jaytaylor/f38147e3f1d331dc5d1f2db1c11fdbf6 to your computer and use it in GitHub Desktop.
Running Wireshark in Docker Containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// This is a cleaned up version of the code found in: | |
// | |
// https://medium.com/@chughtapan/running-wireshark-on-docker-ac90ebc4907b | |
// | |
// Added: | |
// - Container ID passed as command-line argument | |
// - Error handling | |
import ( | |
"fmt" | |
"net" | |
"os" | |
"os/exec" | |
"runtime" | |
"github.com/vishvananda/netns" | |
) | |
func main() { | |
if len(os.Args) == 1 { | |
fmt.Fprintln(os.Stderr, "error: missing required arg: container-id") | |
os.Exit(1) | |
} | |
// Lock the OS Thread so we don’t accidentally switch namespaces. | |
runtime.LockOSThread() | |
defer runtime.UnlockOSThread() | |
// Save the current network namespace. | |
origns, err := netns.Get() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error getting current network namespace: %s\n", err) | |
os.Exit(1) | |
} | |
defer origns.Close() | |
// Get the network namespace based on container id. | |
newns, err := netns.GetFromDocker(os.Args[1]) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error getting network namespace from docker: %s\n", err) | |
os.Exit(1) | |
} | |
defer newns.Close() | |
netns.Set(newns) | |
// Do something with the network namespace. | |
ifaces, err := net.Interfaces() | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error getting network interfaces: %s\n", err) | |
os.Exit(1) | |
} | |
fmt.Printf("interfaces: %v\n", ifaces) | |
cmd := exec.Command("tshark", "-z", "conv,ip", "-i", "eth0") | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
cmd.Stdin = os.Stdin | |
if err := cmd.Run(); err != nil { | |
fmt.Fprintf(os.Stderr, "error running cmd: %s\n", err) | |
os.Exit(1) | |
} | |
// Return to original namespace. | |
netns.Set(origns) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment