Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Warning, Noob when it comes to scripts, but I have a problem with the script below-using cURL.

curl -L "%host%/cm?cmnd=Power%con%%%20On"

The Above code does not execute. %host% does not work but if I enter the IP address or %host1,2,3,..% it then works, but I need it to work with the %host% variable.

example 1 curl "%host1%/cm?cmnd=Power%con%%%20On" Works
example 2 curl "http://192.168.0.254/cm?cmnd=Power%con%%%20On" Works
example 3 curl "%host%/cm?cmnd=Power%con%%%20On" Does not Work

Works = The script inserts the correct host and the script executes its task by turning on the switch (Tasmota smart switch). correct inserted URL example: http://192.168.0.254/cm?cmnd=Power1 On

Does not Work = The script does not execute its task and the switch (Tasmota) does not turn on

It seems that %host% is not inserting the IP Address from set hostX=http://....

Also note the value for if%1==X is passed on via a program so I do not see the values added or the results or execution result of the batch script, Just the results of the Switch turning on or not.

@echo off


set host1=http://192.168.0.254
set host2=http://192.168.0.254
set host3=http://192.168.0.254
set host4=http://192.168.0.254
set host5=http://192.168.0.212

set con1=1
set con2=2
set con3=3
set con4=4
set con5=

@echo off
if %1==7 set host=%host1% & set con=%con1%
if %1==8 set host=%host2% & set con=%con2%
if %1==9 set host=%host3% & set con=%con3%
if %1==10 set host=%host4% & set con=%con4%
if %1==11 set host=%host5% & set con=%con5%

curl -L "%host%/cm?cmnd=Power%con%%%20On"


Thank you
Earl L

What I have tried:

curl -L "%host5%/cm?cmnd=Power%con%%%20On"                   THIS WORKS
curl -L "http://192.168.0.254/cm?cmnd=Power%con%%%20On"      THIS WORKS
curl -L "%host%/cm?cmnd=Power%con%%%20On"                    NEED THIS
Posted
Updated 12-Jun-23 21:31pm
v5
Comments
Dave Kreskowiak 12-Jun-23 21:51pm    
...and the problem would be...?
Earl Livingston 12-Jun-23 22:59pm    
curl -L "%host%/cm?cmnd=Power%con%%%20On"

The Above code does not execute. %host% does not work but if I enter the IP address or %host1,2,3,..% it then works, but I need it to work with the %host% variable.

curl "%hos1t%/cm?cmnd=Power%con%%%20On" Works
curl "http://192.168.0.254/cm?cmnd=Power%con%%%20On" Works
curl "%host%/cm?cmnd=Power%con%%%20On" Does not Work
Dave Kreskowiak 12-Jun-23 23:24pm    
Again, you're going to have to define "does not work". That's the most useless problem description on the planet.

What is the error message returned by curl?

Everything works up to the curl command, so long as you put one of the values in your IF statements on the command line when you run your batch file. You can test that just by replacing the curl line with ECHO %host%.

1 solution

I have made a couiple of minor modifications to your command (not curl) script as follows:
BAT
  1  @echo off
  2  
  3  if NOT "%1" == "" GOTO continue
  4    ECHO Required parameter missing
  5    GOTO :EOF
  6  
  7  :continue
  8  SETLOCAL
  9  
 10  set host1=http://192.168.0.254
 11  set host2=http://192.168.0.254
 12  set host3=http://192.168.0.254
 13  set host4=http://192.168.0.254
 14  set host5=http://192.168.0.212
 15  
 16  set con1=1
 17  set con2=2
 18  set con3=3
 19  set con4=4
 20  set con5=
 21  
 22  @echo off
 23  if %1==7 set host=%host1%& set con=%con1%
 24  if %1==8 set host=%host2%& set con=%con2%
 25  if %1==9 set host=%host3%& set con=%con3%
 26  if %1==10 set host=%host4%& set con=%con4%
 27  if %1==11 set host=%host5%& set con=%con5%
 28  
 29  curl -L "%host%/cm?cmnd=Power%con%%%20On"

Lines 3 to 8 check that a parameter is entered at the console, and uses local variables for the host names rather than the main environment set.
Lines 23 to 27 remove the space character after the host IP address.

There are probably better ways to do this but that should get you started.
 
Share this answer
 
v3
Comments
Earl Livingston 13-Jun-23 5:37am    
Thank you a million Richard, removing the space character after the host IP address fix the script.

Thank you so much for your time.
Richard MacCutchan 13-Jun-23 5:41am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900