Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

download-vendor-asset.sh (Source)

#!/bin/sh
# File: download-vendor-asset.sh
# Location: blog exclusive
# Author: bgstack15
# Startdate: 2026-04-22-4 17:07
# Title: Download Vendor4 Assets
# Project: Vendor4 deployment
# Purpose: authenticate to release.example.com and download indicated assets as desired filename, directly from ec2 node, to avoid the download-to-laptop and upload-to-ec2 loop
# History:
# Usage:
# Reference:
# Improve:
#    Use traps to clean temp files if script is cancelled
# Dependencies:
#    curl
. ~/.config/helpcenter
_cj=~/.cookies
_silent="--silent"
_opts="-c ${_cj} -b ${_cj}"
_b="----separator47"
auth_to_vendor4(){
    rm -f "${_cj:-${HOME}/.cookies}"
    curl ${_silent} -L ${_opts} 'https://release.example.com/login' \
    --compressed \
    -X POST \
    -H "Content-Type: multipart/form-data; boundary=${_b}" \
    -H 'Referer: https://release.example.com/Login' \
    --data-binary $'--'"${_b}"$'\r\nContent-Disposition: form-data; name="username"\r\n\r\n'"${username}"$'\r\n--'"${_b}"$'\r\nContent-Disposition: form-data; name="password"\r\n\r\n'"${password}"$'\r\n--'"${_b}"$'--\r\n'
    printf '\n'
}
fetch_vendor4(){
    tmpfile1="$( mktemp )"
    for word in "${@}" ;
    do
        #-OJ saves the file, but we need to record the header used
        _output="$( curl -v -L -OJ ${_opts} "${word}" 2>&1 )"
        trimmed="$( echo "${_output}" | awk -F"'" '/filename=/{print $2}' )"
        dl="$( ls -1 b\'* 2>/dev/null )"
        if test -n "${dl}" ; then
            mv -f "${dl}" "${trimmed}"
            echo "${trimmed}" >> "${tmpfile1}"
        fi
    done
    cat "${tmpfile1}" | xargs md5sum
    rm -f "${tmpfile1:-NOTHINGTODEL}"
}