V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yanzhiling2001
V2EX  ›  问与答

请教一个 golang 使用 exec 调用 windwos cmd 命令的问题

  •  
  •   yanzhiling2001 · 2020-09-25 23:59:38 +08:00 · 1763 次点击
    这是一个创建于 1280 天前的主题,其中的信息可能已经有所发展或是发生改变。

    cmd 里直接下面命令是设置 ip 的:

    netsh interface ip set address "本地连接 6" source= static 192.168.1.110 255.255.255.0 192.168.1.1
    

    当前有个用 golang 设置 ip 的需求,使用 exec.command 调用 cmd 命令,谷歌一圈 exec.command,简单的 del 文件是没问题,拼设置 ip 的命令一直报错:

    C:\Users\Administrator\Desktop>go run setip.go
    Error:  exit status 1
    

    代码如下

    func main() {
    
    c := exec.Command("cmd", "/C", "netsh interface ip set address", "以太网 6", "static", "192.168.1.110", "255.255.255.0", "192.168.1.1", "1")
    
    if err := c.Run(); err != nil {
    	fmt.Println("Error: ", err)
    
    }
    

    }

    请教一下各位老大,该怎么写合适

    17 条回复    2021-11-16 09:59:02 +08:00
    fishCatcher
        1
    fishCatcher  
       2020-09-26 00:43:15 +08:00 via iPhone
    "以太网 6"改成`"以太网 6"`
    fishCatcher
        2
    fishCatcher  
       2020-09-26 00:44:17 +08:00 via iPhone
    而且你不能把 netsh interface ip set address 放到一个参数上吧,每个空个都是一个单独参数
    yanzhiling2001
        3
    yanzhiling2001  
    OP
       2020-09-26 00:45:50 +08:00
    @fishCatcher 感谢回复,我试试
    ysc3839
        4
    ysc3839  
       2020-09-26 00:47:39 +08:00 via Android
    1. netsh 本来就是一个可执行文件,可以直接执行 netsh,没必要多套一层 cmd 来执行。

    2. Windows 和 Unix 不同,命令行参数传递的是一个字符串,而不是 Unix 那样传递字符串数组,应用程序会自己把一个字符串解析成字符串数组。
    在这里我估计你把整个命令行直接通过一个参数传过去也没问题,因为你传多个参数,最终还是会拼接在一起的。
    WordTian
        5
    WordTian  
       2020-09-26 00:51:15 +08:00 via Android
    建议把 netsh 之后的字符串数组拼成一个长字符串,我之前在 linux 上分开写,也报找不到参数
    kangsheng9527
        6
    kangsheng9527  
       2020-09-26 05:02:51 +08:00
    是不是想做 v 什么 n ???
    这种方式 v 什么 n 很明显特征很容易暴露很容易识别。。。带虚拟网卡的也是。。。
    yanzhiling2001
        7
    yanzhiling2001  
    OP
       2020-09-26 10:43:17 +08:00
    @kangsheng9527 不是,因为种种原因工厂内网的一批电脑 重启之后是 DHCP 自动获取,本来该是固定 IP 的,为了避免重启手动设置 ip,写一个开机小脚本
    ysc3839
        8
    ysc3839  
       2020-09-26 11:11:28 +08:00
    @yanzhiling2001 写脚本的话可以考虑使用 cmd 或者 PowerShell 。
    yanzhiling2001
        9
    yanzhiling2001  
    OP
       2020-09-26 12:11:11 +08:00
    @ysc3839 开始考虑过,如果只是简单的设置 ip 就用 cmd 了。主要是内网的划分经常会变,每次变的时候会给一个包含 ip 信息的 json 文件,不擅长 cmd 脚本,用 go 写写了
    yanzhiling2001
        10
    yanzhiling2001  
    OP
       2020-09-26 23:22:35 +08:00
    @ysc3839 大佬白天有空吗,20 红包求解决这个问题,试了好一会还是不行
    ysc3839
        12
    ysc3839  
       2020-09-26 23:43:01 +08:00
    @yanzhiling2001 自己测试了一下,这样就可以了
    exec.Command("netsh", "interface", "ip", "set", "address", "Ethernet", "static", "192.168.1.110", "255.255.255.0", "192.168.1.1", "1")
    yanzhiling2001
        13
    yanzhiling2001  
    OP
       2020-09-27 00:26:46 +08:00
    @ysc3839 大佬 NB 大佬回复个收款码。
    我勒个擦,我白天也是这么写过,哪里出错了一直没有测试成功
    ysc3839
        14
    ysc3839  
       2020-09-27 00:38:44 +08:00 via Android
    @yanzhiling2001 我不收钱。
    可能是没有管理员权限?
    yanzhiling2001
        15
    yanzhiling2001  
    OP
       2020-09-27 00:54:57 +08:00
    @ysc3839 大佬不用客气,这是我该交的学费。
    可能是,之前一直在 cmd 里执行的,刚刚在 vscode 的 terminal 里执行的,vscode 是有 administrator 权限。
    大佬请留下收款码,白天加个鸡腿。感谢帮助。
    yanzhiling2001
        16
    yanzhiling2001  
    OP
       2021-11-16 09:56:07 +08:00
    golang windows 程序获取管理员权限( UAC )

    在 windows 上执行有关系统设置命令的时候需要管理员权限才能操作,比如修改网卡的禁用、启用状态。双击执行是不能正确执行命令的,只有右键以管理员身份运行才能成功。
    为解决此问题,花了很长时间找了各种方法,最终找到一个简单的方法,双击也能执行成功了。过程如下:
    1> Go get github.com/akavel/rsrc
    2> 把 nac.manifest 文件拷贝到当前 windows 项目根目录
    3> rsrc -manifest nac.manifest -o nac.syso
    4> go build

    nac.mainfest 的内容为:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
    <requestedPrivileges>
    <requestedExecutionLevel level="requireAdministrator"/>
    </requestedPrivileges>
    </security>
    </trustInfo>
    yanzhiling2001
        17
    yanzhiling2001  
    OP
       2021-11-16 09:59:02 +08:00
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
    <requestedPrivileges>
    <requestedExecutionLevel level="requireAdministrator"/>
    </requestedPrivileges>
    </security>
    </trustInfo>
    </assembly>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3585 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:38 · PVG 12:38 · LAX 21:38 · JFK 00:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.