1

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:

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

The batch script looks like this:

@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%"  > wget.out

I copied the wget.exe and four other DLLs into the system32 directory hoping I can make the command run without the full path, but it complains about the libeay32.dll, but that DLL exists in System32 directory., I don't know what is the problem .. but if there is a way to get the response into a variable, even using the full path to wget, as my script is right now, it will be oerfect, now the scripts runs every 5 minutes, and it's working great, but it's not doing anything with the response from the RasPi.

How can I save the output from wget into a variable?

UPDATE @LotPings after trying your modified script (https://superuser.com/a/1228066/746701), 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

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
4
  • 1
    You are confusing bash (a *nix shell) with batch (windows command interpreter)
    – LotPings
    Jul 8, 2017 at 20:42
  • Yes, I did confused it, I never used batch / bash scripts untill a month ago, when I started making a Dashboard interface to have all kinds of things in one interface, my plan is to have alot different devices monitored by one RaspberryPi (BananaPi in fact, as I wanted an HDD not a SD for the database, and constant writing - every minute) now I have 2 bash scripts to monitor 2 dedicated servers (email queue, CPU load, Memory) and one script to monitor my Windows PC's (CPU, RAM, Uptime). The batch script is called every 5 minutes by Task Scheduler, and it push the values to bananaPi
    – ovisopa
    Jul 9, 2017 at 7:40
  • @Scott, the full code was deleted as I added it later as an answer, so I updated my initial question to include the full code. Also in the initial question you can seee the error I get with LotPings code from this post superuser.com/a/1228066/746701
    – ovisopa
    Jul 12, 2017 at 8:28
  • @Scott those variables you mention ware not defined by me, most probably is something wget internally is doing, and I don't know why are you asking me about those variables as I mentioned that the request is successfully made - wget is doing it's job, it always did, even before posting my question. I need a way to capture the response of the server, which is received by wget, as wget it display the response size. The problem is regarding WebResponse variable, not the other!
    – ovisopa
    Jul 13, 2017 at 7:43

2 Answers 2

0

LotPings showed one way to grab the output of a program and put it into a variable.  Another is to write the program output to a file and then read it into a variable with set /p variable_name=.  For example,

your_WGET_command > wget.out  &&  set /p WebResponse= < wget.out

1
  • I placed > wget.out after the call to wget but it didn't saved the output to the file, that file is still empty. I will place a new reply so I can include the full code, which works great for pushing the data, I need a way to capture the response of my BananaPi (if I load the exact same URL in the browser I see the response)
    – ovisopa
    Jul 9, 2017 at 8:04
0

Well from your code it's clear that you are after a batch - not a bash script.

I don't know if you want to schedule the batch to run every 5 minutes,
so I incorporated a loop with a timeout of 300 seconds.

:: 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
)
4
  • I will try to modify your script as I don't need a loop and let you know if I was able. To keep things simple in my initial post I cut a part of the code as I am pushing more values to bananaPi, like CPU usage, uptime .. but it's ok with just one for now. I tried your script and I get those 2 errors: 'C:\Program' is not recognized as an internal or external command, operable program or batch file. Environment variable WebResponse not defined
    – ovisopa
    Jul 9, 2017 at 7:51
  • @LotPings I also tried Set "wget=C:\Progra~2\GnuWin32\bin\wget.exe" and I still get an error : The system cannot find the path specified. Environment variable WebResponse not defined But If I write that path in a new cmd window, it opens the wget, so that should be a correct path, but somehow in batch is not working, as it doesn't work with the space in the name.
    – ovisopa
    Jul 9, 2017 at 8:46
  • The for /f executing commands shows sometimes problems with two double quoted entries, changed that above.
    – LotPings
    Jul 9, 2017 at 9:47
  • Thank you very much @LotPings It runs the wget.exe but it still doesn't "capture" the response, I still get the error about Enviroment variable WebResponse not defined
    – ovisopa
    Jul 9, 2017 at 13:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .