List-Connections bash script
Overview
You can view Cloud Connections in the web admin portal, but if you need to list many connections and their details at once, it is easier and less error-prone to script this process.
This article goes over how to list a Cloud Connection via the command line using our list-connections.sh
script.
IMPORTANT: If you're on version 3.5.0 or newer of SFTP Gateway, make sure to edit the script on line 59
so the endpoint for generating the OAuth token is set to http://127.0.0.1:8080/login
NOT http://127.0.0.1:8080/oauth/token
.
Install dependencies
The list-connections script relies on jq
, which is a command line utility for working with json objects.
SSH into the VM and run the following commands to install this:
sudo su
snap install jq
Create the script
In this section, you will create the bash script.
Paste in the following commands:
cd /usr/local/bin
wget https://thorntech-products.s3.amazonaws.com/sftpgateway/list-connection-script/list-connections.sh
chmod +x list-connections.sh
These commands download the list-connections.sh
script, and make it executable.
Create a credentials file
The list-connections script uses the credentials of your web admin user. So, you will need to save these credentials in a text file.
First, create a credentials file:
cd /usr/local/bin/
touch credentials.txt
nano credentials.txt
Paste the following properties into the file:
admin.username=
admin.password=
Make sure you populate these values with your web admin credentials. For example:
admin.username=bryce
admin.password=password
Usage
Run the script to list all Cloud Connections and their details:
list-connections.sh -f credentials.txt
If you'd like to get the value for a single Cloud Connection, you can run this command (Since the script writes all the JSON output to the user.json file, make sure you run the script normally once before trying to get the details for a specific connection):
cat user.json | jq '.cloudConnections[] | select(.name == "Azure")'
Just change "Azure" to the name of whichever Cloud Connection you'd like to get the details of, here is an example of the output:
{
"createdDate": "2024-06-27T17:04:52.984314Z",
"id": 5,
"modifiedDate": "2024-06-27T17:23:00.514026Z",
"basePrefix": "https://bryce.blob.core.windows.net/demo",
"cloudProvider": "azure",
"connectivity": {
"canList": true,
"canRead": true,
"canWrite": true
},
"name": "Azure",
"notes": "Imported on 2024-06-27",
"useInstanceCredentials": false,
"basePrefixUrl": null,
"hnsEnabled": null,
"basePrefixAsHttpUrl": "https://bryce.blob.core.windows.net/demo/",
"accountName": "bryce",
"connectionStringSet": true
}
Script contents
Here are the contents of the list-connections.sh
file:
#!/bin/bash
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--file)
CREDENTIAL_FILE="$2"
shift
shift
;;
*)
break
;;
esac
done
if [[ `whoami` != "root" ]]; then
echo ""
echo "Please run this script using sudo or as root."
USAGE_FLAG=true
fi
function credential-file-usage {
echo ""
echo "Please provide a path to a valid credential file, which contains the following contents:"
echo ""
echo "admin.username=admin"
echo "admin.password=$(generaterandomstring 15)"
}
function extractPropValueFromSourceFile {
local prefix="${1}"
local str=`grep "${prefix}" ${2} 2>/dev/null`
echo "${str#$prefix}" | xargs
}
if [[ -f $CREDENTIAL_FILE ]]; then
ADMIN_USERNAME=$(extractPropValueFromSourceFile admin.username= $CREDENTIAL_FILE)
ADMIN_PASSWORD=$(extractPropValueFromSourceFile admin.password= $CREDENTIAL_FILE)
fi
if [[ ! -f $CREDENTIAL_FILE ]]; then
credential-file-usage
USAGE_FLAG=true
elif [[ -z $ADMIN_USERNAME ]] || [[ -z $ADMIN_PASSWORD ]]; then
credential-file-usage
USAGE_FLAG=true
fi
if [[ $USAGE_FLAG = true ]]; then
echo ""
usage
fi
APPLICATION_PROPERTIES=/opt/sftpgw/application.properties
CLIENT_ID=$(extractPropValueFromSourceFile "security.client-id=" ${APPLICATION_PROPERTIES})
CLIENT_SECRET=$(extractPropValueFromSourceFile "security.client-secret=" ${APPLICATION_PROPERTIES})
RESPONSE=$(curl --location --request POST 'http://127.0.0.1:8080/login' -u ${CLIENT_ID}:${CLIENT_SECRET} --form 'grant_type="password"' --form "username=\"${ADMIN_USERNAME}\"" --form "password=\"${ADMIN_PASSWORD}\"" --form 'scope="read"' --silent)
ACCESS_TOKEN=$(echo $RESPONSE | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])")
curl -s --insecure --location --request GET 'https://127.0.0.1:443/backend/3.0.0/cloudconnections' --header 'Content-Type: application/json' --header "Authorization: Bearer ${ACCESS_TOKEN}" > user.json
cat user.json | jq '.'