Jump to content

Automatic IP configuration


AlbinoRaven

Recommended Posts

Basically the following scripts ping a range of specified IP addresses which is then dropped into a txt file, another script picks the "available" IP address from the txt file and performs a netsh with the IP address variable and then assigns it to the NIC.

What I'm having trouble with is the first script writes all of the IP's into the txt file, so there are a couple of hundred IP's in the list rather than the first available one.

Second issue is the netsh command doesn't seem to want to assign the ip address. :( makes me sad.

Anyhoo, I have the entire thing running as an "installation" item with the RunOnceEx.cmd during an unattended setup. Next to computer name assignment, the ip addressing is one pain of many in deploying 50 servers without touching anything but my coffee.

Cheers!

Albino

Or if someone has a better way with VB I would seriously owe you a pint or a grande latte.

--------------------------------------------------

IPGrep1.cmd - releases the DHCP address

====start here====

@echo off

ipconfig /release

exit

====end here====

IPGrep2.cmd - the entire thing must be pasted as one line!

====start here====

@echo off

for /l %%a in (200,1,254) do (ping -n 1 -w 200 192.168.1.%%a |find "unreachable" && echo 192.168.1.%%a>>ipaddress.txt)

====end here====

ipaddress.txt

====start here====

<pinged ip addresses that are unreachable>

====end here====

IPGrep3.cmd

====start here====

for /f %%i in (ipaddress.txt) do netsh interface ip set address name="Local Area Connection"&netsh interface ip set address local static %%i 255.255.255.0 192.168.1.1

====end here====

Link to comment
Share on other sites


1. Create a folder on C: called Iprange (or whatever, just modify the script if you use a different folder)

2. Create a file in the iprange folder called source.txt

3. On the first line of the source.txt file, place the IP range you'd like to scan, like so: 192.168.0.

Run the following script:

'*** Begin Script ***

on error resume next

Function Pingable(strHost)
Dim objExec, objRE
set objshell = wscript.createobject("wscript.shell")
blnPingable = "False" 'assume failure
Set objExec = objShell.Exec("cmd /C ping -a -n 2 " & strHost)
string1 = objExec.StdOut.ReadAll
if instr(1,string1,"TTL=",1) >0 then
Pingable = True
else
pingable =false
end if
if pingable =True then
Chaine = "Pinging "
Chaine2 = "["
ValueSearch = InStr(1,string1,Chaine,1)
ValueSearch2 = InStr(valuesearch,string1,Chaine2,1)
If ValueSearch>=1 and ValueSearch2>1 Then
y= mid(string1,ValueSearch,(ValueSearch2-ValueSearch))
else
pingable="false"
End If
pingable = replace(y,"Pinging ","")
pingable = replace(pingable," ","")
if pingable =false or pingable="" then
pingable ="False"
end if
end if
Set objExec = Nothing
Set objRE = Nothing

End Function



Set iFSO = CreateObject("Scripting.FilesyStemObject")
Set oFSO = CreateObject("Scripting.FilesyStemObject")
InputFile ="c:\IpRange\Source.txt"
Outputfile="c:\IpRange\IpRangeLog.txt"

Set ofile = ofso.createTextFile(OutputFile, True)
Set ifile = iFSO.OpenTextFile(inputfile)



Do until ifile.AtEndOfLine
ip = ifile.ReadLine
ofile.writeline "*************************************************"
ofile.writeline "Scan network for ip range : " & ip & " Started"
ofile.writeline now()

x=ip

For i = 1 to 254
x= x + cstr(i)

servername = pingable(x)
if servername<>"False" then
ofile.writeline "________________________________________________"
ofile.writeline "Machine= " & servername & ": IP Address= " & x
ofile.writeline "________________________________________________"
else
ofile.writeline "machine:" & x & " Not Pingable"
end if

x=ip

next
ofile.writeline "Scan network for ip range : " & ip &" COmpleted"
ofile.writeline now()
ofile.writeline "*************************************************"

loop

'*** EOF ***

This will dump all output to a file in the Iprange folder called IpRangeLog.txt. Do whatever you want with that data :).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...