how to save response of wget.exe in a variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

how to save response of wget.exe in a variable?

#1 Post by sibianul » 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:

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 !

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

Re: how to save response of wget.exe in a variable?

#2 Post by igor_andreev » 18 Jul 2017 09:30

Try

Code: Select all

wget.exe  "%URL%=%freephysicalmemory%" 2>&1(or 2^>&1 inside FOR)

Wget output to StdErr by default

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#3 Post by sibianul » 19 Jul 2017 00:56

Thank you Igor, but it still doesn't work, I tried both codes:

Code: Select all

FOR /F "delims=" %%A in (
  ' wget.exe  "%URL%=%freephysicalmemory%" 2^>&1'
) do Set "WebResponse=%%A"
Set WebResponse


The error with this was :

Code: Select all

c:\Ovis>wget.bat
freephysicalmemory=9843352
& was unexpected at this time.


With this code :

Code: Select all

FOR /F "delims=" %%A in (
  ' wget.exe  "%URL%=%freephysicalmemory%" 2>&1'
) do Set "WebResponse=%%A"
Set WebResponse


I got this error:

Code: Select all

c:\Ovis>wget.bat
freephysicalmemory=9843352
& was unexpected at this time.


Any more help is appreciated!

Thank you.

Samir
Posts: 345
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: how to save response of wget.exe in a variable?

#4 Post by Samir » 19 Jul 2017 06:09

What does the output of

Code: Select all

c:\"Program Files (x86)"\GnuWin32\bin\wget.exe "http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%"
look like?

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#5 Post by sibianul » 19 Jul 2017 06:42

this is the output, as you can see where it says Dimensiune: 5 [text/html] , in this case the response was SLEEP (5 characters). The response is either empty, OK or SLEEP

Code: Select all

SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = c:\Program Files (x86)\GnuWin32/etc/wgetrc
--2017-07-19 15:39:36--  http://192.168.1.40/push.php?loadpercentage=%25loadpercentage%25&totalphysicalmemory=%25totalphysicalmemory%25&boottime=%25boottime%25&freephysicalmemory=%25freephysicalmemory%25
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@loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%.1'

100%[==============================================================================>] 5           --.-K/s   in 0s

2017-07-19 15:39:36 (185 KB/s) - `push.php@loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%.1' saved [5/5]

Samir
Posts: 345
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: how to save response of wget.exe in a variable?

#6 Post by Samir » 19 Jul 2017 09:52

Oops! Sorry! I asked the wrong question. :oops:

What does the output of

Code: Select all

http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%
look like?

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

Re: how to save response of wget.exe in a variable?

#7 Post by igor_andreev » 19 Jul 2017 10:08

sibianul wrote:

Code: Select all

& was unexpected at this time.



Escape(add caret) before ampersand too.

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#8 Post by sibianul » 19 Jul 2017 10:11

Samir wrote:Oops! Sorry! I asked the wrong question. :oops:

What does the output of

Code: Select all

http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%
look like?


Loading the URL in browser will return 2 or 3 possible strings:
1. empty webpage in response
2. just 2 letters OK (no markup or anything)
3. 5 letters as a response, the word SLEEP .. again , no markup (no JSON, HTML, etc. just that word)

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#9 Post by sibianul » 19 Jul 2017 10:25

igor_andreev wrote:
sibianul wrote:

Code: Select all

& was unexpected at this time.


Escape(add caret) before ampersand too.


I don't understand, please can you show me what you mean ?

Code: Select all

FOR /F "delims=" %%A in (
  ' wget.exe  "%URL%=%freephysicalmemory%" 2>&1'
) do Set "WebResponse=%%A"
Set WebResponse


Thank you.

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

Re: how to save response of wget.exe in a variable?

#10 Post by igor_andreev » 19 Jul 2017 10:28

2^>^&1

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#11 Post by sibianul » 19 Jul 2017 11:12

Thank's Igor, it worked, no more errors but still in WebResponse variable I don't have the content of the server response, it seems it's the full date (maybe a part of all headers)

With this code:

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%" 2^>^&1'
) do Set "WebResponse=%%A"
Set WebResponse

Echo This is content of webresponse variable:  %WebResponse%

If /i "%WebResponse%" Equ "SLEEP" (
  Echo going to sleep now ....
  Rem rundll32.exe powrprof.dll,SetSuspendState 0,1,0
)



I get this response:

Code: Select all

c:\wget.bat
freephysicalmemory=9379820
WebResponse=2017-07-19 19:50:08 (206 KB/s) - `push.php@freephysicalmemory=9379820' saved [5/5]
This is content of webresponse variable :  2017-07-19 19:50:08 (206 KB/s) - `push.php@freephysicalmemory=9379820' saved [5/5]


At the end it says "saved 5/5", I don't know where, but 5 is the size of the response my RaspberryPi replies, it was the word SLEEP.

Any more help to solve this issue, is really appreciated, it's been 2 weeks since I try to solve this.

Or if you have any other ideas how I can accomplish this task, I'm open to changes :

I want to save in an mysql database (that is located on the RaspberryPi) some values of each computer I have in my office and workshop, all are Windows 7 / 10 .

So far the values are :
1. CPU load
2. RAM usage
3. Uptime (I don;t like the form it is now, but I will change this)

Feature values will be:
4. CPU temperature
5. HDD's free space

As I'm used to code in php, I made the push.php file on the RaspberryPi that process those values (and other received from other ESP8266 based sensors, like temperature, humidity) and saves those values in the mysql database.

I want to be able to do this with minimal softaware installation on the client side (Windows PC's)

As I have quite a few PC's I sometimes forget to turn them off, and I want to have an option to quickly do it from the same dashboard I work on, in my free time, this is where the SLEEP response will come, if I see an PC it's turned on, I will make a button in my dashboard, to "send" the command to SLEEP, the command will not actually be sent, but the server will wait until the next access to push.php file, and if that device had the SLEEP button pushed, it will reply with SLEEP word, else it will reply OK.

I know I can turn off using VNC, I have VNC server on my workstation, but don;t have it on other, and I really wanted to have the ability to turn them of from that dashboard
Attachments
IMG_7268.PNG
Dashboard on mobile, saved on homescreen - NO ADDRESS BAR, just like an normal App
IMG_7268.PNG (83.82 KiB) Viewed 3817 times
dashboard.jpg
Dashboard in a Browser
dashboard.jpg (77.94 KiB) Viewed 3817 times
Last edited by sibianul on 20 Jul 2017 00:05, edited 1 time in total.

Samir
Posts: 345
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: how to save response of wget.exe in a variable?

#12 Post by Samir » 19 Jul 2017 15:08

Hmm...there's a lot going on here. I think the batch part might be working right, but the wget is not.

Try just running the following commands and let me know what you get:

Code: Select all

c:\"Program Files (x86)"\GnuWin32\bin\wget.exe --output-document=TEST "http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%"

Code: Select all

TYPE TEST


The result after the last command should be either OK or SLEEP if things are working the way I think they are.

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#13 Post by sibianul » 19 Jul 2017 23:57

Samir, after typing TYPE TEST it showed what I wanted :)

Code: Select all

C:\Users\Ovidiu>c:\"Program Files (x86)"\GnuWin32\bin\wget.exe --output-document=TEST "http://192.168.1.40/push.php?loadpercentage=%loadpercentage%&totalphysicalmemory=%totalphysicalmemory%&boottime=%boottime%&freephysicalmemory=%freephysicalmemory%"
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = c:\Program Files (x86)\GnuWin32/etc/wgetrc
--2017-07-20 08:48:32--  http://192.168.1.40/push.php?loadpercentage=%25loadpercentage%25&totalphysicalmemory=%25totalphysicalmemory%25&boottime=%25boottime%25&freephysicalmemory=%25freephysicalmemory%25
Connecting to 192.168.1.40:80... conectat.
Cerere HTTP trimisă, se aşteaptă răspuns... 200 OK
Dimensiune: 5 [text/html]
Saving to: `TEST'

100%[==============================================================================>] 5           --.-K/s   in 0s

2017-07-20 08:48:33 (181 KB/s) - `TEST' saved [5/5]


C:\Users\Ovidiu>TYPE TEST
SLEEP
C:\Users\Ovidiu>


I found the other files also, wget was saving files in my Windows User folder, and the last one was the TEST file, and it contained the response of my server, the word SLEEP :) Now what ?
Attachments
output_of_wget.jpg
output_of_wget.jpg (57.61 KiB) Viewed 3793 times

penpen
Expert
Posts: 1725
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how to save response of wget.exe in a variable?

#14 Post by penpen » 21 Jul 2017 01:05

Sorry, i was ill, so i couldn't post earlier.
I wonder which of the "push.php" parameters are needed (it would help if you post its sourcecode):
Depending on the solution in your opening post i assume, that only the free physical memory is needed.

I assume (although i haven't checked it) that older "wget.exe" versions are displaying the download directly to STDOUT;
i guess that's the reason why your helper on the superuser forum has posted his code without setting wget to be "quiet".
So i think this should work for you (but it depends on the implementation of "push.php" if the response you get is correct or not):

Code: Select all

@Echo off
Pushd "C:\Program Files (x86)\GnuWin32\bin\"
setlocal enableExtensions disableDelayedExpansion

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 -q -O - "http://192.168.1.40/push.php?freephysicalmemory=%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
)


penpen

sibianul
Posts: 8
Joined: 17 Jul 2017 01:32

Re: how to save response of wget.exe in a variable?

#15 Post by sibianul » 21 Jul 2017 02:04

Thank you penpen, now the script works perfectly, I updated my initial bat file, and now I push all the paramenters, not just the freephysicalmemory.

So the only thing missing was -q -O :) ?

PS. Now for another PC I will try to modify the script, to add one important thing, this PC is controlling an CNC router, the script should check if the program Mach3 is running , and if it's running to not send the PC to sleep, but ignore that command. I want to be sure that if someone (wife, kids) presses the sleep button in the dashboard, by mistake, it won't shut down the PC while running. If I can't do this myself I will open a new thread .

Thank you guys !

Have a nice day!

Post Reply