Bash script for LDAP website API requests - bash

I want to automate and create a bash script where I have this call:
curl --header "Authorization: Bearer <PANDA TOKEN HERE>" https://www.websitehere.com/api/v2/groups/GROUP NAME/details
My goal is for the bash script to iterate over a list of group names (GROUP NAME).I have my panda token working. Unfortunately I have no previous experience with bash. I have installed WSL and curl.
I tried this bash script, but it doesn't work:
#!/bin/bash
group_details=("acton_team_coordinators" "acs-peripherals" "admingrocery" "ae-central-leads")
for group in "${group_details[#]}"
do
curl -v GET " https://www.websitehere.com/api/v2/groups/acton_team_coordinators/details" -H "<PANDA TOKEN HERE>”
done
I got error in CMD like:
enter image description here

You forgot to code the "group" variable as a variable, by prefixing it with the "$".
It is good practice to surround variable strings with a pair of braces ("{}"), because there are cases where confusion can occur in determining what is the actual variable name.
You also had space inside the double-quotes, before the "http ...". I am not sure if that could create issues, but best to remove that space and avoid them.
The modified script would look like this:
#!/bin/bash
out_capture="somewhere"
group_details=("acton_team_coordinators" "acs-peripherals" "admingrocery" "ae-central-leads")
for group in "${group_details[#]}"
do
curl -v GET "https://www.websitehere.com/api/v2/groups/${group}/details" -H "<PANDA TOKEN HERE>" >${out_capture}/${group}.out
done

Related

Post data on slack channel

I am using shell script to find the tps and error count in a single day using shell script and store the data in a txt file.
Now i want to use the same shell script to post data form that result file onto a slack group, but sometimes command gives bad request, sometimes Invalid Payload and sometimes it works.
curl -X -POST -H --silent --data-urlencode "payload={\"text\": \"$(cat <filepath>/filename.txt)\"}" "<slack url>"
Please help

Cannot use source to execute Bash script on zsh

I have a Mac, on which I have installed and configured zsh to be my default shell. It has worked very well with simple Bash scripts. Until today.
I was trying to set up my AWS CLI access with MFA and used a script to do the same. Since I have multiple accounts, I used an aws_accounts file to store the account numbers with 400 permissions on it. (No real reason why, just felt like it and it works).
I then source this file and get to the part where I need to provide my MFA key, and those work too. The last step, where I export the access key, secret key and session token, works as long as the script is executing, but once done, echoing it shows a blank, because it now no longer has set it in the current shell.
I know the workaround is to do source aws_mfa_access default to run the script, but it doesn't work and I get a bad substitution error. I've tried various combinations of #!/usr/bin/env bash, #!/bin/bash and #!/usr/bin/env zsh, but to no avail. What's going on?
[aws_account_numbers]
default_account_number=<number>
sandbox_account_number=<number>
production_account_number=<number>
[aws_mfa_access]
#!/usr/bin/env bash
source aws_account_numbers
AWS_ENV="${1}"
AWS_ACCT_NBR="${AWS_ENV}"_account_number
#export your aws access key that matches with your account, username
export AWS_PROFILE="${AWS_ENV}"
#If there are existing environment variables set, this can cause issues so we unset them first
unset AWS_SESSION_TOKEN
unset AWS_SECRET_ACCESS_KEY
unset AWS_ACCESS_KEY_ID
#Set the serial number of your MFA token
MFA="arn:aws:iam::${!AWS_ACCT_NBR}:mfa/codingincircles"
#Get the code from the MFA device
echo "Please enter the MFA code for the ${AWS_ENV} account: "
read -r code
#Get the credentials from AWS and store the response in a variable
creds="$( aws sts get-session-token --serial-number "${MFA}" --token-code "${code}" )"
#Parse the response into separate variables
AWS_ACCESS_KEY_ID="$( echo "${creds}" | jq -r .Credentials.AccessKeyId )"
AWS_SECRET_ACCESS_KEY="$( echo "${creds}" | jq -r .Credentials.SecretAccessKey )"
AWS_SESSION_TOKEN="$( echo "${creds}" | jq -r .Credentials.SessionToken )"
#Display the keys to the user for reference/confirm proper working
echo "${access_key}"
echo "${secret_key}"
echo "${session_token}"
#Set the appropriate environment variables
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}"
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"
export AWS_SESSION_TOKEN="${AWS_SESSION_TOKEN}"
EDIT: Two things of note:
The parameter substitution is not an issue at all. It works just fine as is, though the other methods suggested in the comments and answer work too. (I used the jq -r tip and it works like a charm! Thank you!)
I removed the source command in my script (on line 3) and then was able to, without any errors of any kind, able to invoke my script as source aws_mfa_access default. The exported variables persisted and I am able to use the CLI with no problems.
So why does this not like me using source in my script? I've also edited the script to reflect some of the changes.
Indirect parameter expansion is different in zsh than it is in bash
MFA="arn:aws:iam::${(P)AWS_ACCT_NBR}:mfa/codingincircles"
However, you can avoid indirect parameter expansion in the first place by using an associative array to store the account numbers.
typeset -A account_numbers
account_numbers[default]=...
account_numbers[sandbox]=...
account_numbers[production]=...
Then
MFA="arn:aws:iam::${account_numbers[$1]}:mfa/codingincircles"
Finally, source is not an external command that means "execute a bash script here". It's a shell built-in that executes a file in the current shell. If the current shell is bash, it will attempt to execute the contents of a file as a bash script; if it's zsh, as a zsh script; if it's dash, as a dash script.

Curl command in Bash script returns no URL specified

Trying to run a curl command to a test page results in "no URL specified!" when attempting to run via a bash script. This is a test that runs through a proxy (1.2.3.4 in the code example)
The command runs as expected from the command line. I've tried a few variations of quotes around the components of the command however still manage the same "no URL specified" result.
echo $(curl -x http://1.2.3.4:80 https://example.company.com)
The expected result is an HTML file, however instead the above issue is found only when run from this small bash script file.
The command itself runs just fine as "curl -x http://1.2.3.4:80 https://example.company.com" from the command line.
Appreciate in advance any help you can provide!
I have literally edited it down to the following. Everything else is commented out. Still the same error.
#!/bin/bash
curl -x http://1.2.3.4:80 https://example.company.com
In your example you want to use double quotes around the subshell and single qutes for the curl parameters
Make sure to set the correct shebang (#!/bin/bash).
You could also try to run it as:
cat <(curl -x http://1.2.3.4:80 https://example.company.com)
However I am not able to reproduce your example

Bash function to automate curl POST authentication not executing as intended

I am using Ubuntu bash shell on a windows machine and i am trying to create a function to test my endpoints of a spring application. First i must authenticate with the server via the following curl command. The output of the statement run is below.
$> curl -i -X "POST" -d"${auth}" -H"${contentType}" "${host}/login"
When i copy and paste this command into a function it seems to blow up leading to non-execution of curl as intended which should have the same output as above.
$> function springlogin(){curl -i -X "POST" -d"${auth}" -H"${contentType}" "${host}/login";};
$> springlogin
What am i missing here? Is this due to some variable expansion im not aware of? Or something else entirely.
My end goal would be to use the output of this function and use it to authorize my endpoint api calls if i happen to be on my command line.
Thanks to Gordon Davisson i figured it out. This is something that we do at work but i didn't think to do it in my home code. I need to unset the function in my profile before i declare it again.
unset springlogin
function springlogin(){curl -i -X "POST" -d"${auth}" -H"${contentType}" "${host}/login";};

bash how to allow interactive session when runing through a pipe

I'm creating a script that I want people to run with
curl -sSL install.domain.com | bash
As RVM, oh-my-zsh and many others does,
However, I'm having issues because my script is interactive (it uses read and select, and the user is not being prompted, the script just skip those steps as is being executed with a |.
I've tried adding {} to my whole code.
I was thinking in ask the script to download himself again and put in tmp folder, and execute from there, not sure if that will work.
You can explicitly tell read to read from the terminal using
read var < /dev/tty
The solution i found is ask user to run it like:
bash <( curl -sSL install.domain.com )
This way script is passed as an argument and standard input remains untouched.
This is a non-problem, as users should not be executing code directly from the stream. The user should be downloading the code to a file first, then verifying that the script they receive has, for example, an MD5 hash that matches the hash you provide on your website. Only after confirming that the script they receive is the script you sent should they execute it.
$ curl -sSL install.domain.com > installer.sh
$ md5sum installer.bash # Does the output match the hash posted on install.domain.com?
$ bash installer.bash

Resources