How do modify windows IP number using Ruby/

G

guruprasad.mayya

Hye

Am not a programmer. Dont have much use for it so never could get
around to remembering the syntax.

Took to ruby because of its simplicity but having a tough time
programming - cause i tire doing toy programs.

Anyway, i am currentlyusing my office laptops for various
presentations. Each floor of my office has its own unique IP number
sequence and so my laptop cannot access the network on each floor. This
requires me to keep changing the ip numbers on each floor.

Thought this would be a great ruby project for me. I could write a
small ruby programme which would accept the new IP number, Mask,
Gateway and DNS number and update it in one go instead of me having to
go to network properties, advanced blah blah and change the IP number
the traditional way.

But am unable to figure out how one can modify the system IP number.
Can anybody tell me what command / library to use to do that? I can
experiment and try out other requierments such as getting an input, IP
format validation and perhaps a graphical interface. But this one
confounds me.

Would appreciate all help received.

Thanks a bunch in advance.
Prasad
 
C

ChrisH

The command you need is 'ipconfig', the CLI tool that you use to
view/change IP info under windows. Execute it from a ruby script (via
Kernel::` or Kernel::system) with the appropriate settings should do
the trick

Cheers
 
G

guruprasad.mayya

Hye

So we use the Kernel::system to execute a command line programme eh?

Thanks. Think i can use this info.

But right now, I dont think ipconfig is the thing for me. I tried
ipconfig (not inside ruby; as a command line command) but was not able
to make it change my IP. Our company uses static IP numbers and does
not use a DHCP server. That is where the problem is. Ipconfig whould
have been useful in that case.
 
P

phasis

Hi,

You can modify IP setting using win32ole and WMI.
Following code modify ip setting of all ethernet adapters.
Modify sWQLQuery for your environment.

----
require 'win32ole'

ip = "192.168.0.10"
mask = "255.255.255.0"
gw = "192.168.0.1"
dns = ["1.2.3.4","1.2.3.5"]

sWQLQuery = "SELECT * FROM Win32_NetworkAdapter WHERE AdapterType like
'% 802.3' "

mgmt =
WIN32OLE.connect("winmgmts:{impersonationLevel=impersonate}//./root/CIMV2")

mgmt.ExecQuery(sWQLQuery,"WQL").each do |oInstance|

oInstance.Associators_(nil,"Win32_NetworkAdapterConfiguration").each
do |oAssociator|

oMethod = oAssociator.Methods_("EnableStatic")
oInParam = oMethod.InParameters.SpawnInstance_()

oInParam.IPAddress = [ip]
oInParam.SubnetMask = [mask]
oOutParam = oAssociator.ExecMethod_("EnableStatic", oInParam)

oMethod = oAssociator.Methods_("SetGateways")
oInParam = oMethod.InParameters.SpawnInstance_()

oInParam.DefaultIPGateway = [gw]
oInParam.GatewayCostMetric = [1]
oOutParam = oAssociator.ExecMethod_("SetGateways", oInParam)

oMethod = oAssociator.Methods_("SetDNSServerSearchOrder")
oInParam = oMethod.InParameters.SpawnInstance_()

oInParam.DNSServerSearchOrder = dns
oOutParam = oAssociator.ExecMethod_("SetDNSServerSearchOrder",
oInParam)

end

end



Regards,

Park Heesob
 
G

guruprasad.mayya

Sorry for the delay in replying. Wanted to try out the code but could
find no opportunity to use it - now that i have the solution, the
problem is evading me
:)

Thanks a bunch for yoru help. THat was remarkable.

Regards
Prasad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top