add external ip route command

This commit is contained in:
Xavier Henner 2019-07-11 08:15:26 +02:00
parent 1c02c700fa
commit 24406ca0f4
2 changed files with 50 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
test.sh test.sh
openvpn-dm-mgt-server openvpn-dm-mgt-server
openvpn-dm-mgt-server.conf openvpn-dm-mgt-server.conf
iproute/iproute

49
iproute/main.go Normal file
View File

@ -0,0 +1,49 @@
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)
}
}