Author

Topic: .BAT to reset network upon failure (Read 880 times)

legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
June 20, 2017, 04:12:28 PM
#9
beta update;  much cleaner.  Works for me, waiting for secondary feedback before replacing original code.
legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
June 18, 2017, 03:18:01 PM
#8
Awesome work sir!!

I tweaked the netReset.bat file to restart my native windows VPN using rasdial Smiley
Code:
powershell -Command "rasdial VPNNAME USERNAME PASSWORD"
Now my mining system will only ever mine when my VPN is connected, and if the VPN fails it gets restarted Cheesy

I tried to script this and it was very weird.  When I coded the resharing of the internet, an adapter name that was not in my list of hardware was attached to the sharing and it needed a restart to get its info straight.  Its not that simple as it is mentioned by the OP but still it is worth the try.

cluit:  Ill see if I can add a script section for you to check if the ethernet port is down, and if so, disable ICS until the port is up... would that work?   But, see below first and try it;  the static IP on the xbox will probably fix your woes.

When I finally get to getting a good linux build on one of my cellular laptops, Ill be able to see if this whole issue with data dropping off is indeed linked to microsnot ICS.

*to everyone*:
if you ever use win ICS and remove an adaptor (say a USB one) there will be links left in the OS for that ICS connection.... Im wondering if that was your problem.

Try naming the adaptor you wish to reset (the one bearing the internet connection to be shared) to something unique, and try again.

For other issues I have been having, set static IP's and a known DNS in the adaptor properties for the device receiving ICS connections;  

I.E. Device 1
192.168.137.51
255.255.255.0
192.168.137.1

(I have a second 100/1000 port on my first machine accepting the ICS shared connection, so my connections are bridged so machine #2 gets this info:

I.E. Device 2
192.168.137.61
255.255.255.0
192.168.137.1

dns 8.8.8.8/8.8.4.4)


FYI, you can NOT use 2 tier ICS.

I.E.

Machine 1 with internet; using ICS to share to machine #2, which has ICS sharing to machine #3.  This will NEVER work.  you would need to set up your own standalone DHCP server and config to do NAT within NAT in windows.
legendary
Activity: 1176
Merit: 1016
June 17, 2017, 06:45:42 PM
#7
Awesome work sir!!

I tweaked the netReset.bat file to restart my native windows VPN using rasdial Smiley
Code:
powershell -Command "rasdial VPNNAME USERNAME PASSWORD"
Now my mining system will only ever mine when my VPN is connected, and if the VPN fails it gets restarted Cheesy

I tried to script this and it was very weird.  When I coded the resharing of the internet, an adapter name that was not in my list of hardware was attached to the sharing and it needed a restart to get its info straight.  Its not that simple as it is mentioned by the OP but still it is worth the try.
legendary
Activity: 1190
Merit: 1024
June 17, 2017, 06:20:55 PM
#6
Taking a look.  I have a room, non-Bitcoin related, that has a WiFI signal coming in, a PC and an Xbox One.  The Xbox One hates the Wifi, so I grab the Wifi with the PC and use a Cat6 to share the internet with the Xbox.  Sometimes restarting the xbox requires me to unshare the internet, reshare the internet and reset the adapter leading to the CAT6, basically I assume that it is a DHCP issue with the PC handing out an IP for the Xbox. 
legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
June 14, 2017, 01:41:09 PM
#5
Updated code.   Added counters and display for the data.  Also streamlined and trimmed wait counters to a more respectable level.

Seems to keep my cellular connection in-check reliably.

Soon a proper shutdown step to openVPN will be coded in, but I am still working out the bugs.

More TBA.
legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
June 03, 2017, 09:23:54 AM
#4
Just a note to openVPN users:  taskkilling the openVPN service leaves the TUN devices stuck in the OS until it is properly closed out.  There is no built in "command line" style function to reconnect or disconnect;  only connect.    This is a really big upset to me...

I have heard there are ways to send a command to the process itself.... but going through a batch... I will have to find some powershell trickery or something to make this possible.

For now;  it does work... but you end up with some odd errors in the openVPN GUI status readout having to do with an inaccessible windows path....

Ill be updating this more and trying to make it more dynamic...   I have a laundry list of coding to do in the next week and this should be on my list for sure.
legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
May 10, 2017, 12:12:32 AM
#3
You are very welcome.

I myself am enjoying seeing no dips in my hashrate anymore. Smiley

Thanks for posting the your edit; may help others as well.

This is a huge step towards my "set and forget" gpu mining "satellite stations" plan a friend and I are putting together.
sr. member
Activity: 349
Merit: 250
May 09, 2017, 08:47:14 PM
#2
Awesome work sir!!

I tweaked the netReset.bat file to restart my native windows VPN using rasdial Smiley
Code:
powershell -Command "rasdial VPNNAME USERNAME PASSWORD"
Now my mining system will only ever mine when my VPN is connected, and if the VPN fails it gets restarted Cheesy
legendary
Activity: 1848
Merit: 1165
My AR-15 ID's itself as a toaster. Want breakfast?
May 09, 2017, 05:29:05 PM
#1
I have had a personal internet issue;  My main location is out in the boonies, which only allows for cellular high speed.  Next choice was satellite, which is plainly never gonna happen....  So, cellular it is.

Now, it works all fine and dandy, but when the area gets flooded with traffic (interstate backed up for instance), the connection seems to timeout and stay unresponsive until I disconnect, then reconnect the cellular adaptor from the cellular data network.  Which, in-turn, requires me to disconnect my VPN service (openVPN) before and reconnect it after the cellular disconnect as well.

Over time this has pointed me towards problems with connection handling at my local node.    So;  how did I remedy this?  Same as everything else in my life.....  a batch file.  If I were working for the cellular provider;  I'd fix the actual problem.  Till it's fixed, I have this.

They belong in the root of C:\     Yes;  Because admin priv is necessary anyways.
Requires Windows Powershell. (Typically built-in to windows)

BETA TEST VERSION Re-arranged some things.  Changed disconnect/reconnect as to require only one file for the whole batch.  Now there is a powershell script for people with openVPN that properly and cleanly shuts down the connection and application.  This is for inactive systems;  as there is a "foreground sent" enter key by the powershell script to close a dialogue.  The powershell script can be omitted, and your own VPN string inserted in place if you don't use openVPN (see a post comment just a few below the top post for an example).  So far tested on Win7 x64 and Win 10 x64.
C:\rebooter.bat
Code:
@ECHO OFF
SET OVERALLCOUNT=0
SET FAILEDPINGS=0
SET COUNT1=0
SET COUNT2=0
SET COUNT3=0
SET COUNT4=0
SET COUNT5=0
SET COUNT6=0
SET COUNT7=0
SET C1=47.35.23.146
SET C2=www.google.com
SET C3=www.microsoft.com
SET C4=www.facebook.com
SET C5=www.myspace.com
SET C6=sfbay.craigslist.org
SET C7=garlic.com
SET REBOOTCOUNT=0
GOTO FIRSTSTART
:START
SET /A OVERALLCOUNT=%OVERALLCOUNT%+1
:FIRSTSTART
CLS
ECHO Ping loops since reboot:    %OVERALLCOUNT%
ECHO Failed pings since reboot:  %FAILEDPINGS%
ECHO Total number of netReboots: %REBOOTCOUNT%
ECHO.
ECHO Total Ping Breakdown:
ECHO.
ECHO %COUNT1% : %C1%
ECHO %COUNT2% : %C2%
ECHO %COUNT3% : %C3%
ECHO %COUNT4% : %C4%
ECHO %COUNT5% : %C5%
ECHO %COUNT6% : %C6%
ECHO %COUNT7% : %C7%
choice /c q /n /t 60 /d q
ping %C1% -n 1 -w 5000
SET /A COUNT1=%COUNT1%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C2% -n 1 -w 5000
SET /A COUNT2=%COUNT2%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C3% -n 1 -w 5000
SET /A COUNT3=%COUNT3%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C4% -n 1 -w 5000
SET /A COUNT4=%COUNT4%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C5% -n 1 -w 5000
SET /A COUNT5=%COUNT5%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C6% -n 1 -w 5000
SET /A COUNT6=%COUNT6%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C7% -n 1 -w 5000
SET /A COUNT=%COUNT7%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C1% -n 1 -w 5000
SET /A OVERALLCOUNT=%OVERALLCOUNT%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
:JUMP
ECHO Internet connection down, Restarting cellular adaptor...
SET /A REBOOTCOUNT=%REBOOTCOUNT%+1
SET OVERALLCOUNT=0
SET FAILEDPINGS=0
powershell -executionPolicy bypass -file "C:\closeVPN.ps1"
ipconfig /flushdns
netsh interface set interface "Cellular" DISABLED
netsh interface set interface "Cellular" ENABLED
ECHO Restarting VPN Service...
START c:\Progra~1\OpenVPN\bin\openvpn-gui.exe --connect JaredKaragen.ovpn
ECHO VPN Service Started.
choice /c q /n /t 5 /d q
GOTO START

C:\closevpn.ps1
Code:
$sig = @"
        [DllImport(`"user32.dll`")]
           public static extern IntPtr FindWindow(string className, IntPtr useZero);
        [DllImport(`"user32.dll`")]
           public static extern int SendMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
        [DllImport(`"user32.dll`")]
           public static extern void keybd_event(Byte bVk, Byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);
        [DllImport(`"user32.dll`")]
           public static extern int PostMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);
"@
$win32_methods = Add-Type -MemberDefinition $sig -Name "Win32FindWindow" -Namespace Win32Functions -PassThru
$h = $win32_methods::FindWindow("OpenVPN-GUI", 0)
$win32_methods::PostMessage($h, 0x10, 0, 0)
sleep 1
##### simulate enter key down VK_ENTER = 0xD, scan code = 0x9c
$win32_methods::keybd_event(0xD, 0x9c, 0, 0)
##### simulate enter key up: for this change the flags to KEYEVENTF_KEYUP = 0x2
$win32_methods::keybd_event(0xD, 0x9c, 0x2, 0)





*UPDATED 6/14/2017*
 Cleaned up code and added counters with display.  Updated timer values and streamlined the redundancy.
C:\netWatch.bat
Code:
@ECHO OFF
SET OVERALLCOUNT=0
SET FAILEDPINGS=0
SET COUNT1=0
SET COUNT2=0
SET COUNT3=0
SET COUNT4=0
SET COUNT5=0
SET COUNT6=0
SET COUNT7=0
SET C1=10.8.0.1
SET C2=www.google.com
SET C3=www.microsoft.com
SET C4=www.facebook.com
SET C5=www.myspace.com
SET C6=sfbay.craigslist.org
SET C7=garlic.com
SET REBOOTCOUNT=0
GOTO FIRSTSTART
:START
SET /A OVERALLCOUNT=%OVERALLCOUNT%+1
:FIRSTSTART
CLS
ECHO Ping count since reboot:    %OVERALLCOUNT%
ECHO Failed pings since reboot:  %FAILEDPINGS%
ECHO Total number of netReboots: %REBOOTCOUNT%
ECHO.
ECHO Total Ping Breakdown:
ECHO.
ECHO %COUNT1% : %C1%
ECHO %COUNT2% : %C2%
ECHO %COUNT3% : %C3%
ECHO %COUNT4% : %C4%
ECHO %COUNT5% : %C5%
ECHO %COUNT6% : %C6%
ECHO %COUNT7% : %C7%
choice /c q /n /t 60 /d q
ping %C1% -n 1 -w 1000
SET /A COUNT1=%COUNT1%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C2% -n 1 -w 1000
SET /A COUNT2=%COUNT2%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
choice /c q /n /t 5 /d q
ping %C3% -n 1 -w 1000
SET /A COUNT3=%COUNT3%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
choice /c q /n /t 5 /d q
ping %C4% -n 1 -w 1000
SET /A COUNT4=%COUNT4%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
choice /c q /n /t 5 /d q
ping %C5% -n 1 -w 1000
SET /A COUNT5=%COUNT5%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
choice /c q /n /t 5 /d q
ping %C6% -n 1 -w 1000
SET /A COUNT6=%COUNT6%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
choice /c q /n /t 5 /d q
ping %C7% -n 1 -w 1000
SET /A COUNT=%COUNT7%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ping %C1% -n 1 -w 1000
SET /A OVERALLCOUNT=%OVERALLCOUNT%+1
if not errorlevel 1 (GOTO START)
SET /A FAILEDPINGS=%FAILEDPINGS%+1
ECHO Internet connection down, Restarting cellular adaptor...
SET /A REBOOTCOUNT=%REBOOTCOUNT%+1
SET OVERALLCOUNT=0
SET FAILEDPINGS=0
START C:\netReset.bat
choice /c q /n /t 120 /d q
GOTO START

C:\netReset.bat
Code:
@ECHO OFF
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq openvpn-gui.exe"') DO IF %%x == openvpn-gui.exe TASKKILL /F /IM openvpn-gui.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq openvpn.exe"') DO IF %%x == openvpn.exe TASKKILL /F /IM openvpn.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq openvpnserv.exe"') DO IF %%x == openvpnserv.exe TASKKILL /F /IM openvpnserv.exe
choice /c q /n /t 1 /d q
powershell -Command "Restart-NetAdapter -Name "Cellular""
ECHO Restarting VPN Service...
START c:\Progra~1\OpenVPN\bin\openvpn-gui.exe --connect JaredKaragen.ovpn
ECHO VPN Service Started.
EXIT

To create these files, open a command prompt window with admin rights,  go to the root of "C:\" , and type "notepad netWatch.bat".  When notepad loads it will tell you that the file is not found and ask if you want to create it.... this is how.   Be sure you do not save it as a text file.

To run netWatch.bat, and netReset.bat, you need admin privileges.  
You must create a .lnk to a random file;  and point that new .lnk at the batch file "netWatch.bat", and in "Advanced options" check the box to have it "Run as Administrator".  There is no way to run this powershell command without admin priv.

Use this new .lnk to run netWatch.bat

I had several crazy issues I can not explain, which required me to run the batch in a very strange way (one batch triggering another) because when it would reach the end of the loop (after using powershell), powershell didn't seem to ever close cleanly and it would leave me back at a prompt;  instead of looping to the start of the code.   Thus the reason it looks like that.  All good, it works still.

To configure this for your adaptor, and visit the "Network: Change Adaptor Settings" window that lists the active and inactive network adaptors.   Give it a custom name now or use the current one, just know it's exact name.  
Mine in the batch is "Cellular".  
Sometimes it's "Ethernet 2", "Wireless" etc....

if you don't use openVPN... I suggest commenting out those lines via adding "REM " in front of those lines in netReset.bat or removing them.  I can help you if necessary.

You can see how the primary batch pings multiple different locations, and if there's a valid response along the way, it starts over and waits to check again.
You can use your own choices for IP addresses/domain names.

All in all, this should be a pretty clean template for making a network adaptor reset tool.   It is sooooooo nice that my machine automatically fixes itself now when I am not home.    It has worked and done its job several times already...    I think times need to be lengthened... but there's some very strange behavior when my network adaptor needs reset.  sometimes one miner stays online; but I can't visit any webpages on it or use the internet on any other machine.... very strange stuff;  or no ping response yet it can still query DNS for the IP to ping....(and all DNS caching is removed on my entire network chain so its definitely not cached).  Its been a pain; but doesn't happen often.... usually...
Jump to: