[Go]Execute commands in Linux

I have been doing a few HTB machines and used some tools for enumeration and a lot of those tools are made from Golang, I wished to create my own tool in Go and hence I am starting to learn about the language.

Here is a code snippet on how to interact with OS commands.

package main

import (

func execCmd(command string, argsv []string) (err error){
	args := argsv
	cmdObj := exec.Command(command, args...)
	cmdObj.Stdout = os.Stdout
	cmdObj.Stderr = os.Stderr
	err = cmdObj.Run()
	if err != nil {
		return err
	return nil

func main() {
	This demonstrates on executing linux commands.
	To change mac address ip command is used instead of ifconfig.
	step1: shutdown interface by sudo ip link set dev ens33 down
	step2: change hardware address which is mac address, sudo ip link set dev ens33 address 00:11:22:33:44:55
	step3: turn on interface, sudo ip link set dev ens33 up
	Check the new interface address ip link show ens33
	Check all interfaces use ip link show.
	sudo := "sudo"
	device := "ens33"
	macAddress := "00:11:22:33:44:55"
	var options = map[string]string{
		"down": "down",
		"up": "up",
		"address": "address",
		"newMac": macAddress,

	execCmd(sudo, []string{"ip", "link", "set", "dev", device, options["down"]})
	execCmd(sudo, []string{"ip", "link", "set", "dev", device, options["address"], options["newMac"]})
	execCmd(sudo, []string{"ip", "link", "set", "dev", device, options["up"]})
	execCmd("ip", []string{"link", "show", device})

When the binary is run, password will be prompted by the OS, after entering the password the mac address is changed.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s