batch-filevpncisco

How to get Cisco VPN connection status in Batch for further processing


I want to check whether I am connected to a Cisco VPN in a batch script. What I am currently trying is this:

.\vpnclient.exe | Findstr 'Connected'

This does kind of work: Calling vpnclient.exe opens Cisco's cli tool, then the Findstr command runs correctly. What I cannot get to work is: How do I then programmatically exit that tool and proceed? I can then manually exit the tool, but that is not really the idea of automation.

I have tried simply putting exit in the the next line, or echo exit | vpnclient.exe | Findstr 'Connected', .\vpnclient.exe | Findstr 'Connected' | exit, exit | .\vpnclient.exe | Findstr 'Connected'.

None of those exited the tool, the execution of the script simply halts. Sometimes, I can't even exit it manually anymore and have to open a new window.

Other questions on stackoverflow did not help, no one there seems to try to then programmatically exit and proceed.

How can I either programmatically exit Cisco's cli tool or check the connection status in another way, so I can proceed execution depending on the connection status?

Edit: (echo Y|vpnclient.exe) | Findstr "Connected" takes me into the cli tool, but I still have to exit it manually via ctrl + c, which is non-usable. I feel like Cisco's tool has some weird behavior here, or am I wrong about this?


Solution

  • The sites and threads I had previously found all recommended simply calling .\vpnclient.exe to get the connection status. This leads to the behavior described in the question.

    Turns out, the VPN client has a command called status, which does exactly what one would expect. So, what works is:

    .\vpnclient.exe status | Findstr "Connected"
    echo %errorlevel%
    

    Simple as that. Why this is not recommended (or well-documented) more often I do not know.