how to save response of wget.exe in a variable?
Posted: 17 Jul 2017 01:43
hello,
I have this batch script to push some values to a RaspberryPi, and I want now to save the response to a variable. I want this as there will be two possible responses so far: one is OK and one is SLEEP.
When it will get the SLEEP response I want to run this command to put the computer into sleep mode:
The batch script looks like this:
Someone tried to help me on superuser forums but his script didn't worked, I got an error that the variable was not defined
It runs the wget.exe program, makes the request correctly (I have checked apache2 access.log) but it still doesn't save the response to that variable, you can see the error about WebResponse variable bellow:
Any help is appreciated !
I have this batch script to push some values to a RaspberryPi, and I want now to save the response to a variable. I want this as there will be two possible responses so far: one is OK and one is SLEEP.
When it will get the SLEEP response I want to run this command to put the computer into sleep mode:
Code: Select all
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
The batch script looks like this:
Code: Select all
@echo off
set totalphysicalmemory=
set loadpercentage=
set freephysicalmemory=
set boottime=
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set datestr=%month% %day% %year%
FOR /F "tokens=2 delims='='" %%A in ('wmic cpu get loadpercentage /value') do SET loadpercentage=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem get TotalPhysicalMemory /value') do SET totalphysicalmemory=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic OS get FreePhysicalMemory /value') do SET freephysicalmemory=%%A
FOR /f %%a in ('WMIC OS GET lastbootuptime ^| find "."') DO set DTS=%%a
set BOOTTIME=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2% %DTS:~8,2%:%DTS:~10,2%
c:\"Program Files (x86)"\GnuWin32\bin\wget.exe "http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%"
Someone tried to help me on superuser forums but his script didn't worked, I got an error that the variable was not defined
Code: Select all
:: Q:\Test\2017\07\08\SU_1226953.cmd
@Echo off
Pushd "C:\Program Files (x86)\GnuWin32\bin\"
Set "URL=http://192.168.1.40/push.php?freephysicalmemory"
FOR /F "tokens=2 delims='=" %%A in (
'wmic OS get FreePhysicalMemory /value'
) do SET "freephysicalmemory=%%A
set freephysicalmemory
FOR /F "delims=" %%A in (
' wget.exe "%URL%=%freephysicalmemory%" '
) do Set "WebResponse=%%A"
Set WebResponse
If /i "%WebResponse%" Equ "SLEEP" (
Echo going to sleep now ....
Rem rundll32.exe powrprof.dll,SetSuspendState 0,1,0
)
It runs the wget.exe program, makes the request correctly (I have checked apache2 access.log) but it still doesn't save the response to that variable, you can see the error about WebResponse variable bellow:
Code: Select all
C:\Ovis>wget.bat
freephysicalmemory=10421416
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\Program Files (x86)\GnuWin32/etc/wgetrc
--2017-07-09 16:04:56-- http://192.168.1.40/push.php?freephysicalmemory=10421416
Connecting to 192.168.1.40:80... conectat.
Cerere HTTP trimisă, se aşteaptă răspuns... 200 OK
Dimensiune: 5 [text/html]
Saving to: `push.php@freephysicalmemory=10421416'
100%[==============================================================================>] 5 --.-K/s in 0s
2017-07-09 16:04:56 (757 KB/s) - `push.php@freephysicalmemory=10421416' saved [5/5]
Environment variable WebResponse not defined
Any help is appreciated !