-
June 1st, 2015, 12:58 AM
#1
Restart Modem script help
Basically here is the solution I'm looking for.
I want to be able to have Windows 7 detect when it doesn't get an IP from DHCP and run a script.
So I think I can use Task Scheduler to do this but need some help and I have a semi written script that doesn't seem to be working but maybe someone on here can help.
Basically what I need it to do is once the task scheduler detects it doesn't have an IP from DHCP and gets a 169.x.x.x IP address the script runs and then changes to a static IP, then telnet into the modem and run the reset command then then change back to DHCP and renew the IP.
Here is what I have so far but it keeps getting stuck when it goes to to the modem and maybe because I'm opening something twice because 2 CMD black boxes come up and when it logs into the modem it doesn't work right. If I run the restart modem script by itself it works fine just not with all this other stuff attached to it.
Set obj = CreateObject("WScript.Shell")
obj.run"cmd"
WScript.Sleep 500
obj.SendKeys"telnet 192.168.100.1"
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet username
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet password
obj.SendKeys("{Enter}")
obj.SendKeys"reset"
obj.SendKeys("{Enter}")
Wscript.Sleep 500
Dim oShell : Set oShell = CreateObject("WScript.Shell")
' Kill telnet and cmd '
oShell.Run "taskkill /im telnet.exe", , True
oShell.Run "taskkill /im cmd.exe", , True
Wscript.Sleep 500
So here is the script that restarts the modem and works just fine if I run it. But the problem is that if the computer can't get an IP then this script will fail because it can't telnet to 192.168.100.1 so it needs to change the IP to a static IP of 192.168.100.11 subnet 255.255.255.0 gateway 192.168.100.1 in order for it to be able to telnet.
Once it has static IP it can then run the above part.
Then when it's done resetting the modem it should then go back to DHCP.
Hope someone can help me.
Thanks
Brad
-
June 1st, 2015, 04:39 AM
#2
Re: Restart Modem script help
Your PC probably has multiple IP addresses, so you need to find the index of the adaptor that you're really interested in. You can do this by opening a command prompt and typing:
WMIC PATH Win32_NetworkAdapterConfiguration WHERE IPEnabled=True GET CAPTION,IPADDRESS,INDEX
which will list all of them, their current IP address(es), and their index
If you want to find out the IP address of any given adaptor, for example at index 7, you can use this script:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Index=7")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
WScript.Echo "IP Address: " & IPConfig.IPAddress(i)
Next
End If
Next
You can use similar code to enable DHCP on any given interface, e.g. for the index 7 adaptor:
Code:
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableDHCP
If RetVal = 0 Then
WScript.Echo "Success! DHCP Enabled"
Else
WScript.Echo "DHCP not enabled"
End If
And this code to set a static IP address:
Code:
IPAddress = Array("192.168.233.197")
Subnet = Array("255.255.255.0")
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableStatic(IPAddress,Subnet)
If RetVal = 0 Then
WScript.Echo "Success! Static IP Enabled"
Else
WScript.Echo "Failed to set static IP " & IPAddress(0)
End If
Hope that all helps.
-
June 3rd, 2015, 09:08 AM
#3
Re: Restart Modem script help
It doesn't have 2 IP addresses. Just one. And the connection is labeled Local Area Connection. Or are you referring to the actual network card?
GET CAPTION,IPADDRESS,INDEX
Caption Index IPAddress
[00000007] Intel(R) 82566DM-2 Gigabit Network Connection 7 {"45.50.53.228"
, "fe80::a823:aabe:9b55:3dc0"}
C:\Users\admin>
So that 000000007 is that what you're talking about or the Intel(R) 82566DM-2 Gigabit Network Connection 7?
But here's what I get when I do that comm
Originally Posted by the_cat
Your PC probably has multiple IP addresses, so you need to find the index of the adaptor that you're really interested in. You can do this by opening a command prompt and typing:
WMIC PATH Win32_NetworkAdapterConfiguration WHERE IPEnabled=True GET CAPTION,IPADDRESS,INDEX
which will list all of them, their current IP address(es), and their index
If you want to find out the IP address of any given adaptor, for example at index 7, you can use this script:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Index=7")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
WScript.Echo "IP Address: " & IPConfig.IPAddress(i)
Next
End If
Next
You can use similar code to enable DHCP on any given interface, e.g. for the index 7 adaptor:
Code:
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableDHCP
If RetVal = 0 Then
WScript.Echo "Success! DHCP Enabled"
Else
WScript.Echo "DHCP not enabled"
End If
And this code to set a static IP address:
Code:
IPAddress = Array("192.168.233.197")
Subnet = Array("255.255.255.0")
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableStatic(IPAddress,Subnet)
If RetVal = 0 Then
WScript.Echo "Success! Static IP Enabled"
Else
WScript.Echo "Failed to set static IP " & IPAddress(0)
End If
Hope that all helps.
-
June 3rd, 2015, 09:41 AM
#4
Re: Restart Modem script help
Yes, the index is 7. In fact in all my example scripts I used 7 so you can just use them verbatim.
Note that it doesn't have 2 IP addresses - but it COULD have!
-
June 3rd, 2015, 11:25 AM
#5
Re: Restart Modem script help
Ok so I guess I'm having trouble figuring out how to make this script run automatically when the NIC doesn't get an IP. I know task scheduler allows you to do this. Does anyone know how?
I'm also trying to get this to go automatically and not put up pop up boxes letting me know that something has changed. Would anyone be so kind as to take my script above and mix it with the other scripts to have a seemless script that just runs each time it detects the NIC from not getting an IP?
Last edited by mdntblu; June 3rd, 2015 at 11:36 AM.
-
June 3rd, 2015, 11:48 AM
#6
Re: Restart Modem script help
I get this for the part that sets static.
And this for the second part after it does the telnet.
Any ideas?
Here's the full code I have:
Code:
IPAddress = Array("10.10.1.254")
Subnet = Array("255.255.252.0")
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableStatic(IPAddress,Subnet)
If RetVal = 0 Then
WScript.Echo "Success! Static IP Enabled"
Else
WScript.Echo "Failed to set static IP " & IPAddress(0)
End If
Set obj = CreateObject("WScript.Shell")
obj.run"cmd"
WScript.Sleep 500
obj.SendKeys"telnet 192.168.100.1"
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet username
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet password
obj.SendKeys("{Enter}")
obj.SendKeys"reset"
obj.SendKeys("{Enter}")
Wscript.Sleep 500
Dim oShell : Set oShell = CreateObject("WScript.Shell")
' Kill telnet and cmd '
oShell.Run "taskkill /im telnet.exe", , True
oShell.Run "taskkill /im cmd.exe", , True
Wscript.Sleep 2000
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=7")
RetVal = objAdapter.EnableDHCP
If RetVal = 0 Then
WScript.Echo "Success! DHCP Enabled"
Else
WScript.Echo "DHCP not enabled"
End If
-
June 3rd, 2015, 12:07 PM
#7
Re: Restart Modem script help
Your requirement is to check to see if the PC is on DHCP, and if NOT then it sets a static IP, connects to the modem, then sets itself back to DHCP, correct?
So put your code into a sub-routine and call it as required:
Code:
IPAddress = Array("192.168.233.197")
Subnet = Array("255.255.255.0")
strComputer = "."
NICIndex = 7
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & NICIndex)
If objAdapter.DHCPEnabled Then
'DHCP is enabled, so quit
WScript.Quit
Else
'DHCP not enabled - set static IP
objAdapter.EnableStatic(IPAddress,Subnet)
'Now Reset your modem
ResetModem
'Now re-enable DHCP and renew lease
objAdapter.EnableDHCP()
objAdapter.RenewDHCPLease()
End If
Sub ResetModem
Dim obj
Set obj = CreateObject("WScript.Shell")
obj.run"cmd"
WScript.Sleep 500
obj.SendKeys"telnet 192.168.100.1"
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet username
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet password
obj.SendKeys("{Enter}")
obj.SendKeys"reset"
obj.SendKeys("{Enter}")
Wscript.Sleep 500
End Sub
If objAdapter.DHCPEnabled always returns true then you will need to use the other code I gave you but get it to check if the first 3 characters of the current IP address are "169", e.g.
Code:
If Left(IPConfig.IPAddress(i),3)="169" Then
Then just add the VBScript file to your Task Scheduler, or alternatively, open up Windows Explorer to "shell:startup" and then drag a shortcut to this VBScript into there and it will run each time you log in.
-
June 3rd, 2015, 12:14 PM
#8
Re: Restart Modem script help
No, I know the PC will always be on DHCP (unless I change it), but there are times when the DHCP will stop giving an IP unless the modem is restarted using telnet script. I don't even need to know if it's on DHCP, I just need to know when DHCP isn't giving it an IP to switch to a certain static IP telnet to the modem and restart it and wait a few seconds until the modem restarts (usually takes about 15 secs) and then switch back to DHCP. Then be ready for the next time it happens.
I am not very good with this vbs stuff so I'm not quite understanding about sub routines, etc. Also I don't remember seeing that other code about the first 3 characters are 169 until right now. So where would that go?
-
June 3rd, 2015, 12:25 PM
#9
Re: Restart Modem script help
It's worth reading up on how sub-routines and procedures work: http://www.w3schools.com/vbscript/vb...procedures.asp
Not least because almost all programming languages have something similar. Basically a function returns a value (so you might have a function called "Add" that you give it 2 numbers and it returns their sum) whilst a sub-routine doesn't.
So from the sounds of it you do want to have this as a scheduled task. The script would then be something like this:
Code:
IPAddress = Array("10.10.1.254")
Subnet = Array("255.255.252.0")
strComputer = "."
NICIndex = 7
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & NICIndex)
If IsDHCPWorking Then
'DHCP is enabled, so quit
WScript.Quit
Else
'DHCP not enabled - set static IP
objAdapter.EnableStatic(IPAddress,Subnet)
'Now Reset your modem
ResetModem
'Wait your 15 seconds
WScript.Sleep 15000
'Now re-enable DHCP and renew lease
objAdapter.EnableDHCP()
objAdapter.RenewDHCPLease()
End If
Function IsDHCPWorking
Dim objWMIService, bolDHCP
bolDHCP = False
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Index=" & NICIndex)
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
If Left(IPConfig.IPAddress(i),3)="169" Then
bolDHCP = True
End If
Next
End If
Next
' return the value of the function, true or false
IsDHCP = bolDHCP
End Function
Sub ResetModem
Dim obj
Set obj = CreateObject("WScript.Shell")
obj.run"cmd"
WScript.Sleep 500
obj.SendKeys"telnet 192.168.100.1"
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet username
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet password
obj.SendKeys("{Enter}")
obj.SendKeys"reset"
obj.SendKeys("{Enter}")
Wscript.Sleep 500
End Sub
Last edited by the_cat; June 3rd, 2015 at 12:26 PM.
Reason: typo
-
June 3rd, 2015, 12:41 PM
#10
Re: Restart Modem script help
Originally Posted by the_cat
It's worth reading up on how sub-routines and procedures work: http://www.w3schools.com/vbscript/vb...procedures.asp
Not least because almost all programming languages have something similar. Basically a function returns a value (so you might have a function called "Add" that you give it 2 numbers and it returns their sum) whilst a sub-routine doesn't.
So from the sounds of it you do want to have this as a scheduled task. The script would then be something like this:
Code:
IPAddress = Array("10.10.1.254")
Subnet = Array("255.255.252.0")
strComputer = "."
NICIndex = 7
Set objAdapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & NICIndex)
If IsDHCPWorking Then
'DHCP is enabled, so quit
WScript.Quit
Else
'DHCP not enabled - set static IP
objAdapter.EnableStatic(IPAddress,Subnet)
'Now Reset your modem
ResetModem
'Wait your 15 seconds
WScript.Sleep 15000
'Now re-enable DHCP and renew lease
objAdapter.EnableDHCP()
objAdapter.RenewDHCPLease()
End If
Function IsDHCPWorking
Dim objWMIService, bolDHCP
bolDHCP = False
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Index=" & NICIndex)
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
If Left(IPConfig.IPAddress(i),3)="169" Then
bolDHCP = True
End If
Next
End If
Next
' return the value of the function, true or false
IsDHCP = bolDHCP
End Function
Sub ResetModem
Dim obj
Set obj = CreateObject("WScript.Shell")
obj.run"cmd"
WScript.Sleep 500
obj.SendKeys"telnet 192.168.100.1"
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet username
obj.SendKeys("{Enter}")
WScript.Sleep 500
obj.SendKeys"root" 'telnet password
obj.SendKeys("{Enter}")
obj.SendKeys"reset"
obj.SendKeys("{Enter}")
Wscript.Sleep 500
End Sub
Ok I'll definitely read up on some of it. But that script doesn't work because of some parentheses.
-
June 8th, 2015, 03:05 AM
#11
Re: Restart Modem script help
Then remove them...
Code:
objAdapter.EnableStatic IPAddress,Subnet
-
July 5th, 2015, 10:12 PM
#12
Re: Restart Modem script help
Ok I have tried to get this to work and it doesn't seem to solve the problem. So I'm trying something new.
I am trying to build a script that does the following:
*query the current MAC address
*telnet into the modem and run the command "cm_hal\cpe_del XX:XX:XX:XX:XX:XX
*exit telnet (if there is a way)
*randomly change MAC Address on PC (This is the script I use now for that https://airvpn.org/topic/11571-scrip...om-mac-address)
Thanks
Brad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|