Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Bash script remotely via cURL

I have a simple Bash script that takes in inputs and prints a few lines out with that inputs

fortinetTest.sh

read -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"

#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "SSC IP: $ip"
  printf "\n"
else
  echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
  exit
fi

enter image description here

I tried to upload them into my server, then try to run it via curl

enter image description here

I am not sure why the input prompt never kick in when I use cURL/wget.

Am I missing anything?

like image 609
code-8 Avatar asked Apr 05 '18 19:04

code-8


People also ask

How do I run a bash script from anywhere?

In order to run a Bash script from anywhere on your system, you need to add your script to your PATH environment variable. Now that the path to the script is added to PATH, you can call it from where you want on your system. $ script This is the output from script!

Can we use curl command in shell script?

The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.

How do I run a script anywhere in Linux?

Make the scripts executable: chmod +x $HOME/scrips/* This needs to be done only once. Add the directory containing the scripts to the PATH variable: export PATH=$HOME/scrips/:$PATH (Verify the result with echo $PATH .) The export command needs to be run in every shell session.


2 Answers

Your issue can be simply be reproduced by run the script like below

$ cat test.sh | bash
Enter a valid SSC IP address. Ex. 1.1.1.1

This is because the bash you launch with a pipe is not getting a TTY, when you do a read -p it is read from stdin which is content of the test.sh in this case. So the issue is not with curl. The issue is not reading from the tty

So the fix is to make sure you ready it from tty

read < /dev/tty -p "Enter SSC IP: $ip " ip && ip=${ip:-1.1.1.1}
printf "\n"

#check IP validation
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  echo "SSC IP: $ip"
  printf "\n"
else
  echo "Enter a valid SSC IP address. Ex. 1.1.1.1"
  exit
fi

Once you do that even curl will start working

vagrant@vagrant:/var/www/html$ curl -s localhost/test.sh | bash
Enter SSC IP:  2.2.2.2

SSC IP: 2.2.2.2
like image 127
Tarun Lalwani Avatar answered Oct 12 '22 23:10

Tarun Lalwani


With the curl ... | bash form, bash's stdin is reading the script, so stdin is not available for the read command.

Try using a Process Substitution to invoke the remote script like a local file:

bash <( curl -s ... )
like image 29
glenn jackman Avatar answered Oct 13 '22 01:10

glenn jackman