Thursday, October 30, 2008

Disconnect GPRS connection if your NIC is connected

This is a VBScript I created some time ago. It will connect your GPRS connection if your NIC is not connected and disconnect the GPRS connection if the NIC is connected. It will work with other dial-up connections as well.

The script will run as a service with SrvAny and will reconnect the GPRS connection if the connection was dropped. I'm using WMI to determine the status of the LAN connection. For some reason I did not find the correct status of the GPRS connection in WMI and needed to use the output of ipconfig to determine if GPRS was still up.

Dim Shell, Hell, GPRS, ExecuteObj, strcmd, GPRSConnected

On error resume next

Set Shell = CreateObject("WScript.Shell")
strComputer = "."
GPRS = "0"

Do
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter Where NetConnectionID = 'Local Area Connection'")

For Each objItem in colItems
If objItem.NetConnectionStatus = 7 Then
If GPRS = "0" Then
' Wscript.Echo "LAN connection disconnected!"
Shell.Run ("rasdial GPRS"), 0
GPRS = "1"
End if
Else
If GPRS = "1" Then
' Wscript.Echo "LAN connection connected!"
Shell.Run ("rasdial GPRS /DISCONNECT"), 0
GPRS = "0"
End if
End if
Next

If GPRS = "1" Then
Set ExecuteObj = Shell.Exec("ipconfig.exe")
Do While Not ExecuteObj.StdOut.AtEndOfStream
strcmd = ExecuteObj.StdOut.ReadLine()
If Instr(strcmd, "PPP") > 0 Then
GPRSConnected = "1"
End if
Loop
If not GPRSConnected = "1" Then
' Wscript.Echo "GPRS connection disconnected!"
Shell.Run ("rasdial GPRS"), 0
End if
Set ExecuteObj = nothing
Set GPRSConnected = nothing
End if

Wscript.Sleep 10000

Loop Until Hell="Freezes over!"

Automate HTML form submission

I needed to make a large amount of changes that would require me to open a form, fill it in, submit it and repeat this hundreds of times. I really did not have the time to do this so I needed a quick way to automate this process.

First let's take a look at part of the form I needed to fill in:

<form method="POST" action="/cgi-bin/script.cgi">
<input type="radio" name="brand" value="product1">Product 1
<input type="radio" name="brand" value="product2">Product 2
<td>User: </td>
<td><input type="text" name="username" size="20" id="fp1"></td>
<input type="submit" value="Submit" name="submit">

So there's a radio button to select a product and a textbox to submit a username. The data is then posted to the script.cgi script. Now, how would we automate the process to fill this form from a script? I chose to use cURL to submit the data to the script like this:

curl -d "brand=pin&username=product1&username=user&submit=Submit" http://myserver.net/cgi-bin/script.cgi

The parameter -d tells cURL to HTTP post data. The data is specified between the quotes, first a value name and then the value data. Different values are seperated by the & character. Remember to figure out what part triggers submission of the code and to add this value as well. In my case "submit=Submit".

Now all you need to do is use your favorite method to call cURL and enter the correct data between the quotes. I used Bash and chose to read the data from CSV files.

One more thing: I needed to post a slash in one of the values ("server.net/user") and got strange results. As it turned out I needed to escape the data like this: "server.net%2Fuser". You can find a complete list of HTML escape characters here.

Monday, October 27, 2008

Sync Windows Mobile with multiple Exchange servers

It's been a long time since I updated my blog. My new job keeps me busy I guess.

I needed to sync my Windows Mobile smartphone to the Exchange server of my employer and the server of the customer I work for. WM cannot sync to multiple Exchange servers out of the box. Also, I didn't like the security settings that came with the ActiveSync connection to one of the Outlook WebAccess servers. Also I did not want to sync my personal appointments to either server.

I've found a very nice tool that would run on your smartphone and sync to the OWA server: Chronobis. This tool gets the calendar and contact information from your OWA server and syncs your phone, either one way or bi-directional.

Chronobis only supports one Exchange server so I needed to find a way to sync my Outlook calendar to the phone. As I'm running Outlook in a virtual machine I was not able to use ActiveSync and a cable.

For now I sync Outlook with a seperate Google Calendar using Google Calendar Sync. Then I use ActiveGCSync to sync my phone with Google Calendar. This works but of course my PC needs to be on to do the syncronisation between Google Calendar and Outlook.

If this all sounds to complicated, you might take a look at http://www.scheduleworld.com/. They give the option to sync multiple sources. For me that was not an option as one of the OWA servers is not publicly available.

Until my next post: goodbye!