0

Why is this Windows batch not working:

@ECHO OFF
SET PATH="%PROGRAMFILES(x86)%\PuTTY";%PATH%
SET sti=%TEMP%\ipinfo_c200.txt
plink >nul 2>nul
IF %ERRORLEVEL% NEQ 9009 (
    plink -ssh 192.168.1.101 -l root "wget -qO - http://ipinfo.io/ip" >%sti%
    SET /P vpn=<%sti%
    Del /F /Q %sti%
    start "" http://%vpn%:8888/speedtest
) ELSE (
    Echo Command plink does not exist. Install PuTTY.
)
EXIT

When this will work just fine:

@ECHO OFF
SET PATH="%PROGRAMFILES(x86)%\PuTTY";%PATH%
SET sti=%TEMP%\ipinfo_c200.txt
plink -ssh 192.168.1.101 -l root "wget -qO - http://ipinfo.io/ip" >%sti%
SET /P vpn=<%sti%
Del /F /Q %sti%
start "" http://%vpn%:8888/speedtest
EXIT

Only difference is the IF...ELSE.

When working inside the IF-statement the command plink does not return an IP address.

If I put the SET sti= statement inside the IF-statement I get an unexpected-error.

What am I missing (probably something basic)?

Thanks,

a15995
  • 17
  • 5
  • 4
    you fell into the [delayed expansion](https://stackoverflow.com/a/30284028/2152082) trap. – Stephan Sep 09 '17 at 16:10
  • And so I did - and thank God it was that simple. Drove me crazy. Thanks Stephan! – a15995 Sep 09 '17 at 17:40
  • You could also capture the PLINK output with a `FOR /F` command and use the FOR variable instead of an environmental variable. This way you would not need to use delayed expansion. – Squashman Sep 09 '17 at 17:51
  • `IF %ERRORLEVEL% EQU 9009 Echo Command plink does not exist. Install PuTTY.&pause&exit` – JosefZ Sep 10 '17 at 10:03

0 Answers0