Skip to content

Instantly share code, notes, and snippets.

@khaije1
Created February 1, 2011 08:57
Show Gist options
  • Save khaije1/805607 to your computer and use it in GitHub Desktop.
Save khaije1/805607 to your computer and use it in GitHub Desktop.
fun w/ Bash
# construct shell commands, then pass them to the shell interpreter
ls Life\ -\ S02E* | awk '// {print "mv" , "\"" $0 "\"" , "life__" $3 "--[0000]hdtv[eng]-" $5}' | sh
# construct shell commands, then pass them to the shell interpreter
ls Life\ -\ S02E* | awk '// {print "mv" , "\"" $0 "\"" , "life__" $3 "--[0000]hdtv[eng]-" $5}' | sh
#!/bin/bash
#NAME: curl-thru-vip.sh
#PUPOSE: test vhost by vip member
#NB: this method pre-supposes network access to vip members, otherwise it's useless
#REF: http://drewish.com/content/2010/03/using_curl_and_the_host_header_to_bypass_a_load_balancer
read -r -d msg_usage <<-EOF
usage: $0 target_url ip1 [ip2...]
entered: $0 $target_url ${members[@]}
EOF
init_opts=("$@")
target_url="$1"
shift && members=("$@")
curl_cmd="$(which curl)"
curl_opt=" --verbose --insecure "
err_usage(){
exitcode=$1
echo "$msg_usage"
exit $exitcode
}
parse_url(){
local url2parse="$1"
url_prefix="$(echo $url2parse | cut -d/ -f1-2)"
url_hostdomain="$(echo $url2parse | cut -d/ -f3)"
url_port="$(echo $url2parse | cut -d: -f3 | cut -d/ -f1)"
if [[ ! -z "$url_port" ]] ;then
#if string is not empty
url_port=":${url_port}"
fi
url_postfix="$(echo $url2parse | cut -d/ -f4-)"
}
main(){
q="'"
parse_url "$target_url"
echo "#INFO: $url_hostdomain resolves to $(nslookup ${url_hostdomain})"
curl_opt="$curl_opt --header ${q}Host: $url_hostdomain ${q}"
cmd_load=()
for ip2test in "${members[@]}" ;do
curl_target="${url_prefix}/${ip2test}${url_port}/${url_postfix}"
cmd_load+=("$curl_cmd $curl_opt $curl_target")
#echo "$curl_cmd $curl_opt $curl_target"
done
cmd_log=()
cmd_logparse="gawk '/HTTP/{print}'"
for cmd in "${cmd_load[@]}" ;do
cmd_log+=("$($cmd 2>&1)")
local count="$(( ${#cmd_log[@]} - 1 ))"
local cmd_resultcode="$(echo ${cmd_log[$count]} | $cmd_logparse )"
echo "$url_hostdomain $count :!: $cmd_resultcode :!: $cmd"
done
}
startOk(){
if [[ ! -z "$target_url" ]] && [[ ! -z "${member[1]}" ]] ;then
err_usage 1 "err: $target_url " "${member[@]}"
else
return 0
fi
}
if startOk ;then
main
fi
# open/monitor in new screen window
# 3600 is 10hrs , better options: plug into cron a/o git-annex
#NB: may fail at dest w/ multiple simultaneous uploads
watch -n 3600 rsync -avxhP --remove-source-files . sheva.lan:/home/khaije/documents/multimedia/.
#!/bin/bash
#NB: no spaces on either side during assignment
#NB: quote variables to catch 'multi-word' assignments, else only first 'word' is assigned
#NB: statically assign any interesting 'contextual' variables, these are mutable during normal operations
argnum="$#"
basename="$0"
url="$1"
savename="$2"
#NB: include space on either side of function name
function showMsg {
echo "$*"
}
function errUsage {
showMsg "usage error: script expects exactly 2 args, $varnum provided."
showMsg "example: $basename youtube_url \{savename.flv\}"
}
function getSavename {
if [[ ! $savename ]];then
savename=$( awk 'BEGIN{FS="="}{print $2 ".flv"}' <<< $url )
showMsg "save_name not provided, using $savename "
return 0
else
errUsage
return 1
fi
}
function validInit {
if [[ $argnum == 2 ]]; then
return 0
elif [[ $argnum == 1 ]] && getSavename ; then
return 0
fi
errUsage
return 1
}
function main {
showMsg "downloading $url to $savename ..."
wget $url -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" \
| sed -n '/^fmt_url_map/,/videoplayback/p' \
| sed -e :a -e '$q;N;5,$D;ba' \
| tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/' \
| wget -i - -O $savename
}
# main
if validInit ; then
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment