I have this CGI script, but how do I start a download in the browser?
#!/bin/bash
echo "Content-Type: text/plain"
echo
CUR=$(ls -t /var/www/html/certs/*$AUTHENTICATE_UID* | head -1)
if [ "$CUR" != "" ]; then
# start download in the browser
fi
Typically, you would emit the Content-Disposition header:
echo "Content-Type: text/plain"
CUR=$(ls -t /var/www/html/certs/*$AUTHENTICATE_UID* | head -1)
if [ "$CUR" != "" ]; then
echo 'Content-Disposition: attachment; filename="yourFile.txt"'
fi
echo # End of headers.
Related
I am trying to use the following bash script to login to a remote ftp and delete files older than N days old. Script says it is working and does not give an error - but files are not being deleted. What am I missing? Or is there a better way to do this? Keep in mind this is only a remote FTP and not SSH so I can NOT use the mtime function is why I am trying to do this. Can anyone help?
The usage is all commands - here is what I am using via ssh to run the script
./ftprem.sh -s ftp.server.com -u myusername -p mypassword -f /directory -d 3
#!/bin/bash
PROGNAME=$(basename $0)
OUTFILE="/tmp/ftplist.$RANDOM.txt"
CMDFILE="/tmp/ftpcmd.$RANDOM.txt"
ndays=14
print_usage() {
echo ""
echo "$PROGNAME - Delete files older than N days from an FTP server"
echo ""
echo "Usage: $PROGNAME -s -u -p -f (-d)"
echo ""
echo " -s FTP Server name"
echo " -u User Name"
echo " -p Password"
echo " -f Folder"
echo " -d Number of Days (Default: $ndays)"
echo " -h Show this page"
echo ""
echo "Usage: $PROGNAME -h"
echo ""
exit
}
# Parse parameters
options=':hs:u:p:f:d:'
while getopts $options flag
do
case $flag in
s)
FTPSITE=$OPTARG
;;
u)
FTPUSER=$OPTARG
;;
p)
FTPPASS=$OPTARG
;;
f)
FTPDIR=$OPTARG
;;
d)
ndays=$OPTARG
;;
h)
print_usage
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [[ -z "$FTPSITE" || -z "$FTPUSER" || -z "$FTPPASS" || -z "$FTPDIR" ]];
then
echo "ERROR: Missing parameters"
print_usage
fi
# work out our cutoff date
TDATE=`date --date="$ndays days ago" +%Y%m%d`
echo FTP Site: $FTPSITE
echo FTP User: $FTPUSER
echo FTP Password: $FTPPASS
echo FTP Folder: $FTPDIR
echo Removing files older than $TDATE
# get directory listing from remote source
ftp -i -n $FTPSITE <<EOMYF > /dev/null
user $FTPUSER $FTPPASS
binary
cd $FTPDIR
ls -l $OUTFILE
quit
EOMYF
if [ -f "$OUTFILE" ]
then
# Load the listing file into an array
lista=($(<$OUTFILE))
# Create the FTP command file to delete the files
echo "user $FTPUSER $FTPPASS" > $CMDFILE
echo "binary" >> $CMDFILE
echo "cd $FTPDIR" >> $CMDFILE
COUNT=0
# loop over our files
for ((FNO=0; FNO<${#lista[#]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
FMM=${lista[`expr $FNO+5`]}
FDD=${lista[`expr $FNO+6`]}
FYY=${lista[`expr $FNO+7`]}
if [[ $FYY == *\:* ]]
then
FDATE=`date -d "$FMM $FDD" +'%Y%m%d'`
else
FDATE=`date -d "$FMM $FDD $FYY" +'%Y%m%d'`
fi
# echo $FDATE
# check the date stamp
if [[ $FDATE -lt $TDATE ]];
then
echo "Deleting ${lista[`expr $FNO+8`]}"
echo "delete ${lista[`expr $FNO+8`]}" >> $CMDFILE
COUNT=$[$COUNT + 1]
fi
done
echo "quit" >> $CMDFILE
if [[ $COUNT -gt 0 ]];
then
cat $CMDFILE | tr -d "\r" > $CMDFILE
ftp -i -n $FTPSITE < $CMDFILE > /dev/null
else
echo "Nothing to delete"
fi
rm -f $OUTFILE $CMDFILE
fi
If this helps your debugging...
In the# Parse parameter section of the script, the options variable your have just before the case block has value options=':hs:u:p:f:d:' instead of options=':h:s:u:p:f:d:'
I thought i should point that out.
I have created a simple bash script that help you to mute alert within a few second without going into the console.
prerequisite:
jq needs to be installed on local
you should know some portion of your detector.
ex- If I want to mute alert with for detector name "prod-test memory utilization" then while mute alert via script I will simply type prod-test
Token should be created on signalfx so that we can authenticate from local
Ref: https://docs.splunk.com/Observability/alerts-detectors-notifications/mute-notifications.html#nav-Mute-alert-notifications
#!/bin/bash
echo -e "Please enter SFx token to authenticate\n"
read TOKEN
echo -e " \n"
echo -e "Please copy-paste any one muting option from below. \n a) Mute_Indefinitely \n b) Mute_By_Duration \n"
read MUTE_OPTION
echo -e " \n"
## this is comman name from detector in my case it is prod-test and prod-test2"
echo -e "Please copy-paste the server name which you wish to mute.
1. prod-test
2. prod-test2\n"
read SERVER_NAME
echo "Fatching detectors ids, Please wait for few seconds..."
DETECTOR_ID=$(curl -s -X GET "https://<your sfx url>/v2/detector?limit=2000" -H "Content-Type: application/json" -H "X-SF-TOKEN: "$TOKEN"" | jq -r '.results[] | select(.name | contains("'"$SERVER_NAME"'")).id')
############### MUTE BY DURATION FUNCTION ###############
mute_by_duration () {
START_TIME=`date +%s%N | cut -b1-13`
END_TIME=`date -d "+$min minutes" +%s%N | cut -b1-13`
curl -X "POST" "https://<your sfx url>/v2/alertmuting" \
-H 'X-SF-TOKEN: '$TOKEN'' \
-H 'Content-Type: application/json' \
-d $'{
"startTime": "'"$START_TIME"'",
"stopTime": "'"$END_TIME"'",
"filters": [
{
"property": "sf_detectorId",
"propertyValue": "'"$i"'"
}
]
}'
if [ $? -eq 0 ]; then
echo -e "\nDetector has been muted\n"
fi
}
############# MUTE ALERT INDEFINITELY ################
mute_indefinitely () {
curl -X "POST" "https://<your sfx url>/v2/alertmuting" \
-H 'X-SF-TOKEN: '$TOKEN'' \
-H 'Content-Type: application/json' \
-d $'{
"startTime": "",
"stopTime": "",
"filters": [
{
"property": "sf_detectorId",
"propertyValue": "'"$i"'"
}
]
}'
if [ $? -eq 0 ]; then
echo -e "\nDetector has been muted\n"
fi
}
#################### MUTING OPTION ###############
muting_rule() {
case "$MUTE_OPTION" in
"Mute_Indefinitely") echo -e "\n***** You have selected Mute_Indefinitely option, don't forget to unmute later *****\n";
for i in $DETECTOR_ID; do mute_indefinitely; done
;;
"Mute_By_Duration") echo -e "\n***** You have selected Mute_By_Duration option, This Alert will auto unmute after $min minutes *****\n";
for i in $DETECTOR_ID; do mute_by_duration; done
;;
*) echo "Please select correct option"
esac
}
############# SELECTION BASED ON MUTING OPTION #######################
if [ "$MUTE_OPTION" == Mute_By_Duration ]; then
echo -e "How much minutes you want to mute alert from current time? \n Example: Type "30" to mute for 30 mins \n"
read min
muting_rule
elif [ "$MUTE_OPTION" == Mute_Indefinitely ]; then
muting_rule
else
echo "Invalid Option"
fi
I have a simple shell script where I am doing a cURL and based on the response if empty or not wants to log a flag. I tried null check but it still goes to else part.
Script : test.sh
RESP=`curl -m 600 -X POST -H "Accept: application/json" ......` #curl response
#if [ -z "$RESP" ] && echo "Empty"
if [ ${#RESP[#]} -eq 0 ];
then
echo "APIName=Test1Api, TicketRaised=N"
else
echo "RESPONSE is NOT NULL"
echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
fi
Output > (even if Response ie "RESP" if empty it goes into else part)
RESPONSE is NOT NULL
APIName=Test1Api, HTTP_STATUS=[], totalTime=545, Response=[], TicketRaised=Y
I deduce from your answer that what you call an empty answer is the empty JASON string [].
So try:
RESP=`curl -m 600 -X POST -H "Accept: application/json" ......` #curl response
if [ $RESP = "[]" ]
then
echo "APIName=Test1Api, TicketRaised=N"
else
echo "RESPONSE is NOT NULL"
echo -e "APIName=Test1Api, HTTP_STATUS=$HTTP_STATUS, totalTime=$TIME_TAKEN, Response=$RESP, TicketRaised=Y"
fi
Be sure that there is no newline char after []. Otherwise, you will have to adapt the code.
I am currently trying to download data from (https://ladsweb.modaps.eosdis.nasa.gov/search/order). They provide a bash file (https://ladsweb.modaps.eosdis.nasa.gov/tools-and-services/data-download-scripts/) which I need to try and edit. I have a .csv file with all of the file extensions I need to download (specifically I need all VNP46A1 data for China from 2015 until now.
In pseudo-code form I would like to add the following:
FOR url_path IN url_list:
recurse "https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5000/VNP46A1/“+”$url_path"+”.h5" “your_directory”+”$url_path" "TOKEN_HERE"
I need to edit this bash to iterate over the files in the csv and download them into a folder for use later.
The bash file is as follows:
#!/bin/bash
function usage {
echo "Usage:"
echo " $0 [options]"
echo ""
echo "Description:"
echo " This script will recursively download all files if they don't exist"
echo " from a LAADS URL and stores them to the specified path"
echo ""
echo "Options:"
echo " -s|--source [URL] Recursively download files at [URL]"
echo " -d|--destination [path] Store directory structure to [path]"
echo " -t|--token [token] Use app token [token] to authenticate"
echo ""
echo "Dependencies:"
echo " Requires 'jq' which is available as a standalone executable from"
echo " https://stedolan.github.io/jq/download/"
}
function recurse {
local src=$1
local dest=$2
local token=$3
echo "Querying ${src}.json"
for dir in $(curl -s -H "Authorization: Bearer ${token}" ${src}.json | jq '.[] | select(.size==0) | .name' | tr -d '"')
do
echo "Creating ${dest}/${dir}"
mkdir -p "${dest}/${dir}"
echo "Recursing ${src}/${dir}/ for ${dest}/${dir}"
recurse "${src}/${dir}/" "${dest}/${dir}"
done
for file in $(curl -s -H "Authorization: Bearer ${token}" ${src}.json | jq '.[] | select(.size!=0) | .name' | tr -d '"')
do
if [ ! -f ${dest}/${file} ]
then
echo "Downloading $file to ${dest}"
# replace '-s' with '-#' below for download progress bars
curl -s -H "Authorization: Bearer ${token}" ${src}/${file} -o ${dest}/${file}
else
echo "Skipping $file ..."
fi
done
}
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--source)
src="$2"
shift # past argument
shift # past value
;;
-d|--destination)
dest="$2"
shift # past argument
shift # past value
;;
-t|--token)
token="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
if [ -z ${src+x} ]
then
echo "Source is not specified"
usage
exit 1
fi
if [ -z ${dest+x} ]
then
echo "Destination is not specified"
usage
exit 1
fi
if [ -z ${token+x} ]
then
echo "Token is not specified"
usage
exit 1
fi
recurse "$src" "$dest" "$token"
and a shortened (for testing purposes) csv file is given as:
/archive/allData/5000/VNP46A1/2015/001/VNP46A1.A2015001.h30v05.001.2019135185504.h5
/archive/allData/5000/VNP46A1/2015/002/VNP46A1.A2015002.h30v05.001.2019136091632.h5
/archive/allData/5000/VNP46A1/2015/003/VNP46A1.A2015003.h30v05.001.2019136075625.h5
/archive/allData/5000/VNP46A1/2015/004/VNP46A1.A2015004.h30v05.001.2019136081706.h5
/archive/allData/5000/VNP46A1/2015/005/VNP46A1.A2015005.h30v05.001.2019136084155.h5
/archive/allData/5000/VNP46A1/2015/006/VNP46A1.A2015006.h30v05.001.2019136084128.h5
/archive/allData/5000/VNP46A1/2015/007/VNP46A1.A2015007.h30v05.001.2019136085336.h5
/archive/allData/5000/VNP46A1/2015/008/VNP46A1.A2015008.h30v05.001.2019136103147.h5
/archive/allData/5000/VNP46A1/2015/009/VNP46A1.A2015009.h30v05.001.2019136100110.h5
Any help or suggestions will be much appreciated.
Kind regards
You want to create a bash script that will loop over each line to download the data that you need, using the script provided by NASA.
For example say the below script was a file called save-data.sh:
#!/bin/bash
while read p; do
./laads-data-download.sh -s $P -d "destination" -t "token"
echo "$p"
done <paths.txt
Example tree structure:
nasa-satellite-data
├── laads-data-download.sh
├── paths.txt
└── save-data.sh
I have a webservice provided at http://localhost/test/testweb
I want to write a script to check if webservice is up with curl
If there a curl parameter given, returns 200 OK ok true false so that I can use it is if-else block in linux script
curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
-s = Silent cURL's output
-L = Follow redirects
-w = Custom output format
-o = Redirects the HTML output to /dev/null
Example:
[~]$ curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
200
I would probably remove the \\n if I were to capture the output.
I use:
curl -f -s -I "http://example.com" &>/dev/null && echo OK || echo FAIL
-f --fail Fail silently (no output at all) on HTTP errors
-s --silent Silent mode
-I --head Show document info only
Note:
depending on needs you can also remove the "-I" because in some cases you need to do a GET and not a HEAD
Same as #burhan-khalid, but added --connect-timeout 3 and --max-time 5.
test_command='curl -sL \
-w "%{http_code}\\n" \
"http://www.google.com:8080/" \
-o /dev/null \
--connect-timeout 3 \
--max-time 5'
if [ $(test_command) == "200" ] ;
then
echo "OK" ;
else
echo "KO" ;
fi
That will check the headers via wget 2>&1pipes the stderr to stdout
grep filters
-O /dev/null just throws the content of the page
if [ "\`wget http://example.org/ -O /dev/null -S --quiet 2>&1 | grep '200 OK'\`" != "" ];
then
echo Hello;
fi;
I know not curl, but still a solution
I needed a better answer to this, so I wrote the script below.
The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.
#!/bin/bash
fakePhrase="verizon"
siteList=(
'http://google.com'
'https://google.com'
'http://wikipedia.org'
'https://wikipedia.org'
'http://cantgettherefromhere'
'http://searchassist.verizon.com'
)
exitStatus=0
function isUp {
http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
fakeResponse=`cat temp_isUp | grep $fakePhrase`
if [ -n "$fakeResponse" ]; then
http=$fakePhrase
fi
case $http in
[2]*)
;;
[3]*)
echo 'Redirect'
;;
[4]*)
exitStatus=4
echo "$1 is DENIED with ${http}"
;;
[5]*)
exitStatus=5
echo "$1 is ERROR with ${http}"
;;
*)
exitStatus=6
echo "$1 is NO RESPONSE with ${http}"
;;
esac
}
for var in "${siteList[#]}"
do
isUp $var
done
if [ "$exitStatus" -eq "0" ]; then
echo 'All up'
fi
rm temp_isUp
exit $exitStatus
Use this:
curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE