Skip to content

Instantly share code, notes, and snippets.

@iso2022jp
Last active December 18, 2015 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iso2022jp/5819579 to your computer and use it in GitHub Desktop.
Save iso2022jp/5819579 to your computer and use it in GitHub Desktop.
Queries information of associated dropbox account for Cacti.
#!/bin/bash
#
# Query Dropbox Account Information
#
# $referral
# $name
# $uid
# $country
# $normal
# $shared
# $quota
readonly locale='en-US'
readonly prog='dropbox-info'
readonly config=~/.${prog}-auth
readonly work=~/.${prog}-cache
readonly API_REQUEST_TOKEN='https://api.dropbox.com/1/oauth/request_token'
readonly API_ACCESS_TOKEN='https://api.dropbox.com/1/oauth/access_token'
readonly API_ACCOUNT_INFO='https://api.dropbox.com/1/account/info'
readonly URL_WEB_AUTH='https://www.dropbox.com/1/oauth/authorize'
function utime
{
echo $(date +%s)
}
if [ -f "$config" ]; then
. "$config"
else
if [ ! -t 0 ]; then
echo ''
echo 'Unconfigured'
echo 'U'
echo ''
echo 'U'
echo 'U'
echo 'U'
exit 1
fi
#echo -n 'App key: ' 1>&2
#read APPKEY
APPKEY=vnt3oysklmuzpj1
#echo -n 'App secret: ' 1>&2
#read APPSECRET
APPSECRET=wfsnm7tvopq93pd
# token
time=$(utime)
curl -s --show-error --globoff -f -o "$work" --data "locale=${locale}&oauth_consumer_key=${APPKEY}&oauth_signature_method=PLAINTEXT&oauth_signature=${APPSECRET}%26&oauth_timestamp=${time}&oauth_nonce=${RANDOM}" "$API_REQUEST_TOKEN"
if [ $? -ne 0 ]; then
echo 'Cannot acquire request token.' 1>&2
exit 1
fi
OAUTH_TOKEN_SECRET=$(sed -n 's/oauth_token_secret=\([0-9a-zA-Z]*\).*/\1/p' "$work")
OAUTH_TOKEN=$(sed -n 's/.*oauth_token=\([0-9a-zA-Z]*\)/\1/p' "$work")
if [ -z "$OAUTH_TOKEN" -o -z "$OAUTH_TOKEN_SECRET" ]; then
echo 'Invalid app key or secret.' 1>&2
exit 1
fi
# USER AUTH
echo 'Open following URL and accept Nicosys Management App.' 1>&2
echo "${URL_WEB_AUTH}?oauth_token=${OAUTH_TOKEN}" 1>&2
echo 'Press Enter to continue...' 1>&2
read
# API_ACCESS_TOKEN_URL
echo 'Acquiring access token...' 1>&2
time=$(utime)
curl -s --show-error --globoff -f -o "$work" --data "locale=${locale}&oauth_consumer_key=${APPKEY}&oauth_token=${OAUTH_TOKEN}&oauth_signature_method=PLAINTEXT&oauth_signature=${APPSECRET}%26${OAUTH_TOKEN_SECRET}&oauth_timestamp=$time&oauth_nonce=${RANDOM}" "$API_ACCESS_TOKEN"
if [ $? -ne 0 ]; then
echo 'Cannot acquire access token.' 1>&2
exit 1
fi
OAUTH_ACCESS_TOKEN_SECRET=$(sed -n 's/oauth_token_secret=\([0-9a-zA-Z]*\)&.*/\1/p' "$work")
OAUTH_ACCESS_TOKEN=$(sed -n 's/.*oauth_token=\([0-9a-zA-Z]*\)&.*/\1/p' "$work")
if [ -z "$OAUTH_ACCESS_TOKEN" -o -z "$OAUTH_ACCESS_TOKEN_SECRET" ]; then
echo 'Cannot acquire access token.' 1>&2
exit 1
fi
# store config
echo "APPKEY=${APPKEY}" > "$config"
echo "APPSECRET=${APPSECRET}" >> "$config"
echo "OAUTH_ACCESS_TOKEN=${OAUTH_ACCESS_TOKEN}" >> "$config"
echo "OAUTH_ACCESS_TOKEN_SECRET=${OAUTH_ACCESS_TOKEN_SECRET}" >> "$config"
fi
# query account info
time=$(utime)
curl -s --show-error --globoff -f -o "$work" --data "locale=${locale}&oauth_consumer_key=${APPKEY}&oauth_token=${OAUTH_ACCESS_TOKEN}&oauth_signature_method=PLAINTEXT&oauth_signature=${APPSECRET}%26${OAUTH_ACCESS_TOKEN_SECRET}&oauth_timestamp=${time}&oauth_nonce=${RANDOM}" "$API_ACCOUNT_INFO"
if [ $? -ne 0 ]; then
if [ ! -t 0 ]; then
echo ''
echo 'Error'
echo 'U'
echo ''
echo 'U'
echo 'U'
echo 'U'
exit 1
fi
echo 'Cannot retrieve account information.' 1>&2
exit 1
fi
referral=$(sed -n 's/.*"referral_link": *"\([^"]*\)".*/\1/p' "$work")
name=$(sed -n 's/.*"display_name": *"\([^"]*\)".*/\1/p' "$work")
uid=$(sed -n 's/.*"uid": *\([0-9]\+\).*/\1/p' "$work")
country=$(sed -n 's/.*"country": *"\([^"]*\)".*/\1/p' "$work")
quota=$(sed -n 's/.*"quota": *\([0-9]\+\).*/\1/p' "$work")
shared=$(sed -n 's/.*"shared": *\([0-9]\+\).*/\1/p' "$work")
normal=$(sed -n 's/.*"normal": *\([0-9]\+\).*/\1/p' "$work")
echo $referral
echo $name
echo $uid
echo $country
echo $normal
echo $shared
echo $quota
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment