Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hkparker
Last active June 5, 2016 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkparker/c18c35efe5ac14751d86bc47a59741cc to your computer and use it in GitHub Desktop.
Save hkparker/c18c35efe5ac14751d86bc47a59741cc to your computer and use it in GitHub Desktop.
Setup xen vif bridges at boot for IDS
package main
import (
"bufio"
"fmt"
"os/exec"
"regexp"
"strings"
)
func main() {
suffixes := map[string]int{
" Bridge \"xenbr7\"": 7,
" Bridge \"xenbr8\"": 8,
" Bridge \"xenbr9\"": 9,
}
iface_numbers := make(chan int, 3)
vifs := make(chan string, 3)
vif_line := regexp.MustCompile(`vif[0-9]+.[0-9]`)
output, _ := exec.Command("ovs-vsctl", []string{"show"}...).Output()
scanner := bufio.NewScanner(strings.NewReader(string(output)))
grabbing := false
for scanner.Scan() {
line := scanner.Text()
if number, ok := suffixes[line]; ok {
iface_numbers <- number
grabbing = true
} else if vif := vif_line.FindString(line); vif != "" {
if !grabbing {
continue
}
vifs <- vif
grabbing = false
}
}
for i := 0; i < 3; i++ {
iface := <-iface_numbers
vif := <-vifs
args := strings.Fields(fmt.Sprintf(
"-- set Bridge xenbr%d mirrors=@m -- --id=@eth%d get Port eth%d -- --id=@%s get Port %s -- --id=@m create Mirror name=eth%d-mirror select-dst-port=@eth%d select-src-port=@eth%d output-port=@%s",
iface,
iface,
iface,
vif,
vif,
iface,
iface,
iface,
vif,
))
out, _ := exec.Command("ovs-vsctl", args...).Output()
fmt.Print(string(out))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment