Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #set -x # uncomment/comment to enable/disable debug mode
- # name: tomato-installer.sh
- # version: 2.5.2, 24-jul-2022, by eibgrad
- # purpose: install pastebin script into tomato router
- # function usage()
- usage() {
- echo 'Usage: tomato-installer.sh [options] pastebin-id [ext=sh]'
- echo
- echo ' Install pastebin script into tomato router.'
- echo
- echo ' Installation directory hierarchy:'
- echo ' </path> (from --dir option)'
- echo ' /jffs/etc/config'
- echo ' /opt/etc/config'
- echo ' /tmp'
- echo
- echo ' Options:'
- echo ' --dir PATH installation directory (created as necessary)'
- echo " --pre NUM add prefix to filename (e.g., '01')"
- echo ' --wrap split configuration into wrapper & wrapped scripts'
- echo ' --nocom remove blank and comment-only lines'
- echo ' --comp same as --nocom, plus remove leading whitespace'
- echo ' --noprompt overwrite existing file(s) without prompting'
- echo ' -h,--help this usage information'
- echo
- echo ' # e.g., install script w/o comments and w/ bash (sh) extension'
- echo ' tomato-installer.sh --nocom nRtRhKf3'
- echo
- }
- # function query( message [default-reply] )
- query() {
- local reply
- read -p "$1 " reply < /dev/tty
- [ "$reply" ] && echo "$reply" || echo "$2"
- }
- # function is_mounted()
- is_mounted() { df | grep -Eq "\s+${1}\$"; }
- # function wraptize()
- wraptize() {
- local wrapper="$file"
- local wrapped args
- # configure proper extension for wrapped
- if [ "$ext" != 'sh' ]; then
- # replace extension w/ .sh
- wrapped="$(echo $file | sed 's/\.[^.]*$/.sh/')"
- else
- # strip extension
- wrapped="$(echo $file | sed 's/\.[^.]*$//')"
- fi
- # add preferred command-line arguments (optional)
- case "$pbid" in
- 'zkDfbTEJ') args="--ip $(nvram get lan_ipaddr) --port 80 --debug";;
- esac
- # move script to wrapped
- mv $file $wrapped
- # create and configure wrapper (which calls wrapped)
- echo '#!/bin/sh' > $wrapper
- echo "$wrapped$([ "$args" ] && echo " $args")" >> $wrapper
- chmod +x $wrapper
- echo "info: script installed (wrapper): $wrapper"
- echo "info: script installed (wrapped): $wrapped"
- }
- # function customize()
- customize() {
- _tomato_pia_port_forward() {
- local url='pastebin.com/raw/Xi2JGkne'
- local file="${file%.*}.callback"
- if [ ! -f $file ]; then
- # download sample callback script
- $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
- # verify file is bash script by locating shebang
- if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|\s+)'; then
- rm "$file"
- echo "error: file not found: $file"
- fi
- # mark file executable
- chmod +x "$file"
- echo "info: script installed: $file"
- fi
- }
- # script-specific customization (optional)
- case "$pbid" in
- 'zkDfbTEJ') _tomato_pia_port_forward;;
- esac
- }
- # function exit_1( [message] )
- exit_1() { [ "$1" ] && echo "$1"; exit 1; }
- # handle help/usage requests
- for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
- # try curl, fallback to wget
- which curl >/dev/null 2>&1 && GET_URL='curl -sLk' || GET_URL='wget -qO -'
- # process command line options/arguments
- while [ $# -gt 0 ]; do
- case $1 in
- --dir ) shift; file_dir="${1//[[:space:]]/}";;
- --pre ) shift; file_pre="${1//[[:space:]]/}";;
- --wrap ) wrap=;;
- --nocom ) nocom=;;
- --comp ) comp=;;
- --noprompt ) noprompt=;;
- *) break 2;;
- esac
- shift
- done
- # set pastebin-id
- [ "$1" ] && pbid="$1" || exit_1 'error: nothing to do'
- # set and verify extension
- ext="$([ "$2" ] && echo "${2//[[:space:]]/}" || echo sh)"
- echo $ext | grep -q '\.' && exit_1 "error: extension cannot contain '.'"
- # construct pastebin url for retrieving raw file
- url="pastebin.com/raw/$pbid"
- if [ ! "$file_dir" ]; then
- # locate storage
- if is_mounted '/jffs'; then
- file_dir='/jffs/etc/config'
- elif is_mounted '/opt'; then
- file_dir='/opt/etc/config'
- else
- file_dir='/tmp'
- echo 'warning: /jffs and /opt not mounted; using /tmp'
- fi
- fi
- # convert cryptic pastebin id to common name
- case "$1" in
- 'Yp0ptsjh') file='tomato-ovpn-client-killswitch';;
- 'MPnU5WrK') file='tomato-ovpn-client-watchdog';;
- 'UUUT8GiW') file='tomato-ovpn-remote-access';;
- 'GMUbEtGj') file='tomato-ovpn-split-advanced';;
- 'xEziw8Pq') file='tomato-ovpn-split-basic';;
- 'zkDfbTEJ') file='tomato-pia-port-forward';;
- '9csaJiUc') file='tomato-routing-policy-fix-v2';;
- 'CnjxdGtU') file='tomato-wol-port-forward';;
- *) file="pastebin-$1";;
- esac
- # check for existing files
- efiles="$(echo $file_dir/*$file*)"
- if [ "$efiles" != "$file_dir/*$file*" ]; then
- _is_callback() {
- echo "$1" | grep -q '\.callback$'
- }
- if [ ! ${noprompt+x} ]; then
- efile_count=0
- for efile in $efiles; do
- # ignore callback scripts
- _is_callback "$efile" || \
- { echo "warning: existing file: $efile"; let efile_count++; }
- done
- if [ $efile_count -gt 0 ]; then
- # obtain permission to overwrite existing file(s)
- while :; do
- case $(query 'Overwrite existing file(s) (yes/[no])?' no) in
- yes) break;;
- no) exit_1 'info: installation aborted';;
- esac
- done
- # delete existing files
- for efile in $efiles; do
- _is_callback "$efile" || rm -rf $efile
- done
- fi
- fi
- fi
- # option pre: add prefix to filename
- [ "$file_pre" ] && file="${file_pre}-$file"
- # create directory
- if ! mkdir -p "$file_dir" 2>/dev/null; then
- exit_1 "error: cannot create directory: $file_dir"
- fi
- # construct full path + filename + extension
- file="$file_dir/$file.$ext"
- # confirm file can be created through initialization
- if ! (> "$file") 2>/dev/null; then
- exit_1 "error: cannot create file: $file"
- fi
- # retrieve raw file from pastebin
- $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
- # verify file is bash script by locating shebang
- if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|\s+)'; then
- rm "$file"
- exit_1 "error: file not found: $pbid"
- fi
- # mark file executable
- chmod +x "$file"
- # option nocom: remove blank lines and non-functional comments
- [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|$))/d' "$file"
- # option comp: same as --nocom, plus remove leading whitespace
- [ ${comp+x} ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|$))/d' "$file"
- # option wrap: split configuration into wrapper & wrapped scripts
- [ ${wrap+x} ] && wraptize || echo "info: script installed: $file"
- # check for any script-specific customization
- customize
- exit 0
Add Comment
Please, Sign In to add comment