0

I'm currently trying to write a batch file to automatically detect if a VPN is logged in or not. However, the code is not working as expected.

set VAR=SUCCESS
for /f "delims=: tokens=1" %%i in ('"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info') do (
    set str=%%i
    set rep=%str:logged out=% 
    echo %str%
    echo %rep%
    if not "%str%"=="%rep%" (
        set VAR=FAIL
    )
    echo %VAR%
)
echo %VAR%

Running "C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info will give exactly the following (will have spaces before the start of each line):

Command arguments:

INFO result:

session:        code:   status:

xxxxxx          64      logged out

There is 1 active session(s)!

The status can be either logged out or session established. What I'm trying to determine is that I'd have run the VPN. If the user has successfully logged into the VPN, then I need to do something, otherwise, have to execute some other code.

VAR=FAIL means the VPN failed to log in successfully and would be in logged out state. The problem I'm facing is that the str and rep variables are not assigned any value. I can determine it by the echo statements.

Can somebody help me out in this?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Arun
  • 131
  • 1
  • 12
  • Its mentioned there I can use call stmt to bypass delayed expansion. Still not working. Also, I've already tried delayed expansion. Still is not setting value to variable correctly. Hence the new question – Arun Aug 02 '18 at 04:44

1 Answers1

1

There is a much more simple way to work around this. The way you have the for statement setup will cause it to perform echo's and set strings for each new line of the output string.

A very easy way is to use the | FIND /I after your command. This will filter and search for a statement or text your looking for without any loops.

"C:\Program Files (x86)\F5 VPN\f5fpc.exe" -info | FIND /I "logged out">Nul && (Echo.logged out) || (Echo.session established)

To use the setup, simply put your command in the front (Before the pipe) and use the find /I "" to search for that within the output. Please keep in mind that >Nul will silence the command from being shown on console, which is great for this use.

John Kens
  • 1,615
  • 2
  • 10
  • 28