How to use SSH in bash script to download files from bitbucket? - bash

At my work,
the current method of downloading custom git pre-commit hooks (from a bitbucket repo) uses curl in a bash script as shown :
where $USERNAME, $PASSWORD and $build_support_url are previously assigned.
...<some code>...
# Download templates from http://swbuilds to home dir
echo "Downloading pre-commit.pl hook"
curl -u $USERNAME:$PASSWORD --fail --show-error --silent --output ~/.git_template/hooks/pre-commit $build_support_url/pre-commit.pl
echo "Downloading prepare-commit-msg.py hook"
curl -u $USERNAME:$PASSWORD --fail --show-error --silent --output ~/.git_template/hooks/prepare-commit-msg $build_support_url/prepare-commit-msg.py
echo "Downloading commit-msg.py hook"
curl -u $USERNAME:$PASSWORD --fail --show-error --silent --output ~/.git_template/hooks/commit-msg $build_support_url/commit-msg.py
# Force the execute bit to be set
chmod a+x ~/.git_template/hooks/*
# Also download the customer list used by the pre-commit hook
echo "Downloading customer list"
curl -u $USERNAME:$PASSWORD --fail --show-error --silent --output ~/.git_template/customer_list.txt $build_support_url/customer_list.txt
# Configure Git templates
git config --global init.templatedir '~/.git_template'
...<some more code>...
This downloads the pre-commit hooks from the link $build_support_url/pre-commit.pl and places them in the ~/.git_template folder.
However, since this process is using curl withHTTPS, the script will require a password everytime it is run.
To avoid that hassle, I am told to edit the script so that, it uses SSH to download the files (which doesn't require a password).
Any suggestions on how to use SSH in this script to obtain those files?
Thanks.
PS: I have a crude idea of backend and API (and only know the basic HTTP requests like GET and POST. Hope that gives a little more context to the situation).

You can store your credentials in ~/.netrc file so curl uses them as explained here.

The solution was just to use the 'git archive' command instead of curl

Related

How can I verify curl response before passing to bash -s?

I have a script that looks like:
curl -sSL outagebuddy.com/path/linux_installer | bash -s
Users can install a linux client for the site using the command that is provided to them. I'm thinking there should be an intermediary step that verifies the curl had a 2XX response and downloaded the content successfully before passing it to bash. How can I do that?
Without a user-managed temporary file:
if script=$(curl --fail -sSL "$url"); then
bash -s <<<"$script"
fi
If you don't mind having an intermediate file (which you certainly need if you want to make sure the curl command worked fully) then you can use:
if curl --fail -sSL <params> -o script.sh
then
bash script.sh
fi

Running long commands in powershell

I need to run a curl command on a container running in kubernetes cluster via PowerShell. Now, with curl, i need to pass various headers containing long token Strings. However, when i execute the command, the command string gets split in midway and terminal enters into newline mode from which I am not able to exit.
Below is the command.
curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prdcopy/projects/PROJECT-NOVEMBER2020-eCom-SPORT_STYLE-3 -H "authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSIsImtpZCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSJ9.eyJhdWQiOiJodHRwczovL2Rldi5hcGkuYWRpZGFzLmNvbS9jb3JwbWFya2V0L3ByZGNvcHkiLCJpc3MiOiJodHRwOi8vc3RzLXRlc3QuYWRpZGFzLWdyb3VwLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNTYzOTUyOTI5LCJuYmYiOjE1NjM5NTI5MjksImV4cCI6MTU2Mzk1NjUyOSwic3ViIjoic3ZjX3BjYXBpIiwiZGlzdGluZ3Vpc2hlZG5hbWUiOiJDTj1zdmNfcGNhcGksT1U9U2VydmljZUFjY291bnRzLE9VPVVzZXJzLE9VPUVNQWRtaW5pc3RyYXRpb24sREM9ZW1lYSxEQz1h456bnQsREM9Yml6IiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNTA4NzM2YTAtNzM5Mi00ODBkLTlmYWQtYWQ5NzUwMDI3ZjVhIiwiYXV0aG1ldGhvZCI6InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphYzpjbGFzc2VzOlBhc3N3b3JkUHJvdGVjdGVkVHJhbnNwb3J0IiwiYXV0aF90aW1lIjoiMjAxOS0wNy0yNFQwNzoyMjowOS43NTFaIiwidmVyIjoiMS4wIiwic2NwIjoidXNlcl9pbXBlcnNvbmF0aW9uIG9wZW5pZCJ9.kBrQO48rK_lkhDeZIQkW_If9lcYn5kERiif1VtlcXAPQ_j94KDGTfrmvF9f89EeyzExZSHFnCx0HyI8tvpeklEypWGchI4qG8V8z-AjTBrmKnX6mMSVLYaoh0TsLdeaFX-ByQ4TmyXzt5jje-CcS62w8P1J3aalVNhlTUp9e5rtP9qpttu2QmA482anQSsS6GN6ltJY9NyjfrW-ElZRrPXunIAf16jOjG0ehEUS8GpUJnc-wStJnF_zYuvnb-4m2GqzXqDHtpEvojg5N0Luq07z2mMpeEXbrGx-0fScjkSAsCJ8E-sFpbBYSXOCHfCslUPjM07kyzaZaa9tgWJGrGA" -H "cache-control: no-cache" -H "content-type: application/json" -H "project-id-type: API"
However, when i enter this command in power-shell after entering into the container, it looks like below.
PS R:\> kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh
/opt/app # curl -X GET http://sit-product-abcd-xyzx-adapter:8080/prdcopy/projects/PROJECT-NOVEMBER2020-eCom-SPORT_STYLE-
3 -H "authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSIsImtpZCI6
IlhSQVFGbGs3V0tNWnFzRm1ZUDcxaE55U3c4TSJ9.eyJhdWQiOiJodHRwczovL2Rldi5hcGkuYWRpZGFzLmNvbS9jb3JwbWFya2V0L3ByZGNvcHkiLCJpc3M
iOiJodHRwOi8vc3RzLXRlc3QuYWRpZGFzLWdyb3VwLmNvbS9hZGZzL3NlcnZpY2VzL3RydXN0IiwiaWF0IjoxNTYzOTUyOTI5LCJuYmYiOjE1NjM5NTI5Mjk
sImV4cCI6MTU2Mzk1NjUyOSwic3ViIjoic3ZjX3BjYXBpIiwiZGlzdGluZ3Vpc2hlZG5hbWUiOiJDTj1zdmNfcGNhcGksT1U9U2VydmljZUFjY291bnRzLE9
VPVVzZXJzLE9VPUVNQWRtaW5pc3RyYXRpb24sREM9ZW1lYSxEQz1h456bnQsREM9Yml6IiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiNTA4
NzM2YTAtNzM5Mi00ODBkLTlmYWQtYWQ5NzUwMDI3ZjVhIiwiYXV0aG1ldGhvZCI6InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphYzpjbGFzc2VzOlBh
c3N3b3JkUHJvdGVjdGVkVHJhbnNwb3J0IiwiYXV0aF90aW1lIjoiMjAxOS0wNy0yNFQwNzoyMjowOS43NTFaIiwidmVyIjoiMS4wIiwic2NwIjoidXNlcl9p
bXBlcnNvbmF0aW9uIG9wZW5pZCJ9.kBrQO48rK_lkhDeZIQkW_If9lcYn5kERiif1VtlcXAPQ
>
>
>
After this, i need to close the terminal and re-login into the container and kill the sh process manually.
How can i exit this curl command in power-shell? How can i execute long curl commands? I tried powershell_ise, but in that, i am not able to execute kubectl exec command. After executing that command, the terminal remains stuck forever.
Maybe you can copy the token as file into the container/pod and then pass it to curl as file.
More or less like this:
# Copy file
kubectl cp /tmp/token namespace/sit-pcapi-order-event-consumer-b6f4c8797-m578d:/tmp/token
# Pass the token as file instead of literal
kubectl exec -it sit-pcapi-order-event-consumer-b6f4c8797-m578d -- sh /opt/app # curl -X GET http://.../ -H #header_file $URL
Regards

Git post-receive hook, send curl commit message to Discord Webhook

I am trying to post an information-message in our discord every time someone pushes to the master.
I have a post-receive bash script looking like this:
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
tail=$(git log -1 --pretty=format:'%h %cn: %s%b' $newrev)
url='https://discordapp.com/api/webhooks/validapikey'
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST \
--data '{"content": "'$tail'"}' $url
fi
done
If I output tail to a file I get the expected string
6baf5 user: last commit message
but the message does not get posted on discord
If I replace $tail with "hello" it gets posted.
3 suggestions:
a) -d "{\"content\": \"${tail}\"}"
b) You can write this in the same language your project is, like Python or NodeJS, which is always better than bash (use the same name and make it executable)
c) To avoid this to be maintained in each dev machine, you can version this logic inside your repo using https://pypi.org/project/hooks4git or any other too that provides git hook management.

How to retrieve error code from cURL on shell

I know a similar question was posted, but I can't get it to work on my machine.
I tried the 1st answer from the mentioned question, i.e. response=$(curl --write-out %{http_code} --silent --output /dev/null servername) and when I echo $response I got 000 [Not sure if that is the desired output].
However, when trying to do so with my cURL command, I get no output.
This is my command:
curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
and I use it with
x=$(curl -k --silent --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt)
but when I try to echo $x all I get is a newline...
I know the cURL is failing, because when I run the same command, without --silent, I get curl: (7) Couldn't connect to server
This Q is tagged with both sh, bash because I've tried it on both with same results
I found this option which kind of helps (but I still don't know how to assign it to a variable, which should be easier than this...):
--stderr <file>
Redirect all writes to stderr to the specified file instead. If the file name is a plain '-', it is instead written to stdout.
If this option is used several times, the last one will be used.
When I use it like this:
curl -k --silent -S --stderr my_err_file --ftp-pasv --ftp-ssl --user C:is_for_cookies --cert localcert_cert.pem --key certs/localcert_pkey.pem ftps://10.10.10.10:21/my_file.txt
I can see the errors (i.e. curl: (7) Couldn't connect to server) inside that file.
I used --silent to suppress all output, and -S to un-suppress the errors, and the --stderr <file> to redirect them

Script works in the shell but not via GUI dd-wrt router

I have the following script working very well when I ssh into my dd-wrt router and issue the command
/opt/bin/curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "username#gmail.com" --mail-rcpt "username#gmail.com" --upload-file /mnt/mail.txt --user "username#gmail.com:password" --insecure
but when I issue the same commands via GUI; Administration/commands then the same script doesn't work.
any working solution will be a great help, thanks
Log in into the machine and type the command:
which sh
Put this value as a first line, preceding it with th #! characters, like
#!/bin/sh
What is your shell version? Look carefully at the first lines after you connected via ssh to your router. It's similar to:
BusyBox v1.15.3 (2011-11-24 00:44:20 CET) built-in shell (ash)
Enter 'help' for a list of built-in commands.
You may want to log the output of the command for debug purposes:
#!/bin/sh
/opt/bin/curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "username#gmail.com" --mail-rcpt "username#gmail.com" --upload-file /mnt/mail.txt --user "username#gmail.com:password" --insecure 2>&1 | logger -t $0
It will log the output using syslog facility. If it happens you have no syslog installed, you can log the output to a file:
#!/bin/sh
/opt/bin/curl --url "smtps://smtp.gmail.com:465" --ssl-reqd --mail-from "username#gmail.com" --mail-rcpt "username#gmail.com" --upload-file /mnt/mail.txt --user "username#gmail.com:password" --insecure >> /tmp/mylogfile 2>&1
Also, make sure, the script has an executable attribute set:
chmod a+rx /path/to/your/script

Resources