What's new

How to send http request in user script?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Partizanin

Occasional Visitor
Hi, i have asus tuf ax3000, with AsusWrt Merlin firmware and i`m trying to send https request through user script.
I have created file service-event and put it to the /jffs/scripts and make it executable,but for some reason my request hasn`t send, but script running every time when a service event occurs because i can see it in log.
When i try execute script manually the request is sending and i got message on my phone but if it executing by service event script is starting but request is not sending.
May be someone know resean or solution for this ?

Here my script file
Code:
#!/bin/sh
BOT_TOKEN=""
CHAT_ID=""
curl --location --request GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'

Here log of my router log screenshot
 
Are you using the router's built-in curl command? I don't think it supports the --location option.
 
You say this works when you run the script (not just the curl command) from the command line, i.e. /jffs/scripts/service-event?
 
First off, it's NOT clear why you decided to choose the service-event given you are NOT checking the input arguments for a specific event. If you don't, then it's going to execute repeatedly, for ALL supported event notifications. It would seem better suited to say services-start (which has no arguments, it occurs once, after all services are started).

Secondly, I prefer to use the following in my scripts, so I get relevant output regarding all actions by the script (good or bad) sent to the syslog.

Code:
#!/bin/sh
set -x # uncomment/comment to enable/disable debugging mode
{
BOT_TOKEN=""
CHAT_ID=""
curl --location --request GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'
} 2>&1 | logger -t $(basename $0)[$$]

Otherwise, you're flying blind.

P.S. With services-start, it's also more likely the WAN is actually up and available! You can't necessary assume that's the case w/ any and all service-event events. That's why it's also a good idea to check for internet connectivity in any such script before continuing.
 
Last edited:
Here's an example.

Code:
#!/bin/sh
set -x # uncomment/comment to enable/disable debugging mode
(
# wait until internet connectivity is confirmed!
until ping -qc1 -W3 8.8.8.8 &>/dev/null; do sleep 10; done

BOT_TOKEN=""
CHAT_ID=""
curl --location --request GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'
) 2>&1 | logger -t $(basename $0)[$$] &

Notice this time, I run the script in the background (&) just in case it does get held up.
 
First off, it's NOT clear why you decided to choose the service-event given you are NOT checking the input arguments for a specific event. If you don't, then it's going to execute repeatedly, for ALL supported event notifications. It would seem better suited to say services-start (which has no arguments, it occurs once, after all services are started).

Secondly, I prefer to use the following in my scripts, so I get relevant output regarding all actions by the script (good or bad) sent to the syslog.

Code:
#!/bin/sh
set -x # uncomment/comment to enable/disable debugging mode
{
BOT_TOKEN=""
CHAT_ID=""
curl --location --request GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'
} 2>&1 | logger -t $(basename $0)[$$]

Otherwise, you're flying blind.

P.S. With services-start, it's also more likely the WAN is actually up and available! You can't necessary assume that's the case w/ any and all service-event events. That's why it's also a good idea to check for internet connectivity in any such script before continuing.
Hi eibgrad, thanks for your reply, you are right it should to accept all input argument and sent all action to syslog, but this is test version where i trying to fix my issue and after that i will put my script in order.
 
Hi eibgrad, thanks for your reply, you are right it should to accept all input argument and sent all action to syslog, but this is test version where i trying to fix my issue and after that i will put my script in order.

I wasn't concerned so much about accepting input arguments. My point is, it's almost impossible to debug ANY script that's being run w/o a terminal if you don't provide an alternative, such as the syslog or a file. You're just left to *guess* why things are NOT working as expected. IOW, the syslog is your friend here.
 
I wasn't concerned so much about accepting input arguments. My point is, it's almost impossible to debug ANY script that's being run w/o a terminal if you don't provide an alternative, such as the syslog or a file. You're just left to *guess* why things are NOT working as expected. IOW, the syslog is your friend here.
Ok, clear, thanks, so lets try to log this and see what went wrong.
I have add log output and got that, when i execute script manually it works and i see that in log, but when it executed by event nothing happen.

1657044559094.png
 
Ok, clear, thanks, so lets try to log this and see what went wrong.
I have add log output and got that, when i execute script manually it works and i see that in log, but when it executed by event nothing happen.


Remember, when you're logged into the SSH server, the environment is different than when the system is booting up. In the former case, you KNOW you have internet connectivity at that time, whereas the latter, that's NOT necessarily the case when dealing w/ these various events. Also, in the former, you have your own login profile based on the admin account, which could mean a difference in the defined PATH.

IOW, you can't just assume if its runs while logged in, that it will work flawlessly from bootup. The situations and conditions are NOT identical. That's usually why things go wrong.

P.S. It's also a good practice to include a timeout argument to curl. It will default to (iirc) 9 mins, which is insane. Better to set something reasonable (say 30 seconds), and retry if necessary, perhaps some maximum number of retries (e.g., 3).

Code:
curl --location --request --connect-timeout 30 GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'
 
Remember, when you're logged into the SSH server, the environment is different than when the system is booting up. In the former case, you KNOW you have internet connectivity at that time, whereas the latter, that's NOT necessarily the case when dealing w/ these various events. Also, in the former, you have your own login profile based on the admin account, which could mean a difference in the defined PATH.

IOW, you can't just assume if its runs while logged in, that it will work flawlessly from bootup. The situations and conditions are NOT identical. That's usually why things go wrong.

P.S. It's also a good practice to include a timeout argument to curl. It will default to (iirc) 9 mins, which is insane. Better to set something reasonable (say 30 seconds), and retry if necessary, perhaps some maximum number of retries (e.g., 3).

Code:
curl --location --request --connect-timeout 30 GET 'https://api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=CHAT_ID&text=hi'
Ok, clear, so you don't have solution for me.
Thanks for help, will be looking for solution further.
 
What is the purpose of BOT_TOKEN and CHAT_ID in your script? As it stands they're empty variables that are not referenced in the curl command. You have the same words (BOT_TOKEN and CHAT_ID) appearing in the curl command but they're not variables. Are you substituting something here that you're not showing us or is this the way it's meant to be?
 
What is the purpose of BOT_TOKEN and CHAT_ID in your script? As it stands they're empty variables that are not referenced in the curl command. You have the same words (BOT_TOKEN and CHAT_ID) appearing in the curl command but they're not variables. Are you substituting something here that you're not showing us or is this the way it's meant to be?
Yes, it is sensitive parameter which i cant show but i replace that and it works, i got message from script
 
Yes, it is sensitive parameter which i cant show but i replace that and it works, i got message from script
So you're typing these values directly into the curl command line and not setting them in the BOT_TOKEN= and CHAT_ID= lines?
 
So you're typing these values directly into the curl command line and not setting them in the BOT_TOKEN= and CHAT_ID= lines?
No, i change the source file and put that values before sending file to the router.
Ok, doesn't matter we can make request to another url for example google.com
 
When you use curl from an ssh prompt, it will GET the file and write it to the current directory (unless you use the -o option, or redirect it (>)). But when you use the same script w/ the bootup process, where is the file being written to?! You aren't specific. You just assume it's writable. Seems to me you should be specific as to /path/filename (e.g., /tmp/somefile, /jffs/somefile).
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top