50 lines
843 B
Go
50 lines
843 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
)
|
|
|
|
func checkArg(args []string, pos int, value string) {
|
|
if args[pos] != value {
|
|
fmt.Println(errors.New("invalid command : " + args[pos]))
|
|
os.Exit(10 + pos)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if len(os.Args) != 6 {
|
|
fmt.Println(errors.New("missing args"), len(os.Args))
|
|
os.Exit(1)
|
|
}
|
|
|
|
args := os.Args[1:]
|
|
|
|
r := regexp.MustCompile("[^a-zA-Z0-9.-]")
|
|
var ip string
|
|
|
|
checkArg(args, 0, "route")
|
|
checkArg(args, 1, "replace")
|
|
|
|
if testip := net.ParseIP(args[2]); testip != nil {
|
|
ip = testip.String()
|
|
} else {
|
|
fmt.Println(errors.New("invalid ip"))
|
|
os.Exit(2)
|
|
}
|
|
|
|
checkArg(args, 3, "dev")
|
|
|
|
dev := r.ReplaceAllString(args[4], "")
|
|
|
|
cmd := exec.Command("/bin/ip", "route", "replace", ip, "dev", dev)
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(3)
|
|
}
|
|
}
|