ScoobyNet.com - Subaru Enthusiast Forum

ScoobyNet.com - Subaru Enthusiast Forum (https://www.scoobynet.com/)
-   Computer & Technology Related (https://www.scoobynet.com/computer-and-technology-related-34/)
-   -   Change IP LogIn Script Help *please*;) (https://www.scoobynet.com/computer-and-technology-related-34/316968-change-ip-login-script-help-please.html)

Dr Hu 06 April 2004 01:57 PM

Change IP LogIn Script Help *please*;)
 
Calling the Power of Scoobynet

I need to change the static IP's of about 150 pcs on our network, and the associated Hosts file - something like this:-

m\c W1A gets IP 10.2.1.1
m\c W1B gets IP 10.2.1.2
m\c W1C gets IP 10.2.1.3
etc

m\c W2A gets IP 10.2.2.1
m\c W2B gets IP 10.2.2.2
m\c W2C gets IP 10.2.2.3

I know you can use NETSH with W2k & XP - see example:-

:: Set the IP address.
netsh interface ip set address "Local Area Connection"
static 192.168.1.10 255.255.255.0 192.168.1.1 1
netsh interface ip set dns "Local Area Connection"
static 192.168.1.2


but i need it to 'look up' the m\c name and choose the correct IP from the list, using a TXT file or similar. HELP!
I could have one script per block i.e W1's W2's W3's

I reckon changing the HOSTS file should be easy such as:

if exists "C:\windows\drivers\system32\drivers\hosts copy \\server\hosts C:\windows\drivers\system32\drivers\hosts"
if exists "C:\winnt\drivers\system32\drivers\hosts copy \\server\hosts C:\winnt\drivers\system32\drivers\hosts"


all the PC's are mainly W2k or XP (occasional NT but I can do these few manually if needed) using NT4 domain server

Actually could probably use %root% on the above example to save having to have two lines

And don't even mention DHCP - i know, i know - but my IT director doesnt:rolleyes:

SO anybody got any help with this script - beg beg:D

David_Wallis 06 April 2004 02:00 PM

easy...

Mail me

David.wallis@ventura-uk.com

Dr Hu 06 April 2004 02:17 PM

Thanks for the swift post -

YHM :)

David_Wallis 06 April 2004 02:46 PM

Code:

' Read Machine Names from file specified below in the format: machinename,Ipaddress with one machine per line
' David Wallis        06/04/2004

Const ForReading = 1
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
strFileName = "p:\machines.txt"

Set objNetwork = CreateObject("WScript.Network")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (strFileName, ForReading)

Do Until objTextFile.AtEndOfStream
        strNextLine = objTextFile.Readline
        strMachineName = Mid(strNextLine,1,(InStr(1,UCase(Trim(strNextLine)),",")-1))

        If strMachineName = UCase(objNetwork.ComputerName) Then
                strIp = Mid(strNextLine,(InStr(1,UCase(Trim(strNextLine)),",")+1),Len(strNextLine))
                Wscript.echo "Match Found for " & strMachineName & " " & strIp

                strComputer = "."
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
                strIPAddress = Array(strIp)

                For Each objNetAdapter in colNetAdapters
                        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
                        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
                        If errEnable = 0 Then
                                WScript.Echo "The IP address has been changed."
                        Else
                                WScript.Echo "The IP address could not be changed."
                        End If
                Next

        End If
Loop


Dr Hu 06 April 2004 03:17 PM

Blimey - Thanks a lot, it looks fabulous - I won't try and fool you into knowing what the hell it does - its mostly double dutch to me!

How do I format my TXT file?

W1A,10.2.0.1,W1B,10.2.0.2,W1C,10.2.0.3....etc

or another way?

and do I need to run it for each block, W1's W2's or does it do it in one big file?

Thanks again!

David_Wallis 06 April 2004 07:40 PM

machinea,10.0.0.1
computera,10.0.0.2
fred,192.168.0.1

run it for each block??? eh?

Dr Hu 06 April 2004 11:25 PM

yep - tried it on my PC this afternoon and it works great - thanks! Just need to build the full TXT file & set the Login Script for all the users now.

I tried to add the pri DNS setting in to the script but it didnt work:-

I put in:-

define the Dns str variable at the top of the script a la Gateway & subnet

strDns = Array("195.183.89.230")

then near the bottom after the errGateways line:

errDns = objNetAdapter.SetDns(strDns, strDnsmetric)

I'm guessing its just a syntax problem as the DNS has a primary & secondary value - or am I going about this the wrong way.....

Thanks again for all your help - you've saved me some serious shoe leather here!

David_Wallis 07 April 2004 10:39 AM

dns doesnt have a metric for a start.

David

David_Wallis 07 April 2004 10:46 AM

try....

Code:

' Read Machine Names from file specified below in the format: machinename,Ipaddress with one machine per line
' David Wallis        06/04/2004

Const ForReading = 1
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
strDNSServerSearchOrder = Array("192.168.0.123","192.168.1.123")
strFileName = "p:\machines.txt"

Set objNetwork = CreateObject("WScript.Network")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile (strFileName, ForReading)

Do Until objTextFile.AtEndOfStream
        strNextLine = objTextFile.Readline
        strMachineName = Mid(strNextLine,1,(InStr(1,UCase(Trim(strNextLine)),",")-1))

        If strMachineName = UCase(objNetwork.ComputerName) Then
                strIp = Mid(strNextLine,(InStr(1,UCase(Trim(strNextLine)),",")+1),Len(strNextLine))
                Wscript.echo "Match Found for " & strMachineName & " " & strIp

                strComputer = "."
                Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
                Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
                strIPAddress = Array(strIp)

                For Each objNetAdapter in colNetAdapters
                        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
                        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
                        errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNSServerSearchOrder)
                        If errEnable = 0 Then
                                WScript.Echo "The IP address has been changed."
                        Else
                                WScript.Echo "The IP address could not be changed."
                        End If
                Next

        End If
Loop



All times are GMT +1. The time now is 10:00 AM.


© 2024 MH Sub I, LLC dba Internet Brands