topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Monday March 18, 2024, 11:26 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Turn a batch file into an AHK script.  (Read 55623 times)

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Turn a batch file into an AHK script.
« on: April 29, 2013, 07:16 AM »
Ever since I started using Windows 7 one of my dislikes was that there was no way to show an icon in the system tray to tell if a PPTP/L2TP VPN was connected, unlike in XP.
Having sorted out most of the other things I wanted to change I have come back to the issue and think that I have found a solution but I need some help to finalise it.

I have written a batch file that enables me to toggle individual PPTP VPN's on and off but that still leaves me without an icon.
My idea is to create an AHK script to do the same job, as far as I can see it is not possible to change the icon on an AHK script which leads me to the possibility of converting it to an EXE.

The batch file:

if exist pptp-running.txt goto running
rem prog is not running
copy NUL>pptp-running.txt
start /D "C:\Batch Files\PPTP ON-OFF" PPTP-US-ON.bat
goto alldone
:running
rem prog is already running
del pptp-running.txt
start /D "C:\Batch Files\PPTP ON-OFF" PPTP-OFF.bat
:alldone

Can this be converted to an AHK script and if compiled will it still toggle on alternate triggers?
If not can anybody suggest an alternative?
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

c.gingerich

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 748
    • View Profile
    • The Blind House
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #1 on: April 29, 2013, 08:00 AM »
So do you want an app that does the same as above but you have access to it from the system tray?

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #2 on: April 29, 2013, 08:19 AM »
The idea is to trigger something with a hotkey that will enable the VPN and put an icon in the system tray.
Trigger it a second time from the same hotkey and it will disable the VPN and remove the icon.

It would be useful if the icon only appeared once the connection is established but that is not essential as all my connections are set to reconnect if they drop out anyway.

The reason I was looking at an executable was to give me a choice of icons but I would need the script for it as I use numerous VPN's and they all require their own batch files so the script would be different for each one.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #3 on: April 29, 2013, 08:35 AM »
A simple AutoIt script, seems to work here, you may need to change the batch file folder and/or names if I got it wrong.

I'm sure you could think of something better to call it:

Code: AutoIt [Select]
  1. ; ToggleIcon
  2.  
  3. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  4. #AutoIt3Wrapper_Icon=OnOff.ico
  5. #AutoIt3Wrapper_UseUpx=n
  6. #AutoIt3Wrapper_Res_Icon_Add=On.ico
  7. #AutoIt3Wrapper_Res_Icon_Add=Off.ico
  8. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  9.  
  10. #include <Constants.au3>
  11. #include <GUIConstantsEx.au3>
  12. #include <WindowsConstants.au3>
  13. #include <StaticConstants.au3>
  14.  
  15. $flag = "C:\pptp-running.txt"
  16.  
  17.         TraySetIcon(@ScriptName, -6)
  18.         TraySetIcon(@ScriptName, -5)
  19.  
  20.         $msg = TrayGetMsg()
  21.         Switch $msg
  22.                 Case $TRAY_EVENT_PRIMARYUP
  23.                         _Toggle()
  24.                 Case Else
  25.                         ContinueLoop
  26.         EndSwitch
  27.         Sleep(500)
  28.  
  29. Func _Toggle()
  30.         If Not FileExists($flag) Then
  31.                 $file = FileOpen($flag, 2)
  32.                 FileClose($file)
  33.                 Run(@ComSpec & " /c " & '"C:\Batch Files\PPTP-US-ON.bat"', "C:\Batch Files", @SW_HIDE)
  34.                 TraySetIcon(@ScriptName, -5)
  35.         Else
  36.                 FileDelete($flag)
  37.                 Run(@ComSpec & " /c " & '"C:\Batch Files\PPTP-US-OFF.bat"', "C:\Batch Files", @SW_HIDE)
  38.                 TraySetIcon(@ScriptName, -6)
  39.         EndIf

Left-click the icon to toggle the connection, right-click to exit it via menu.

NOTE: This was knocked up before you mentioned hotkeys and different icons but it wouldn't be hard to change.

Changing icons is just a matter of changing the icon files and recompiling or you could probably go nuts and use a config file that specifies what images to use.
« Last Edit: April 29, 2013, 08:40 AM by 4wd »

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #4 on: April 29, 2013, 09:12 AM »
4wd,

Thanks for that.

I just tried the EXE but apart from an icon appearing nothing happened, I don't have AutoIt installed to modify the script.
Given my lack of programming understanding ('programmatically challenged is a good expression') I am trying to keep to AHK and batch files, they give me enough headaches. :(

My intention was to use a one of the Windows network icons and only have it show when the VPN is connected, the way it is on XP.
I could also vary it, perhaps by colour, depending on which VPN I was using.
In fact it just occurred to me that I could use the relevant country flag for each VPN's icon.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #5 on: April 29, 2013, 09:11 PM »
I just tried the EXE but apart from an icon appearing nothing happened, ......

That's exactly what should happen when you run it.......then it just sits there until you click the LMB on it at which point it goes and runs your batch file and the icon changes colour.

Simples  :)

I am trying to keep to AHK and batch files, they give me enough headaches. :(

Fair enough, I find the syntax used in AHK is too strange to get to grips with :)

My intention was to use a one of the Windows network icons and only have it show when the VPN is connected, the way it is on XP.

So what you want is something along the lines of the OpenVPN GUI which provides an indication in the SysTray and allows you to easily switch between VPNs?

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #6 on: April 30, 2013, 11:48 AM »
Apologies for not working it out.

I just tried it again and the icon is flashing between different colours and a cross, I opened Network Connections and the VPN that this is set up for shows no indication of trying to connect.

I'll try and explain more clearly what I want to do, at the moment I have a batch file X that alternately triggers two other batch files ON and OFF.
The OFF file does not contain specific details so it will disconnect ANY PPTP/L2TP VPN that is connected when it is triggered.
The ON files contain the name of the VPN as well as username and password so each one is specific to a single VPN server, therefore I am going to need a different ON file for each VPN server, although I have no intention of creating files for all of them at the last count there were 40+.
This also means that I will need a separate X batch file for each one and that in turn means that I need separate scripts or apps to trigger the various X batch files as the path will be different for each one.

Moving on to icons, on XP both PPTP/L2TP VPN's use the same icon(s) as the main connection:

System32/netshell.dll

netshell_190.ico
netshell_191.ico
netshell_192.ico
netshell_193.ico

These icons are also in Windows 7 and they are the same ones as Network Activity Indicator uses, which I run in Windows 7 for the main connection.
The VPN icons need to be automatically removed from the system tray when not in use just as they are in XP.

At the moment I have shortcuts to the PPTP/L2TP VPN's that I most frequently use which make the connection but give no visible indication i.e. an icon.
Instead of pointing those shortcuts directly at the connections I want to be able to point them at individual apps that will trigger a specific VPN and put an icon in the system tray at the same time.
When I trigger them a second time I want the VPN to disconnect and the apps icon to disappear.
I do not want or need a GUI and the only option it would be useful to have in the icon would be to disconnect the VPN and exit the app at the same time without going back to the shortcut, which is not really necessary.
Except on very rare occasions whatever VPN I connect to when I start for the day I stay on so being able to switch between them is not a consideration while connecting/disconnecting them is.

If I can get a working AHK script that I can edit and compile I can change all the variables to suit the different VPN's myself.

Just to finish on your last point, all my OpenVPN VPN's come with their own software and none of them use the OpenVPN GUI, when I am not using any of them I disable the TAP-Win32 adaptor and there is nothing showing in the system tray.

The main issue for me from the beginning is that by default in Windows 7 there is no visible indication when a PPTP/L2TP VPN is connected, to have an icon in the system tray that is synchronised with the VPN: connected - icon visible, disconnected - icon not visible, is all I am really interested in and the only way that I can see to do that is with an app because the connections themselves have no icons.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #7 on: April 30, 2013, 11:24 PM »
I just tried it again and the icon is flashing between different colours and a cross, I opened Network Connections and the VPN that this is set up for shows no indication of trying to connect.

If it's flashing between an icon and a red cross that means the script is paused, ie. you clicked the RMB on it and the default AutoIt action is to bring up the Script Paused/Exit menu - just right-click it again and choose Script Paused to unpause.

The ON files contain the name of the VPN as well as username and password so each one is specific to a single VPN server, therefore I am going to need a different ON file for each VPN server, although I have no intention of creating files for all of them at the last count there were 40+.
This also means that I will need a separate X batch file for each one and that in turn means that I need separate scripts or apps to trigger the various X batch files as the path will be different for each one.

It sounds more like you need a decent Network Connection Manager but let's see if I can reduce the number of batch files required without doing much of anything :)

The way you've described it you need one "X" batch file for every ON batch file, is this correct?

The "X" batch file is there for the sole purpose of setting/deleting a flag file and choosing which of the following two, (ON or OFF), batch files to run based on the (non)existence of the flag file.

However, if you move the conditional test to the ON and OFF batch files there's no need for the "X" batch files at all.

Now you only need one ON batch file for each connection and the OFF batch file.

eg. The first few lines of each ON batch file would be:

Code: Text [Select]
  1. if not exist pptp-running.txt goto startvpn
  2. REM prog is already running
  3. start /D "C:\Batch Files\PPTP ON-OFF" PPTP-OFF.bat
  4. goto alldone
  5. :startvpn
  6. REM prog is not running
  7. copy NUL>pptp-running.txt
  8.  
  9. VPN connection commands
  10.  
  11. :alldone

For the OFF batch file, (PPTP-OFF.bat), the first few lines would be:
Code: Text [Select]
  1. if not exist pptp-running.txt goto alldone
  2. del pptp-running.txt
  3.  
  4. VPN disconnection commands
  5.  
  6. :alldone

Running any ON batch file with an already established connection would cause disconnection, so in effect every ON file becomes an OFF file if there is a connection.

If we add in a little program, (see below), that throws an icon into the SysTray when run and optionally runs the OFF batch file when clicked then all you require would be a program launcher, (Launchy, FARR, etc), to choose which ON batch file you want to run.

Would not this be easier?

NetConnected is a small program that will put an icon into your SysTray and then just sit there.  It takes one optional argument, a file to run when you click on it with the LMB or choose Exit from its menu - the file will then execute and NetConnected will exit.

eg. NetConnected.exe "C:\Batch Files\PPTP-OFF.bat"

If you run NetConnected without arguments then it will terminate any previous running instance then exit.  You can also rename the executable to anything you like, it will still work.  (I chose a generic network icon for it but if you want something else let me know - I might see if I can get it to change according to a supplied country code or icon.)

So your ON files would become:
Code: Text [Select]
  1. if not exist pptp-running.txt goto startvpn
  2. REM prog is already running
  3. start /D "C:\Batch Files\PPTP ON-OFF" PPTP-OFF.bat
  4. goto alldone
  5. :startvpn
  6. REM prog is not running
  7. copy NUL>pptp-running.txt
  8.  
  9. VPN connection commands
  10.  
  11. NetConnected.exe "C:\Batch Files\PPTP-OFF.bat"
  12. :alldone

And you could add it to the OFF file:
Code: Text [Select]
  1. if not exist pptp-running.txt goto alldone
  2. del pptp-running.txt
  3.  
  4. VPN disconnection commands
  5.  
  6. :alldone
  7. NetConnected.exe

Is that any better?
« Last Edit: May 02, 2013, 02:58 AM by 4wd »

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #8 on: May 01, 2013, 12:15 AM »
can we see the content of one of the 'on' batch files?

seems like it would be more logical to start from scratch instead of building a tool to control a tool to control a tool to control a tool...

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #9 on: May 01, 2013, 04:22 AM »
4wd,

I'll try the app and the batch file later but to pick up on some of your comments:

I unpaused the previous app but the VPN still showed no sign of connecting.

All my connections show up in the Network Connections window so I can connect/disconnect them from there and I can open that by right clicking on the icon for Network Activity Indicator which stays in the tray all the time, but I do not want to keep opening a window every time which is why I do not want an app with a GUI, nor does it provide an icon to show the VPN is connected.

Your idea about cutting out the X batch files makes sense to me and as I said I will try it later, remember that my understanding of these things is somewhat limited and I have been basically putting together various scripts that I have found online so it is almost certain that there is a more efficient way to do what I am trying to do.
It's like the question I asked at the end of Reply 7 in THIS thread, my only way of answering it will be by trial and error, at the moment each new type of batch file is taking me an average of 10 hours to sort out.

I have never tried to run a batch file with an already established connection but if I was trying to change connections in that way I would simply have to click it twice, the first time to disconnect the old connection (thats why I made the OFF file non-specific), the second time to connect the new one.

I already have a number of launchers for various things and I do not want to install another, the sole purpose of any additional program is to put an icon in the system tray to indicate the connection is active and then remove the icon when it is not.

I will try NetConnected as I said but if that leaves an icon in the tray when there is no VPN connected it is not what I want.
When there is no VPN running there should be nothing else associated with them running (i.e. an app) and nothing visible (i.e. an icon).


Target,

The ON files read as follows:

rasdial "VPN Name" "Username" "Password"


The image below shows the left hand end of my taskbar, the first 3 icons are in Quick Launch, the next 3 in an additional toolbar:

1.jpg

From left to right the shortcuts are as follows:

Quick Launch: Router Connection, TAP-Win32 adaptor, LAN.

Second Toolbar: EU VPN, UK VPN, US VPN. (All PPTP)

All I want to do is continue using the same PPTP shortcuts but with (A) the ability to toggle the connections, which I can now do with the batch files, and (B) put an icon in the system tray, (only) when a connection is 'on'.

I'll be back when I have tried out the latest suggestions.

Many thanks for the help by the way.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #10 on: May 01, 2013, 08:21 AM »
4wd,

I have tried all 4 batch files in your last post and they all worked except for one thing which I have resolved.
In the NetConnected ON file I had to add 'start' to the beginning of line 11 or the cmd window stayed open.

The good:

Using NetConnected puts an icon in the tray (ON) and removes it (OFF). (Terrible icon. ;D)

The bad:

If I hover the cursor over the NetConnected icon there is a 'Left-click to execute file and exit' option, what actually happens is that the icon disappears but the VPN stays connected.

If the VPN connection drops out the icon stays put, so there is no indication the the connection has dropped.

Batch file X is a toggle, when it is removed I need 2 trigger points (hotkeys, shortcuts) as opposed to 1.
An extra batch file is far less of an issue for me than an extra click.


I've taken the liberty of changing the NetConnected icon to netshell_193.ico to match the one in Network Connection Indicator.
In NCI as well as by default in XP the 4 netshell.dll icons change in relation to the traffic, which in numerical order is, 190 - in & out, 191 - in, 192 - out, 193 - none.
Without getting overly complicated is there a way of incorporating this in NetConnected?

The only other thing that I have noticed is that if I have the icon showing in the system tray and disconnect, when I reconnect the icon reappears far left in the system tray (XP default) rather than where it was before (7 default).
(For some reason this appears to have resolved itself, I think it might have had something to do with which programs were running when I was switching NetConnected on and off.)

All the testing I have done so far has been in Windows 7 but I am confident that once I finalise the details it will work in both.


With what I have now I can put together a working solution to what I was looking for.
All the issues I have mentioned above are relatively trivial and can either be resolved (reinstating the X batch file) or ignored (changing icons).
If you, or anybody else, can suggest ways of tidying things up in any way let me know and I'll give it a try.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi
« Last Edit: May 01, 2013, 08:30 AM by pilgrim »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #11 on: May 02, 2013, 02:57 AM »
If the VPN connection drops out the icon stays put, so there is no indication the the connection has dropped.

Half a dozen posts later it has all become clear - all you want is a network connection monitor, nothing to do with batch files at all really.  ;D

If I hover the cursor over the NetConnected icon there is a 'Left-click to execute file and exit' option, what actually happens is that the icon disappears but the VPN stays connected.

It's not an option, it's a ToolTip - what you're saying is that as soon as you hover over the icon you get the ToolTip and then the program exits without you doing anything further, (something it doesn't do on my machine).

Anyway, now that all that's required is a connection monitor it's back to the drawing board  ;)

Icon changed for NetConnected, download archive again.

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #12 on: May 02, 2013, 05:52 AM »
If the VPN connection drops out the icon stays put, so there is no indication the the connection has dropped.

Half a dozen posts later it has all become clear - all you want is a network connection monitor, nothing to do with batch files at all really.  ;D

If I hover the cursor over the NetConnected icon there is a 'Left-click to execute file and exit' option, what actually happens when clicked is that the icon disappears but the VPN stays connected.

It's not an option, it's a ToolTip - what you're saying is that as soon as you hover over the icon you get the ToolTip and then the program exits without you doing anything further, (something it doesn't do on my machine).

Anyway, now that all that's required is a connection monitor it's back to the drawing board  ;)

Icon changed for NetConnected, download archive again.

Mea culpa, mea culpa, mea maxima culpa. It's what comes of trying to think of too many things at once when you don't understand most of them in the first place.  :-[

When I started this there were three things that I was missing, the first was the cmd line commands to turn a VPN on and off, those I finally found after much searching online. The second was a way to toggle the VPN on and off from a single hotkey/shortcut, the batch files solved that. The third was to have an icon to indicate the VPN state, originally I was thinking only in terms of whether it was on or off, it was only later that I considered the possibility of it indicating traffic.

Last night it suddenly occurred to me that there was an obvious answer right in front of me, running a second instance of Network Activity Indicator, the first to monitor the main connection, the second to monitor a VPN. Unfortunately it is only possible to run a single instance of it and that monitors all connections. I have sent the developer an email to see what he thinks of the idea.

The first part of your reply highlights something I have noticed repeatedly when searching for things, it's not just about what you are looking for but the words you use to describe it.
In the last two months I have amassed a considerable amount of information about things I had never heard of before, devcon, rasdial and numerous others.
Rather than just copy and paste everything into text files I have added explanations that I can understand, which is more than I can say about many of the sites that information came from.

Going back to something I said yesterday, I am still suffering from wandering icons, this happened before when I was playing with batch files.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #13 on: May 02, 2013, 06:53 AM »
Simple connection monitor: ConnStat.exe

The simple part: an icon = connected; no icon = disconnected


ConnStat - Connection Status

ConnStat.exe <adapter> [delay]

where: <adapter >= NIC adapter name as given under Windows Network Connections, use quotes if necessary
            [delay]    = Optional delay before monitoring starts (in seconds)

eg.  ConnStat.exe "virtualbox host-only network" 30

Tray icon will change colour at the end of the delay period, (if given), to indicate monitoring has begun.

If the NIC given does not exist or it does not have a status of Connected, the program will exit.

NIC status will be checked every 1000 milliseconds.

ConnStat can be terminated by causing the NIC to disconnect, (eg. pull the cable, disable it, nuke the destination, etc), or choosing Exit from the icon context menu.

If the icon is flashing between its normal icon and a red cross it indicates the program is PAUSED - use the context menu to unpause.


Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Icon=netshell_193.ico
  3. #AutoIt3Wrapper_UseUpx=n
  4. #AutoIt3Wrapper_Res_Icon_Add=netshell_190.ico
  5. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  6.  
  7. #comments-start
  8.  
  9. ConnStat - Connection Status
  10.  
  11. ConnStat.exe <adapter> [delay]
  12.  
  13. where: <adapter >= NIC adapter name as given under Windows Network Connections, use quotes if necessary, it's case-insensitive
  14.             [delay]    = Optional delay before monitoring starts, (in seconds).
  15.  
  16. eg.  ConnStat.exe "virtualbox host-only network" 30
  17.  
  18. Tray icon will change colour at the end of the delay period, (if given), to indicate monitoring has begun.
  19.  
  20. If the NIC given does not exist or it does not have a status of Connected, the program will exit.
  21.  
  22. NIC status will be checked every 1000 milliseconds.
  23.  
  24. ConnStat can be terminated by causing the NIC to disconnect, (eg. pull the cable), or choosing Exit from the icon context menu.
  25.  
  26. If the icon is flashing between its normal icon and a red cross it indicates the program is PAUSED - use the context menu to unpause.
  27. #comments-end
  28.  
  29. #Include <Array.au3>
  30.  
  31. ; No command line args = message and exit
  32. If $CmdLine[0] = 0 Then
  33.         MsgBox(48, "ConnStat", "Command format:" & @CRLF & "ConnStat.exe <network connection> [delay]" & @CRLF & @CRLF & 'eg. ConnStat.exe "Local Area Connection 2" 30')
  34.         _Exit()
  35.  
  36. TraySetState()                                                                   ; Show tray icon
  37. $checkConn = $CmdLine[1]                                            ; arg 1 = NIC
  38. If $CmdLine[0] > 1 Then
  39.         If Int($CmdLine[2]) > 0 Then Sleep(Int($CmdLine[2]) * 1000)    ; arg2 = optional delay
  40. TraySetState(2)                                                                 ; Hides tray icon appears after delay, ie. monitoring starts
  41. TraySetIcon(@ScriptName, -5)                                         ; Change icon
  42. TraySetState(1)                                                                 ; Show tray icon
  43.  
  44.         $connNICs = _AdaptersConnected()                            ; get NICs with state "connected"
  45.         $i = _ArraySearch($connNICs, $checkConn, 1)            ; search NIC list for our adapter
  46.         If @error Then _Exit()                                                   ; exit if not found
  47.         Sleep(1000)
  48.  
  49.  
  50. Func _Exit()
  51.         Exit
  52.  
  53. ; Function by guiness @ http://www.autoitscript.com/forum/topic/134245-get-netconnection-status/#entry936095
  54. Func _AdaptersConnected($sComputer = @ComputerName)
  55.     Local $aReturn[1] = [0], $iDimension = 0, $oColItems, $oWMIService, $sDelimeter = Chr(01), $sReturn = ""
  56.  
  57.     $oWMIService = ObjGet("winmgmts:\\" & $sComputer & "\")
  58.     $oColItems = $oWMIService.ExecQuery("Select * From Win32_NetworkAdapter Where NetConnectionStatus='2'", "WQL", 0x30)
  59.  
  60.     If IsObj($oColItems) Then
  61.         For $oObjItem In $oColItems
  62.             If $oObjItem.AdapterType = "" Or $oObjItem.NetConnectionID = "" Then ; Ensures that the connections are physical adapters.
  63.                 ContinueLoop
  64.             EndIf
  65.             If StringInStr($sDelimeter & $sReturn, $sDelimeter & $oObjItem.Description & $sDelimeter, 1) Then ; Ensures there are no duplicates.
  66.                 ContinueLoop
  67.             EndIf
  68.             $sReturn &= $oObjItem.Description & $sDelimeter
  69.  
  70.             If ($aReturn[0] + 1) >= $iDimension Then ; http://www.autoitscript.com/forum/topic/...en-you-have-to-resize-an-exist
  71.                 $iDimension = ($aReturn[0] + 1) * 2
  72.                 ReDim $aReturn[$iDimension]
  73.             EndIf
  74.  
  75.             $aReturn[0] += 1
  76.             $aReturn[$aReturn[0]] = $oObjItem.NetConnectionID
  77.         Next
  78.     EndIf
  79.  
  80.     ReDim $aReturn[$aReturn[0] + 1]
  81.     Return $aReturn
  82. EndFunc   ;==>_AdaptersConnected


You can run multiple copies and you can rename the executable to anything you like.

EDIT: For some wierd reason I can't get the ToolTip to change to something other than the executable name, (I was going to make it the passed NIC name), more reading to be done.

NOTE: Currently only works for physical adapters.
« Last Edit: May 02, 2013, 08:49 PM by 4wd »

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #14 on: May 02, 2013, 07:41 AM »
Hmmm?

The icon appears (193) after delay it changes (190) and immediately disappears.
Tried it on several connections with the same result each time.

EDIT: Just tried it on OpenVPN Adapter and it works perfectly, had to add 'start' to get rid of cmd window.
         It works perfectly on the Router Connection as well, went back to PPTP, same as before.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi
« Last Edit: May 02, 2013, 07:57 AM by pilgrim »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #15 on: May 02, 2013, 08:15 AM »
Can you run the attached program when you have a PPTP VPN connected, select all the lines in the output window, copy then paste into a text file and attach it to a reply here?

Doesn't do anything strange, just reports information regarding network devices: device names, status, etc - no secret information, (source included so anyone can check).

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #16 on: May 02, 2013, 08:44 AM »
File attached. I've resized the columns to fit the text, any columns that are missing were empty.

[attachmini=#][/attachmini]
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #17 on: May 02, 2013, 10:20 PM »
Thanks - looks like this is going to be a bit of fun, the PPTP not having a physical connection means I need another way to see if it's online, (possibly something as simple as count the number of IPs periodically).

Quick question, do you only ever have one PPTP VPN connected at a time?

EDIT: Worked out how - some things are so easy once you stop looking too hard  :-[


PPTPchek - Simple PPTP connection monitor (I'm starting to run out of names)

Nothing fancy, same command args as ConnStat above:

PPTPcheck.exe [delay]       - delay = optional delay before monitoring starts
eg. PPTPchek.exe 30

All it does is call rasdial.exe every 2 seconds, if the named connection no longer exists the program exits.

Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Icon=netshell_190.ico
  3. #AutoIt3Wrapper_UseUpx=n
  4. #AutoIt3Wrapper_Res_Fileversion=0.3.0.20
  5. #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
  6. #AutoIt3Wrapper_Res_Icon_Add=netshell_193.ico
  7. #AutoIt3Wrapper_Res_Icon_Add=netshell_191.ico
  8. #AutoIt3Wrapper_Res_Icon_Add=netshell_192.ico
  9. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  10.  
  11. #comments-start
  12.  
  13. PPTPchek- Displays an icon in the SysTray while the specified PPTP connection exists
  14.  
  15. PPTPchek.exe [delay]
  16.  
  17. where: [delay]       = Optional delay before monitoring starts (in seconds)
  18.  
  19. eg.  PPTPchek.exe 30
  20.  
  21. Tray icon will  change colour at the end of the delay period, (if given or when rasdial.exe has finished), to indicate monitoring has begun.
  22.  
  23. NIC status will be checked every 2000 milliseconds.
  24.  
  25. PPTPchek can be terminated by causing the PPTP connection to disconnect, (eg. pull the cable), or choosing Exit from the icon context menu.
  26.  
  27. If the icon is flashing between its normal icon and a red cross it indicates the program is PAUSED - use the context menu to unpause.
  28. #comments-end
  29.  
  30. #include <Constants.au3>
  31. #include <StaticConstants.au3>
  32.  
  33. Opt("TrayAutoPause", 0)
  34.  
  35. TraySetState()                                                                   ; Show tray icon
  36. If $CmdLine[0] > 1 Then
  37.         If Int($CmdLine[2]) > 0 Then
  38.                 $j = 0
  39.                 $i = TimerInit()
  40.                 While $j < Int($CmdLine[2]) * 1000                          ; Loop until delay expired
  41.                         $j = TimerDiff($i)
  42.                         If ProcessExists("rasdial.exe") = 0 Then ExitLoop   ; If rasdial.exe process doesn't exist, exit the loop
  43.                         Sleep(1000)
  44.                 WEnd
  45.         EndIf
  46. TraySetState(2)                                                                 ; Hides tray icon after delay
  47. TraySetIcon(@ScriptName, -5)                                         ; Change icon
  48. TraySetState(1)                                                                 ; Show tray icon, ie. monitoring starts
  49.  
  50.         If _GetState() = "" Then _Exit()
  51.         Sleep(2000)
  52.  
  53.  
  54. Func _Exit()
  55.         Exit
  56.  
  57.  
  58. Func _GetState()
  59.         While ProcessExists("rasdial.exe")
  60.                 Sleep(250)
  61.         WEnd
  62.         Local $foo = Run(@comspec & ' /c rasdial.exe', '.', @SW_HIDE, $STDOUT_CHILD)
  63.         If @error Then
  64.                 MsgBox(48, "PPTPchek", "PPTPchek: Failed to run rasdial.exe")
  65.         EndIf
  66.         Local $output
  67.         $output = ''
  68.         While 1
  69.                 $output &= StdoutRead($foo)
  70.             If @error <> 0 Then ExitLoop
  71.         WEnd
  72.         StdioClose($foo)
  73.         If StringInStr($output, "No connections") Then Return ""
  74.         Local $temp = StringSplit($output, @CRLF)
  75.         TraySetToolTip($temp[2])
  76.         Return $temp[2]

UPDATE:
  • Monitoring delay will be terminated as soon as the rasdial.exe process terminates.
  • ToolTip now indicates connection name.
  • Doesn't need a connection argument, (for running via Task Scheduler)
  • Checks to see if rasdial.exe is running before executing it
  • Interval changed to 2 seconds
« Last Edit: May 11, 2013, 02:21 AM by 4wd »

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #18 on: May 03, 2013, 05:18 AM »
After many false starts, I think my memory is degrading, it works as advertised, thank you.
The icon is also reappearing in the same location if I stop and then restart the connection.
I have swapped the icons around in the exe, after running it for a while I decided the darker icon (193) looks better most of the time and the brighter one (190) catches my attention more when it first connects.
All I need to do now is sort the batch files and the shortcuts out for individual VPN's.

You have made using my computer a little bit easier and saved me countless hours of trying to sort this out myself, at the end of which I would probably have given up.
Before I leave this subject I have one further question if you don't mind:

All it does is call rasdial.exe every 5 seconds, if the named connection no longer exists the program exits.

I have been trying for a long time to find a way to put an argument into a batch file that states: 'if PPTP is (not) connected then........'.
Can your idea be used in that way and if so how would it read?
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #19 on: May 03, 2013, 06:43 AM »
Before I leave this subject I have one further question if you don't mind:

All it does is call rasdial.exe every 5 seconds, if the named connection no longer exists the program exits.

I have been trying for a long time to find a way to put an argument into a batch file that states: 'if PPTP is (not) connected then........'.
Can your idea be used in that way and if so how would it read?

Off the top of my head, something along the lines of:

Code: Text [Select]
  1. rasdial.exe | find /i "no connections" >NUL
  2. if errorlevel 1 goto running
  3.  
  4. echo No PPTP connection
  5.  
  6. goto alldone
  7. :running
  8.  
  9. echo PPTP connection exists
  10.  
  11. :alldone

ie.
Execute rasdial and pipe the output to FIND to search for "no connections", (case insensitive), if it doesn't exist the errorlevel will be 1 so we jump to the running label.

You have made using my computer a little bit easier and saved me countless hours of trying to sort this out myself, at the end of which I would probably have given up.

No problem, gives me something to do besides blast things in Borderlands 2  ;D
« Last Edit: May 04, 2013, 06:08 AM by 4wd, Reason: Fixed the conditional since errorlevel 0 did not work »

wr975

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 369
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #20 on: May 03, 2013, 10:24 AM »
Ever since I started using Windows 7 one of my dislikes was that there was no way to show an icon in the system tray to tell if a PPTP/L2TP VPN was connected, unlike in XP.

I had the same problem and tried and errored my way into a basic AHK script I'm using since 2+ years or so (also to learn this script language). I thought about sharing the tool, but I guess it's too strange or complicated. It features different trask tray icons (connected, disconnect, trying to connect), keeps checking for existing PPTP VPN connections and sends a error message if a connection is lost.

It's written for Witopia VPN connections, since Witopia offers plenty of servers to connect to and back then they didn't offer an own connection utility. Now they do, but I still prefer my script. *g*. It can also be used to be "just" a task tray PPTP connection monitor (with optional error message if connection is lost). Read "read me" file to get an explanation. And if all fails, the source might give you some ideas for your own script.
« Last Edit: May 03, 2013, 12:04 PM by wr975 »

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #21 on: May 03, 2013, 11:06 AM »
I spoke too soon. :(

I've spent the last couple of hours trying to put the batch files together and no matter what I try when I combine the PPTPcheck file with the VPN command file I am getting two cmd windows left open every time.
It works alright if I use the separate VPN ON file then run the PPTPcheck file, running VPN OFF shuts everything down, anything beyond that I'm left with open cmd windows.

wr975,

I think you had the right thought, I've just downloaded that and had a look at the script, my first thought was 'I wonder if there's an English version?'


I've given up for the day.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #22 on: May 03, 2013, 10:51 PM »
Use Cmdow instead of Start - an old standby from the heady days of DOS.

Cmdow - Cmdow is a Win32 commandline utility for NT4/2000/XP/2003 that allows windows to be listed, moved, resized, renamed, hidden/unhidden, disabled/enabled, minimized, maximized, restored, activated/inactivated, closed, killed and more.

eg. cmdow.exe /run /hid pptpchek.exe US-VPN 20

Only the Netload download link seems to be working at this time.

NOTE: Your AV program will probably hate it unless you tell it to ignore it - although MSE and MBAM seem OK with it.

It'll do until I work out how to detach from the CLI, which seems to be surprisingly hard, (or obscure).
« Last Edit: May 03, 2013, 11:05 PM by 4wd »

pilgrim

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 316
  • Cogito ergo ?
    • View Profile
    • Pilgrim's Page
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #23 on: May 04, 2013, 04:59 AM »
The batch file I started with is this:

if exist pptp-running.txt goto running
rem prog is not running
copy NUL>pptp-running.txt
start /d "C:\Batch Files\PPTP ON-OFF" PPTP-US-ON.bat
start /d "C:\Batch Files\PPTP ON-OFF" PPTPcheck.bat
goto alldone
:running
rem prog is already running
del pptp-running.txt
start /d "C:\Batch Files\PPTP ON-OFF" PPTP-OFF.bat
:alldone

That is running the commands but leaving 2 cmd windows open on enable and 1 cmd window open on disable, so apart from the left-over windows it works.
As I said yesterday, if I run the individual batch files separately the cmd windows disappear as soon as a specific command completes.

Downloaded Cmdow and all my security software ignored it.
Substituted 'start /d', first with 'cmdow' then after looking up the options with 'cmdow /run', in both cases the cmd window flashed on and off, it created the text file but nothing else happened.

For the moment I have put a shortcut to PPTPcheck.bat into the PPTP toolbar so I can start it from there when a VPN is connected and it shuts down automatically when the VPN is disconnected.

I will have another go later, I also have to look into whether all this will work with L2TP given that it requires an additional piece of information in the form of a PSK.
I spent 25 years training to be an eccentric then I woke up one morning and realised that I'd cracked it.
I've not had to try since.

I wonder what happens if I click on thi

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,640
    • View Profile
    • Donate to Member
Re: Turn a batch file into an AHK script.
« Reply #24 on: May 04, 2013, 05:59 AM »
Code: Text [Select]
  1. rasdial.exe | find /i "no connections" >NUL
  2. if errorlevel 1 goto running
  3. rem prog is not running
  4. cmdow.exe /run rasdial.exe PPTP-UK freecloudvpn.com 1702
  5. cmdow.exe /run /hid PPTPchek.exe PPTP-UK 60
  6. goto alldone
  7. :running
  8. rem prog is already running
  9. cmdow.exe /run rasdial.exe /disconnect
  10. :alldone

Tested working on WHS2011, (equivalent to Win7 x64), running it from the directory in which cmdow.exe, PPTPchek.exe and the batch file all reside.

Run it from a shortcut and no CLI windows are left open.

No separate OFF batch file required, just the ON ones.

(That's a free VPN BTW, password changes every few hours.)