My go language program prints "Bad value for option -n 1, valid range is from 1 to 4294967295." when trying to ping using below code snippet
result , err := exec.Command("ping","-n 1", "-w 1", ip).Output()
fmt.Printf("%s
", result)
When doing it from cmd in Win i.e. 'ping -n 1 -w 1 8.8.8.8' is fine
You need to separate the -n
and -w
flags and their values into separate arguments (your shell was already doing this):
result , err := exec.Command("ping", "-n", "1", "-w", "1", ip).Output()
exec.Command()
doesn't create a string to run all at once.
It creates the ping
process and a set of options that gets sent to it.
So each flag and its corresponding values need to be passed separately:
result, err := exec.Command("ping","-n", "1", "-w", "1", ip).Output()