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