2

Just started to learn GO. Need to add rule into iptables. Thats how i read ip from client

  func get_client_ip(w http.ResponseWriter, r *http.Request) {

  ip,_,_ := net.SplitHostPort(r.RemoteAddr)
   }

I need use "ip" variable here

cmd := exec.Command("iptables", "-I INPUT -s ip -j ACCEPT")

What is the right way to use variables in os/exec ?

1 Answer 1

2

Command arguments map one to one with function arguments to exec.Command. Do it like this:

cmd := exec.Command("iptables", "-I", "INPUT", "-s", ip, "-j", "ACCEPT")

The code in the question passes "-I INPUT -s ip -j ACCEPT" as a single argument to the command. That's not what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.