Execute Bash Script from Url

Execute bash script from URL

source <(curl -s http://mywebsite.example/myscript.txt)

ought to do it. Alternately, leave off the initial redirection on yours, which is redirecting standard input; bash takes a filename to execute just fine without redirection, and <(command) syntax provides a path.

bash <(curl -s http://mywebsite.example/myscript.txt)

It may be clearer if you look at the output of echo <(cat /dev/null)

Execute bash script from URL using python

Evidently, os.system runs its command through /bin/sh, which usually causes whichever shell it's linked to to drop to a compatibility mode that doesn't include the <(...) construction. You can get around it by either storing the result in a temporary file or using another level of shell. Ugly, but it works.

os.system('bash -c "bash <(curl -s http://mysite.com/myscript.sh) World"')

How to execute a url and parse it from bash shell script?

great start! Let's break it down:

COUNT=60   #number of 10 second timeouts in 10 minutes
SUM_SYNCS=0
SUM_SYNCS_BEHIND=0

while [[ $COUNT -ge "0" ]]; do

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for syncs and syncs_behind
SYNCS=$(echo $DATA | grep -oE 'syncs: [0-9]+' | awk '{print $2}')
SYNCS_BEHIND=$(echo $DATA | grep -oE 'syncs_behind: [0-9]+' | awk '{print $2}')

#add new values to the sum totals
let SUM_SYNCS+=SYNCS
let SUM_SYNCS_BEHIND+=SYNCS_BEHIND

#verify conditionals
if [[ $SYNCS -gt "8" && $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

Execute bash script from url via cron

The hint in Olof's answer got me there, but I'm posting the full result here for completeness:

Because of a cloud provider's script which takes 20-40 seconds following reboot, my desired connection IP wasn't available to me when I first executed cron. It would either timeout, or connect after a significant delay. I have modified my connection script to poll the connection until it is available before calling curl:

#! /bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOST_IP=192.168.100.59

check_online() {
IS_ONLINE=$(netcat -z -w 5 $HOST_IP 80 && echo 1 || echo 0)
}

# Initial check to see if we're online
check_online

# Loop while we're not online.
while [ $IS_ONLINE -eq 0 ];do
# We're offline. Sleep for a bit, then check again
sleep 5;
check_online
done

# Run remote script
bash <(curl -s http://${HOST_IP}/user/startup.sh.txt)

Shell script to open a URL

Method 1

Suppose your browser is Firefox and your script urlopener is

#!/bin/bash
firefox "$1"

Run it like

./urlopener "https://google.com"

Sidenote

Replace firefox with your browser's executable file name.


Method 2

As [ @sato-katsura ] mentioned in the comment, in *nixes you can use an application called xdg-open. For example,

xdg-open https://google.com

The manual for xdg-open says

xdg-open - opens a file or URL in the user's preferred application
xdg-open opens a file or URL in the user's preferred application. If a
URL is provided the URL will be opened in the user's preferred web
browser.

If a file is provided the file will be opened in the
preferred application for files of that type. xdg-open supports file,
ftp, http and https URLs.

As [ this ] answer points out you could change your preferred browser using say:

xdg-settings set default-web-browser firefox.desktop

or

xdg-settings set default-web-browser chromium-browser.desktop

Run bash script from GitHub with curl

The "Firstly" is more complex, but you might try a temporary shell script without using pipe.

curl -s https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh >tmp.sh

bash tmp.sh

Your "Secondly" question is very easy to explain, when you do curl -s https://git.io/Jt0fZ, we get a Redirect 302, here is hints:

We try to get headers only from the server:

curl --head https://git.io/Jt0fZ

HTTP/1.1 302 Found
Server: Cowboy
Connection: keep-alive
Date: Sun, 25 Apr 2021 14:24:43 GMT
Status: 302 Found
Content-Type: text/html;charset=utf-8
Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh
Content-Length: 0
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Runtime: 0.003242
X-Node: 69d60833-56c5-4364-94c1-6f0c5b12863f
X-Revision: 392798d237fc1aa5cd55cada10d2945773e741a8
Strict-Transport-Security: max-age=31536000; includeSubDomains
Via: 1.1 vegur

We see HTTP/1.1 302 Found and Location: https://raw.githubusercontent.com/roysubs/custom_bash/master/custom_loader.sh, not the real content of the latter.

So bash gets empty script to run, by default, curl does not do a second call to the new location, as does graphic webbrowsers usually do.

But you can do this to overcome the default:

curl -L https://git.io/Jt0fZ | bash

man curl gives this:

-L, --location
(HTTP) If the server reports that the requested page has moved to a different location (indicated with
a Location: header and a 3XX response code), this option will make curl redo the request on the new
place.

Pipe into a shell/bash script from Web URL

Try

echo "Test Data" | sh <(curl -sfL http://example.com/do-stuff.sh)

This needs to be run in bash or zsh or ksh due to the process substitition.

The reason why yours isn't working is that the sh process's stdin channel is already consumed by reading the source code, so you can't pipe data into it.

The process substitution acts like a file, so sh can get the source from the "file" and still read data from stdin.

How to run a shell script from a html page on raspberry Pi

This is my index.html page

Should it not be index.php ...



Related Topics



Leave a reply



Submit