windows - Setting Static IP Address VB.net -
i writing script setting static ip computers. reads file has mac addr - ip addr pair. based on computers mac address gets ip address file. have problem setting up. have never done kind of .net programming. wrote bashscript linux side works, windows don't have experience. wrote program in vb.net. until program can data file, have set static ip based on mac address , hostname. there several different posts 1, 2, in c# ,and have problem converting them vb.net. great if provide pointer on how set static ip address specific nic on local computer.
imports system imports system.text.regularexpressions imports system.net.networkinformation imports system.io imports system.management module module1 const failure = 1 const success = 0 dim phyaddr string = getmac() sub main() dim arguments(3) string dim filename string = "" if environment.getcommandlineargs.count = 3 arguments = environment.getcommandlineargs filename = arguments(2) else console.writeline("wrong syntax!") help() console.read() close(failure) end if if validname(filename) if fileexists(filename) 'search file ip dim confdata string = searchfile(phyaddr, filename) if not string.isnullorempty(confdata) dim netconf() string = splitline(confdata) dim hostname string = netconf(1) dim ipaddr string = netconf(2) dim netmask string = netconf(3) dim gateway string = netconf(4) dim dns1 string = netconf(5) dim dns2 string = netconf(6) else console.writeline("couldn't find mac {0} in file {1}", phyaddr, filename) console.read() close(failure) end if else console.writeline("file {0} doesn't exist", filename) console.writeline("please provide absolute path file") console.read() close(failure) end if else console.writeline("file name {0} not recognized", filename) console.read() close(failure) end if end sub private sub help() console.writeline("please call program as: ") console.writeline("networkconfiguration -f datafile") end sub private sub close(exitcode integer) environment.exit(exitcode) end sub private function validname(name string) boolean static filenameexpression new regex("^[\\:_a-za-z0-9.]+") return filenameexpression.ismatch(name) end function private function fileexists(name string) boolean return my.computer.filesystem.fileexists(name) end function private function getmac() string dim nic networkinterface dim result string = string.empty each nic in networkinterface.getallnetworkinterfaces() if nic.name.contains("ethernet0") result = nic.getphysicaladdress.tostring exit end if next return result end function private function searchfile(keyword string, filename string) string 'store result dim result string = string.empty 'search keyword in returned data using reader new streamreader(filename) while not reader.endofstream dim line string = reader.readline if line.contains(keyword) result = line exit while end if end while end using return result end function private function splitline(line string) string() dim separator char = ";" return line.split(separator) end function private function setupnetwork(ipaddr string, netmask string, gateway string, dns1 string, dns2 string) boolean dim mc new managementclass("win32_networkadapterconfiguration") dim moc new managementobjectcollection dim mo managementobject moc = mc.getinstances() each mo in moc 'make sure ipenabled device 'not memory card or vmware next end function end module
ok solved it. post answer here others might benefit.
' set network configuration of computer function setupnetwork(phyaddr string, ipaddr string, netmask string, gateway string, dns1 string, dns2 string) boolean dim result boolean = false ' concatenate 2 dns addresses 1 dim dnssearchorder string = dns1 + "," + dns2 dim objmc managementclass = new managementclass("win32_networkadapterconfiguration") dim objmoc managementobjectcollection = objmc.getinstances() each objmo managementobject in objmoc if (cbool(objmo("ipenabled"))) ' remove colons mac address match ' provided mac address dim origmac string = objmo("macaddress").tostring() dim pattern string = ":" dim replacement string = "" dim rgx new regex(pattern) ' mac address colons removed dim repmac string = rgx.replace(origmac, replacement) if (string.equals(phyaddr, repmac)) try dim objnewip managementbaseobject = nothing dim objnewgate managementbaseobject = nothing dim objnewdns managementbaseobject = nothing dim objsetip managementbaseobject = nothing objnewip = objmo.getmethodparameters("enablestatic") objnewgate = objmo.getmethodparameters("setgateways") objnewdns = objmo.getmethodparameters("setdnsserversearchorder") 'set defaultgateway objnewgate("defaultipgateway") = new string() {gateway} objnewgate("gatewaycostmetric") = new integer() {1} 'set ipaddress , subnetmask objnewip("ipaddress") = new string() {ipaddr} objnewip("subnetmask") = new string() {netmask} objnewdns("dnsserversearchorder") = dnssearchorder.split(",") objsetip = objmo.invokemethod("enablestatic", objnewip, nothing) objsetip = objmo.invokemethod("setgateways", objnewgate, nothing) objsetip = objmo.invokemethod("setdnsserversearchorder", objnewdns, nothing) result = true exit catch ex exception console.writeline("couldn't set ip address!") console.read() close(failure) end try end if end if next return result end function 'set computers host name private function sethostname(hostname string) boolean dim result boolean = false dim path new managementpath path.server = system.net.dns.gethostname path.namespacepath = "root\cimv2" path.relativepath = "win32_computersystem.name='" & path.server & "'" dim objmo new managementobject(path) dim params() object = {hostname} objmo.invokemethod("rename", params) result = true return result end function
Comments
Post a Comment