Add/Delete ip address

2,100 views
Skip to first unread message

Andrew

unread,
Feb 20, 2015, 5:26:37 PM2/20/15
to golan...@googlegroups.com

I can add (or delete) an ip address using the following command from cmd.exe:

"netsh interface ipv4 add (or delete) adapterName x.x.x.x y.y.y.y"

How can I set / remove an ip address from a network adapter within Go? (Windows 7)

Thanks.

Tamás Gulácsi

unread,
Feb 21, 2015, 1:06:35 AM2/21/15
to golan...@googlegroups.com
a) search the proper MS API and use that,
b) just call "netsh" with is/exec.Command

Andrew

unread,
Feb 23, 2015, 12:37:56 PM2/23/15
to golan...@googlegroups.com
Thanks, I used the second method and it works fine. Just wondering if Go has native support for this function.

Andrew

unread,
Feb 24, 2015, 7:53:17 PM2/24/15
to golan...@googlegroups.com
The netsh command can use the adapter(network interface) name to set the IP address like this:

netsh interface ipv4 add "Local Area Connection 2" 192.168.1.1 255.255.255.0

But from Go I get the interface name like this: {A3768A7A-8E5F-42F8-81E3-3BCAACEC9FBE}, this name doesn't work when using netsh.

Yes, I can use the adapter index number instead. But is there a way to convert this name to "Local Area Connection 2"?

Thanks.

brainman

unread,
Feb 25, 2015, 7:20:16 PM2/25/15
to golan...@googlegroups.com
On Saturday, 21 February 2015 09:26:37 UTC+11, Andrew wrote:
How can I set / remove an ip address from a network adapter within Go? (Windows 7) 

I don't believe it is possible to use standard Go library to set / remove ip addresses on Windows. Tamás suggestion sounds reasonable to me. I don't know how to do it with MS API. But, if you provide "netsh" commands to do what you want, I will show you how to call them using os/exec package.

Alex
Reply all
Reply to author
Forward
0 new messages