[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do modify windows IP number using Ruby/

guruprasad.mayya

4/26/2006 9:39:00 AM

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

4 Answers

Chris Hulan

4/26/2006 4:38:00 PM

0

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

guruprasad.mayya

4/27/2006 4:44:00 AM

0

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.

Heesob Park

4/27/2006 7:30:00 AM

0

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

guruprasad.mayya

5/3/2006 8:48:00 AM

0

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