Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating IP details 1

Status
Not open for further replies.

dulux

Programmer
Sep 4, 2002
36
GB
Hi all,

I have a potentially stupid question...

I would like to be able to run a perl program that changes the IP Address, Subnet Mask and Workgroup \ Domain name under Win2k.

Basically, I am using my laptop on 2 separate networks - home and office, and am mighty hacked off with having to mess about with my IP settings twice a day.

Is this possible? Is there a module I can download and use?

Thanks in advance

 
There is probably a DHCP server in the network that assigns IP's Automaticly, try to make 2 networkconnections 1 for home and one for work and disable/enable them
 
Set up hardware profiles. Set one for work and one for home or whatever it would be. When the computer starts up it will prompt you for one or the other. Just select which profile and set the TCP/IP settings. When you switch between them the settings will change as well.
 
Thanks all,

I will implement your suggestions, but I am now faced with the poser - can it be done with a script anyway?

I am now interested in how much of the back end of windows I can actually mess around with using perl...

Is there a good resource anyone knows of for this?

Thanks again

 
Have a look at some of the Win32:: modules in CPAN. I don't know if there is a module that does the trick, but there are plenty of Win32 specific modules so it's possible. Barbie
Leader of Birmingham Perl Mongers
 
Thanks.

I get the feeling I will be downloading the whole of CPAN eventually...

 
Hi,

I have 4 networks to connect too + the one at home. To get around this I hacked an existing script. Unfortunatly it is in VBscript and not perl, but here it is anyway


'****************************************************************
' File: getipname.vbs (WSH for VBscript)
' Author: Shane Dunn 2002/07/24
'
' Based on script by M. Harris & T. Lavedas & M. Gallant:
' posted to:

' This application write/delete entries in the registry to change internet settings
' Reads IP addresses via:
' ipconfig.exe (NT4 and Win2000)
' winipcfg.exe (Win95)
' For NT4, Win2000 resolves IP addresses to FQDN names via:
' nslookup.exe (with default DNS server)
'
'****************************************************************
'
' ------------------------------------------------------------------------
' Copyright (C) 2002 DunLow Pty Ltd
'
' You have a royalty-free right to use, modify, reproduce and distribute
' this Application Files (and/or any modified version) in any way
' you find useful, provided that you agree that DunLow has no warranty,
' obligations or liability for any Application Files.
' ------------------------------------------------------------------------
'

Dim WSHShell

arAddresses = GetIPAddresses()
info = ubound(arAddresses)+1 & " IP Address(es) found"
if ubound(arAddresses) > -1 then
Set WSHShell = WScript.CreateObject("WScript.Shell")
for each ip in arAddresses
info = info & vbcr & ip & " : " & vbTab & GetFQDN(ip)
data = split(ip,".")
select case data(0) & "." & data(1)
case "10.112"
'Gateway 10.112.64.1
'DHCP server 10.112.64.6
'DNS servers 10.112.64.12 10.60.79.219 10.112.4.41
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", "10.112.64.12:8080"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", "internal*.health.nsw.gov.au;127.0.0.1;10.*"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
On Error Resume Next
WSHShell.RegDelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\AutoConfigURL"
info = info & vbcr & "Internet Setup for MWAHS"
case "10.32"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", "swsproxy1:8080"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", "internal*.health.nsw.gov.au;127.0.0.1;10.*"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
On Error Resume Next
WSHShell.RegDelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\AutoConfigURL"
info = info & vbcr & "Internet Setup for SWSAHS"
case "10.192"
'Gateway 10.192.83.1
'DHCP server 10.192.65.168
'DNS servers 10.192.65.21 10.192.65.168
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", "proxy12.health.nsw.gov.au:8888"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", "internal*.health.nsw.gov.au;doh.health.nsw.gov.au;10.*.*.*;127.0.0.1;10.66.0.26"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
On Error Resume Next
WSHShell.RegDelete "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\AutoConfigURL"
info = info & vbcr & "Internet Setup for NSW Health"
'case "10.22"
case "10.27"
'Gateway 10.27.3.254
'DHCP server 10.27.2.3
'DNS servers 10.27.2.3 10.22.239.200 10.22.239.201
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyServer", ""
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyOverride", ""
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\AutoConfigURL", " info = info & vbcr & "Internet Setup for NSAHS"
case else
info = info & vbcr & "No Internet Setup"
end select
next
set WSHShell = nothing
else
info = info & vbcr & "Not Connected"
end if
WScript.echo info

Function GetFQDN(ipaddress)
'====
' Returns Fully Qualified Domain Name
' from reverse DNS lookup via nslookup.exe
' only implemented for NT4, 2000
'====
set sh = createobject("wscript.shell")
set fso = createobject("scripting.filesystemobject")
Set Env = sh.Environment("PROCESS")

info = "OS : " & Env("OS")
if Env("OS") = "Windows_NT" then
workfile = fso.gettempname
sh.run "%comspec% /c nslookup " & ipaddress & " > " & workfile,0,true
set sh = nothing
set ts = fso.opentextfile(workfile)
data = split(ts.readall,vbcr)
ts.close
set ts = nothing
fso.deletefile workfile
set fso = nothing
for n = 0 to ubound(data)
info = info & vbcr & data(n)
if instr(data(n),"Name") then
parts = split(data(n),":")
hostname= trim(cstr(parts(1)))
Exit For
end if
hostname = "could not resolve IP address"
next
GetFQDN = hostname
else
set sh = nothing
set fso = nothing
GetFQDN = ""
end if
'WScript.echo info
End Function


Function GetIPAddresses()
'=====
' Returns array of IP Addresses as output
' by ipconfig or winipcfg...
'
' Win98/WinNT have ipconfig (Win95 doesn't)
' Win98/Win95 have winipcfg (WinNt doesn't)
'
' Note: The PPP Adapter (Dial Up Adapter) is
' excluded if not connected (IP address will be 0.0.0.0)
' and included if it is connected.
'=====
set sh = createobject("wscript.shell")
set fso = createobject("scripting.filesystemobject")

Set Env = sh.Environment("PROCESS")
info = "OS : " & Env("OS")
if Env("OS") = "Windows_NT" then
workfile = fso.gettempname
sh.run "%comspec% /c ipconfig > " & workfile,0,true
else
'winipcfg in batch mode sends output to
'filename winipcfg.out
workfile = "winipcfg.out"
sh.run "winipcfg /batch" ,0,true
end if
set sh = nothing
set ts = fso.opentextfile(workfile)
data = split(ts.readall,vbcr)
ts.close
set ts = nothing
fso.deletefile workfile
set fso = nothing
arIPAddress = array()
index = -1
for n = 0 to ubound(data)
info = info & vbcr & data(n)
if instr(data(n),"IP Address") then
info = info & vbcr & "-----------------------------"
parts = split(data(n),":")
if trim(parts(1)) <> &quot;0.0.0.0&quot; then
index = index + 1
ReDim Preserve arIPAddress(index)
arIPAddress(index)= trim(cstr(parts(1)))
end if
end if
next
'WScript.echo info
GetIPAddresses = arIPAddress
End Function


does not use WMI but gets there in the end

Shane
 
Brilliant.

Thanks for the effort.

 
Hi,

why not just use netsh.
Open a command promt and type netsh. It will even create a script for you to do the changes.
 
netsh? I'm not familiar with it...

Can you elaborate, or maybe direct me to a source where I can find out about it?

Thanks
 
these are the commands in the netsh shell(from a ms-dos session)

netsh>help

The following commands are available:

Commands in this context:
.. - Goes up one context level.
? - Displays a list of commands.
abort - Discards changes made while in offline mode.
add - Adds a configuration entry to a list of entries.
alias - Adds an alias.
bridge - Changes to the `netsh bridge' context.
bye - Exits the program.
commit - Commits changes made while in offline mode.
delete - Deletes a configuration entry from a list of entries.
diag - Changes to the `netsh diag' context.
dump - Displays a configuration script.
exec - Runs a script file.
exit - Exits the program.
help - Displays a list of commands.
interface - Changes to the `netsh interface' context.
offline - Sets the current mode to offline.
online - Sets the current mode to online.
popd - Pops a context from the stack.
pushd - Pushes current context on stack.
quit - Exits the program.
ras - Changes to the `netsh ras' context.
routing - Changes to the `netsh routing' context.
set - Updates configuration settings.
show - Displays information.
unalias - Deletes an alias.

The following sub-contexts are available:
bridge diag interface ras routing

To view help for a command, type the command, followed by a space, and then
type ?.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top