Difference between revisions of "Golang exec.command"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
     "os/exec"
 
     "os/exec"
 
     "os"
 
     "os"
     //"strings"
+
     "strings"
    "runtime"
+
"runtime"
 +
"regexp"
 +
//match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
 
)
 
)
 +
 +
 +
func cmd_filter(name string, match_list []string){
 +
allow := false
 +
for k, v := range match_list {
 +
fmt.Println("At index", k, "value is", v)
 +
match, _ := regexp.MatchString(v, name)
 +
if match == true {
 +
allow = true
 +
break
 +
}
 +
    }
 +
    if allow != true {
 +
fmt.Printf("E: Command %v is %v in shellcmd not allowed\n", name, allow)
 +
os.Exit(1)
 +
}
 +
}
  
  
 
func shell_cmd(name string, args []string) {
 
func shell_cmd(name string, args []string) {
cmd := exec.Command(name, args...)
+
    cmd := exec.Command(name, args...)
out, err := cmd.Output()
+
    out, err := cmd.Output()
if err != nil {
+
    if err != nil {
  fmt.Println(err)
+
        fmt.Println(err)
}
+
    }
fmt.Println(string(out))
+
fmt.Printf(string(out))
 
}
 
}
  
  
 
func shellcmd() {
 
func shellcmd() {
system_os := runtime.GOOS
+
    system_os := runtime.GOOS
if len(os.Args) < 3 {
+
    if system_os == "linux" {
fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
+
        name := os.Args[2]
fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
+
args := os.Args[3:]
os.Exit(3)
+
match_list := []string{"Get", "Fet"}
}
+
cmd_filter(name, match_list)
 
+
        shell_cmd(name, args)
if system_os == "linux" {
+
    }else if system_os == "windows" {
name := os.Args[1]
+
        if len(os.Args) < 3 {
args := os.Args[2:]
+
            fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
shell_cmd(name, args)
+
            fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
}else if system_os == "windows" {
+
            os.Exit(3)
 +
        }
 
var name string = "powershell.exe"
 
var name string = "powershell.exe"
 +
match_list := []string{"Get", "Fet"}
 +
                // match_list := []string{".*", "Get", "Fet"}
 
args := os.Args[2:]
 
args := os.Args[2:]
shell_cmd(name, args)
+
s := os.Args[2]
}else{
+
fmt.Println(string(s))
  fmt.Printf("OS %v not supported.\n", system_os)
+
cmd_filter(s, match_list)
}
+
        shell_cmd(name, args)
}
+
    }else{
 
+
        fmt.Printf("OS %v not supported.\n", system_os)
 
+
    }
func main() {
 
if len(os.Args) < 2 {
 
fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
 
fmt.Printf("Example: %v shellcmd ls -lhat\n", os.Args[0])
 
os.Exit(3)
 
  }
 
  function := os.Args[1]
 
if function == "shellcmd" {
 
shellcmd()
 
}else{
 
fmt.Printf("E: Unsupported function %v\n", function)
 
}
 
}
 
```
 
 
 
Windows and Linux 2
 
```
 
package main
 
 
 
import (
 
    "fmt"
 
    "os/exec"
 
    "os"
 
    //"strings"
 
    "runtime"
 
)
 
 
 
  
func linux_cmd() {
 
  if len(os.Args) < 2 {
 
    fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
 
    fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
 
    os.Exit(3)
 
  }
 
  name := os.Args[1]
 
  args := os.Args[2:]
 
  cmd := exec.Command(name, args...)
 
  out, err := cmd.Output()
 
  if err != nil {
 
    fmt.Println(err)
 
  }
 
  fmt.Println(string(out))
 
 
}
 
}
  
 
func win_cmd() {
 
  if len(os.Args) < 2 {
 
    fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
 
    fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
 
    os.Exit(3)
 
  }
 
  // name := os.Args[1]
 
  name := "powershell.exe"
 
  args := os.Args[1:]
 
  cmd := exec.Command(name, args...)
 
  out, err := cmd.Output()
 
  if err != nil {
 
    fmt.Println(err)
 
  }
 
  fmt.Println(string(out))
 
}
 
  
 
func main() {
 
func main() {
  os := runtime.GOOS
+
    if len(os.Args) < 2 {
  //var os string = runtime.GOOS
+
        fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
  if os == "linux" {
+
        fmt.Printf("Example: %v shellcmd ls -lhat\n", os.Args[0])
    linux_cmd()
+
        os.Exit(3)
  }else if os == "windows" {
+
    }
fmt.Printf("OS %v is supported.", os)
+
    function := os.Args[1]
win_cmd()
+
    if function == "shellcmd" {
  }else{
+
        shellcmd()
  fmt.Printf("OS %v not supported.", os)
+
    }else{
  }
+
        fmt.Printf("E: Unsupported function %v\n", function)
 +
    }
 
}
 
}
 
```
 
```
 
  
  

Latest revision as of 04:49, 16 November 2020

Windows and Linux

package main

import (
    "fmt"
    "os/exec"
    "os"
    "strings"
    "runtime"
    "regexp"
    //match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
)


func cmd_filter(name string, match_list []string){
    allow := false
    for k, v := range match_list {
        fmt.Println("At index", k, "value is", v)
        match, _ := regexp.MatchString(v, name)
        if match == true {
            allow = true
            break
        }
    } 
    if allow != true {
        fmt.Printf("E: Command %v is %v in shellcmd not allowed\n", name, allow)
        os.Exit(1)
    }
}


func shell_cmd(name string, args []string) {
    cmd := exec.Command(name, args...)
    out, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf(string(out))
}


func shellcmd() {
    system_os := runtime.GOOS
    if system_os == "linux" {
        name := os.Args[2]
        args := os.Args[3:]
        match_list := []string{"Get", "Fet"}
        cmd_filter(name, match_list)
        shell_cmd(name, args)
    }else if system_os == "windows" {
        if len(os.Args) < 3 {
            fmt.Printf("Usage: %v <cmd> <args>\n", os.Args[0])
            fmt.Printf("Example: %v ls -lhat\n", os.Args[0])
            os.Exit(3)
        }
        var name string = "powershell.exe"
        match_list := []string{"Get", "Fet"}
                // match_list := []string{".*", "Get", "Fet"}
        args := os.Args[2:]
        s := os.Args[2]
        fmt.Println(string(s))
        cmd_filter(s, match_list)
        shell_cmd(name, args)
    }else{
        fmt.Printf("OS %v not supported.\n", system_os)
    }

}


func main() {
    if len(os.Args) < 2 {
        fmt.Printf("Usage: %v <fuction>\n", os.Args[0])
        fmt.Printf("Example: %v shellcmd ls -lhat\n", os.Args[0])
        os.Exit(3)
    }
    function := os.Args[1]
    if function == "shellcmd" {
        shellcmd()
    }else{
        fmt.Printf("E: Unsupported function %v\n", function)
    }
}

Windows

package main

import(
    "fmt"
    "os/exec"
    "os"
    "log"
)


func pscmd(s string){
    args := []string{s}
    // cmd := exec.Command("powershell.exe", "ls", "-Depth", "1")
    cmd := exec.Command("powershell.exe", args...)

    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        log.Fatalf("cmd.Run() failed with %s\n", err)
    }
    fmt.Println("======")
}

func main(){   

    pscmd("ls -Depth 1")
    pscmd("echo 'hi there'")

}