Skip to content

Instantly share code, notes, and snippets.

@ddoc

ddoc/utils.sh

Created Jan 15, 2014
Embed
What would you like to do?
utility functions for foreman_hooks https://github.com/theforeman/foreman_hooks
#!/bin/bash
export FOREMAN_SERVER=$foremanserver/api
export FOREMAN_USER=apiuser
export FOREMAN_PASSWORD=$password
export FOREMAN_HOME=/usr/share/foreman
export PATH=$PATH:/usr/local/bin
reverseIp() {
local a i n
IFS=. read -r -a a <<< "$1"
n=${#a[@]}
for (( i = n-1; i > 0; i-- )); do
printf '%s.' "${a[i]}"
done
printf '%s' "${a[0]}"
}
export -f reverseIp
echolog() {
MYNAME=`basename "$0"`
echo "$MYNAME: $@"
echo "$MYNAME: $@" &>> $FOREMAN_HOME/log/hooks.log
}
export -f echolog
gethost() {
FQDN=$1
URL="https://$FOREMAN_SERVER/hosts/${FQDN}?format=json"
curl -s -H "Accept:application/json" \
-k -u $FOREMAN_USER:$FOREMAN_PASSWORD \
$URL
}
urlencode() {
arg="$1"
i="0"
while [ "$i" -lt ${#arg} ]; do
c=${arg:$i:1}
if echo "$c" | grep -q '[a-zA-Z/:_\.\-]'; then
echo -n "$c"
else
echo -n "%"
printf "%X" "'$c'"
fi
i=$((i+1))
done
}
urldecode() {
arg="$1"
i="0"
while [ "$i" -lt ${#arg} ]; do
c0=${arg:$i:1}
if [ "x$c0" = "x%" ]; then
c1=${arg:$((i+1)):1}
c2=${arg:$((i+2)):1}
printf "\x$c1$c2"
i=$((i+3))
else
echo -n "$c0"
i=$((i+1))
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment