I am currently making a golang program which is supposed to be able to run but for some reason even though I enter the same options which work (eg. -oN test.txt
) in the cmd, it doesn't run and gives me this error message This is odd because in the command line that line works but the moment I try to run it in my program it doesn't.
The source code is here:
package main
import (
"os/exec"
"os"
"fmt"
"strings"
)
func main(){
fmt.Println("Please enter which scanner tool you wish to use:")
fmt.Println("nmap for nmap")
var toolargs []string
var ipaddress string
var toolchoice string
fmt.Scanln(&toolchoice)
if toolchoice == "nmap" {
toolargs = nmap()
fmt.Println(toolargs)
}else{
fmt.Println("invalid input")
}
fmt.Println("Please enter the IP address you wish to scan:")
fmt.Scanln(&ipaddress)
fmt.Println(toolchoice)
fmt.Println(toolargs)
fmt.Println(ipaddress)
funccmd(toolchoice, toolargs, ipaddress)
fmt.Scanln()
}
func nmap() []string{
var NoOfArgs int
var args1 string
var args2 string
var args3 string
var args4 string
var args5 string
var args []string
fmt.Println("Please enter a number for how many arguments you wish to use:")
fmt.Scanln(&NoOfArgs)
switch NoOfArgs{
case 1:
fmt.Println("Please enter your argument")
fmt.Scanf("%s", &args1)
args = []string{args1}
case 2:
fmt.Println("Please enter your arguments, separated with a blank space between arguments (eg. -sn -sS -O)")
fmt.Scanf("%s %s", &args1, &args2)
fmt.Println(args1)
fmt.Println(args2)
args = []string{args1, args2}
fmt.Println(args)
case 3:
fmt.Println("Please enter your arguments, separated with a blank space between arguments (eg. -sn -sS -O)")
fmt.Scanf("%s %s %s", &args1, &args2, &args3)
args = []string{args1, args2, args3}
case 4:
fmt.Println("Please enter your arguments, separated with a blank space between arguments (eg. -sn -sS -O)")
fmt.Scanf("%s %s %s %s", &args1, &args2, &args3, &args4)
args = []string{args1, args2, args3, args4}
case 5:
fmt.Println("Please enter your arguments, separated with a blank space between arguments (eg. -sn -sS -O)")
fmt.Scanf("%s %s %s %s %s", &args1, &args2, &args3, &args4, &args5)
args = []string{args1, args2, args3, args4, args5}
}
fmt.Println(args)
return args
}
func funccmd(toolchoice string, args []string, ipaddress string){
execpath, _ := exec.LookPath("/usr/bin/nmap")
stringargs := strings.Join(args, " ")
fmt.Println(stringargs)
final := []string{execpath, stringargs, ipaddress}
fmt.Println(final)
cmdNmap := &exec.Cmd{
Path: execpath,
Args: []string{execpath, stringargs, ipaddress},
Stdout: os.Stdout,
Stderr: os.Stdout,
}
fmt.Println(args)
if err := cmdNmap.Run(); err !=nil{
fmt.Println("Error", err);
}
}
The problem is that you are joining the arguments together into a single string (stringargs
) before passing them to exec.Cmd
. They need to each be a separate argument in the Args
array. Nmap is seeing the first arguments as "-oN results.txt" and parsing that as the deprecated option "-o" and the file name "N results.txt". The warning is precisely to prevent this kind of error, since you most likely do not mean to create an output file with the name "N results.txt".