dongqin6926
2018-02-09 03:12
浏览 117
已采纳

如何在GO中的os / exec中使用变量?

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 ?

图片转代码服务由CSDN问答提供 功能建议

刚开始学习GO。 需要在iptables中添加规则。 这就是我从客户端读取ip的方式

  func get_client_ip(w http.ResponseWriter,r * http.Request){
 
 ip,_,_:= net.SplitHostPort(r.RemoteAddr)
} \  n   
 
 

我需要在此处使用“ ip”变量

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

在os / exec中使用变量的正确方法是什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpq755012465 2018-02-09 03:18
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题