I have a script written in VBS that checks every second if the LAN port has a connection and if so, disables the wireless, or enables the wireless if no LAN connection exists. There's an unfortunate bug in this that leaves the wireless disabled if you shut down the computer while this script is running that I would like to fix. The script runs at logon via a GPO, so it won't run if one logs in off the network.
Is it at all possible to catch the End Task signal and perform cleanup operations before the script ends? I'd like for this to re-enable the wireless as the system shuts down.
Thanks
No i think it is not possible and if so it would be unreliable or slow down your pc i'm afraid. But just as you can have a script running at logon you can also have a script running at logoff, i suggest you take that road.
Related
My college requires students to periodically authenticate for using WiFi and LAN. I am writing a Python script that will automatically do that so that I don't have to manually enter my credentials. The authentication is also separate for WiFi and LAN, and that makes me enter my credentials when I switch between them. So, for the python script, I want to detect when my authentication has expired and my connection is disconnected.
I also don't want the python script to be running constantly in the background and pinging a website as that really isn't optimal and I'll have to run the script every time my PC restarts. I was thinking of using the Windows Task Scheduler to fire the script when it detects that my connection is lost. The trigger event cannot be fixed intervals as the connection can be lost in between the intervals and also when switching between LAN and WiFi.
So, is there any network event that will capture the functionality I want? As Windows gives a notification of "opening the browser to connect" I feel there has to be a background event running.
I tried the NetworkProfile/Operational Event in the Task Scheduler with event id 10001 and 8003. But that just fires when I switch off the WiFi of my PC.
Thank you
Got it!
NetworkProfile/Operational Event with ID 4002 waits for network authentication.
There doesn't seem to be any easy way to tell Windows 7 that a network connection should allways use a VPN connection (like it is in Linux), so I've been playing around a bit with Task Scheduler to do this.
What I need is to automatically connect to a VPN when a WLAN connection is established. Why? Because if I connect to the VPN manually, every time I put the computer to sleep or hibernate it, the VPN is disconnected and I have to connect manually again. Which is easy to forget.
In Task Scheduler, I've created the following:
Triggers:
On an event -> Log: System, Source: Power-Troubleshooter, Event ID: 1
On an event -> Log: System, Source: Kernel-Power, Event ID: 42
(I found the above in a tutorial, and they seem to work.)
Action: rasdial
This works. When running the task manually, it connects to the VPN.
However, the problem is that when logging in after sleep/hibernation, the task is run immediately. Hence, it runs before the WLAN connection is established, thus failing and quitting in a couple of seconds.
So, what I need is to delay the task for a few seconds, until the WLAN connection is established. Either for a specified number of seconds, or better - wait for the WLAN connection to be established.
Is there any way of doing this, without creating a script file with a wait function? The task can be set to retry if it fails, but only with 1 minute intervals, which is much too long.
If the only solution is to create a script file for starting the VPN connection, and adding a wait function there, could someone show me an example? (I'm not good at windows scripting at all.)
I don't think there's a way to do that without scripting it. At least not that I know of (which doesn't necessarily mean very much).
If you want to script it, here's a simple .vbs script that waits before running the rasdial command:
WScript.Sleep 5000
Set objShell = "WScript.CreateObject("WScript.Shell")
objShell.Run """c:\windows\system32\rasdial.exe"" <vpn connection> <username> <password>", 1, True
Set objShell = Nothing
Then just run this script file instead of the rasdial.exe command from the Task Scheduler.
I'm working with an embedded system which has a RAS entry already set up, using the API function RasDial from rasapi32.dll.
All works well except if something goes wrong after RasDial and before RasHangUp. In this case any further attempt to dial is met with error 756 "connection is being dialled", whether the dial attempt is done via the API or via the Windows rasdial command line utility.
rasdial connectionname /d doesn't help either.
The com port used for the modem is locked.
The only way to recover is to reboot.
Obviously under normal circumstances the solution is to make sure that RasDial is always followed by RasHangUp. But for cases where this doesn't happen, is there a way of aborting the dial attempt? For example, if the app calls RasDial and then crashes, how do I get out of that other than by rebooting?
Unfortunately, unless your application can properly terminate the connection that's in progress before exiting the RAS state machine becomes corrupted and must reboot to fix the problem. I've noticed that Windows 7 handles these sorts of scenarios better than XP and Vista did, but there are still occasions when I've had to reboot.
I've managed to prevent most of these sorts of problems with the DotRas API as long as they're occuring in the event handlers of the RasDialer, but if the application crashes from another thread and not from the background thread which raises the RasDialer events, there's nothing I can do about that.
For asynchronous dialing using the DotRas 1.2 SDK:
using DotRas;
RasDialer dialer = new RasDialer();
dialer.EntryName = "My Connection";
dialer.Credentials = new NetworkCredential("My", "User");
dialer.DialAsync();
From this point you can call dialer.DialAsyncCancel() if you want to cancel the connection attempt that's in progress.
For synchronous dialing using the DotRas 1.2 SDK is very similar to asynchronous dialing other than replacing the DialAsync call with simply dialer.Dial().
Here's a link to the API I was talking about: http://www.codeplex.com/DotRas
Hope that helps!
I have a test driver program that launches a separate test server process. The test server process listens on a local port, and after it's ready, the test driver runs a test that accesses the test server.
Currently the test driver repeatedly tries to connect to the local port (loop some, sleep some, try again). It's not an optimal solution, and is clearly unreliable.
Is it possible to wait for some event that says "somebody listens on a local port"? Trying to connect to early results in a "port closed" error.
I'd like to implement the solution on Windows, Linux, and Mac OS X. If you have some tips for any of these systems, it's welcome (it's probably going to be system-specific in each case).
On Windows I use a named event for this kind of thing.
The test harness can create the event and communicate the name of the event to the server that it launches; it then waits on the event to be signalled before continuing the test. The server then connects to the event, initialises itself and once it's ready to accept connections it signals the event.
Well, if you launch the server process, you can intercept the stdout of the server right?
So have the server output "server started" when the socket ready. The driver should wait until the server sends this string to stdout, then try to connect to the server port.
I am trying to simulate a scenario where connection to the server of one process is down while the connection to another server is up. Just pulling the network cable won't work in my case since I need another process connection to stay up.
Is there any tool for this kind of job? I am on Windows. Thanks!
There's a few layers which you can simulate this at. The easiest would be if your two servers listen on two distinct TCP ports. In that case, you could run two tcp proxies, and stop/pause one when you want to simulate a failure. For Windows I would suggest using tcpTrace to do this.
Another option would be to have the two servers bound to two virtual NICs, which are bridged to the physical NIC. Of course if you have two physical NICs, you could bind each server process to a different physical NIC.
At a lower level, you can ran a WAN simulator. Most simulators allow you to impair specific types of traffic or specific ports. One such simulator is Packetstorm.
One other method which I would suggest is attaching a debugger to one process, and halting all threads on the process with the debugger. Often, a process doesn't die, but gets stuck in garbage collection, or in a loop. As the sockets don't close, many 'high availability' solutions won't automatically failover.
One approach would be to mock the relevant network connection code for the purposes of testing. In this case you would probably want to mock it returning whatever it usually would if the connection was down.
A poor man's approach if you can use sleep/hibernate mode on your machine :
Set an Outbound rule in the Windows Firewall to disallow connection for a particular Program.
Already connected sockets stay connected: put the machine in sleep/hibernate mode for a brief moment to force those sockets to disconnect.
When the system is restored, the program cannot establish new connections.
New connections are made possible as soon as you disable the firewall rule.
Note that it does not simulate network outage because each connection fails immediately with an permission error. But it prevents a process to establish connections.