mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-15 21:32:17 +08:00
47 lines
945 B
Go
47 lines
945 B
Go
package ncutils
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"math/rand"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
// Runs Commands for FreeBSD
|
|
func RunCmd(command string, printerr bool) (string, error) {
|
|
args := strings.Fields(command)
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
go func() {
|
|
<-ctx.Done()
|
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
|
}()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil && printerr {
|
|
log.Println("error running command:", command)
|
|
log.Println(strings.TrimSuffix(string(out), "\n"))
|
|
}
|
|
return string(out), err
|
|
}
|