Skip to content

Instantly share code, notes, and snippets.

@tb3088
Last active October 4, 2017 22:23
Show Gist options
  • Select an option

  • Save tb3088/3d0b541c47d7aa8e682d0ea4eb43f358 to your computer and use it in GitHub Desktop.

Select an option

Save tb3088/3d0b541c47d7aa8e682d0ea4eb43f358 to your computer and use it in GitHub Desktop.

Revisions

  1. tb3088 revised this gist Oct 4, 2017. 1 changed file with 39 additions and 23 deletions.
    62 changes: 39 additions & 23 deletions ssh-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,12 @@

    # Usage:
    #
    # [VERBOSE='-v[vv]'] [PROFILE=profile] ssh-wrapper.sh [cmd] <host> [args]
    # [VERBOSE=-v]
    # [PROFILE=<profile>]
    # [IDENTITY=<key>]
    # ssh-wrapper.sh [cmd] <host> [args]
    #
    # just symlink to the wrapper to automatically set PROFILE
    # when 'cmd' is any of 'scp, sftp, ssh' or PROFILE ends in '.git', Screen is disabled

    [ ${#@} -gt 0 ] || { >&2 echo ' insufficient arguments'; exit 1; }

    @@ -18,52 +20,66 @@ esac
    : ${SSH:=`$WHICH ssh 2>/dev/null`}
    : ${SCP:=`$WHICH scp 2>/dev/null`}
    : ${SFTP:=`$WHICH sftp 2>/dev/null`}
    : ${SCREEN:=`$WHICH screen 2>/dev/null`}

    [ -x "$SSH" -a -x "$SCP" -a -x "$SFTP" ] || { >&2 echo " missing binaries: SSH=$SSH, SCP=$SCP, SFTP=$SFTP"; exit 2; }
    [ -x "$SSH" -a -x "$SCP" -a -x "$SFTP" ] || {
    >&2 echo -e " missing binaries!\n SSH=$SSH\n SCP=$SCP\n SFTP=$SFTP\n"
    exit 2;
    }


    # strip common script suffix off
    # compute from wrapper filename
    : ${PROFILE:=`basename -s .sh "$0"`}

    # disable Screen
    [ "${PROFILE##*.}" = "git" -o "${PROFILE##*.}" = "rsync" ] && SCREEN=""
    for s in ${NO_SCREEN:-git rsync}; do
    [ "${PROFILE##*.}" = "$s" ] && { unset SCREEN; break; }
    done


    function _ssh() {
    # environment:
    # SCREEN - if set but empty, disable use of screen
    # PROFILE - name of SSH configuration file
    # SCREEN - if set but empty, disable use of screen
    # PROFILE - name of SSH configuration file (-F)
    # IDENTITY - path to identity file (-i)

    shopt -s extglob
    local _screen _conf _cmd
    local _screen _conf _ident _cmd

    if [ "${TERM%.*}" = "screen" ]; then
    _screen=${SCREEN-`\which --skip-functions --skip-alias screen 2>/dev/null`};
    _screen="$SCREEN";
    TERM=${TERM/screen./}
    TERM=${TERM/screen/}
    : ${TERM:=linux}
    # : ${TERM:=linux}
    fi

    if [ -n "$PROFILE" ]; then
    #FIXME detect paths instead of assume default
    _conf="${HOME:-\~}/.ssh/config.$PROFILE"
    [ -r "$_conf" ] || { >&2 echo " config ($_conf) not found"; exit 1; }
    for _conf in {,${HOME:-\~}/.ssh/}{,config{_,.}}"$PROFILE"; do
    [ -r "$_conf" ] && break
    done
    [ -n "$_conf" ] || { >&2 echo "ERR! configuration ($PROFILE) not found"; return 1; }
    fi
    if [ -n "$IDENTITY" ]; then
    for _ident in {,${HOME:-\~}/.ssh/}{,id_}"$IDENTITY"; do
    [ -r "$_ident" ] && break
    done
    [ -n "$_ident" ] || { >&2 echo "ERR! identity ($IDENTITY) not found"; return 1; }
    fi

    _cmd=SSH
    case ${1^^} in
    SCP|SFTP)
    _cmd=${1^^}
    ;&
    SSH)
    shift; unset _screen
    SCP|SFTP)
    _cmd=${1^^}
    ;&
    SSH)
    shift;
    # disable Screen where persistent command output is helpful
    unset _screen
    ;;
    esac

    ${DEBUG:+set -x}
    ${_screen:+$_screen -t "${PROFILE:+${PROFILE#adfs-ftf-}:}$1" ${TERM:+-T ${TERM:-linux}}} \
    ${!_cmd} ${VERBOSE} ${_conf:+-F $_conf} "$@"
    ${_screen:+$_screen -t "${PROFILE:+${PROFILE#adfs-ftf-}:}$1" ${TERM:+-T $TERM}} \
    ${!_cmd} ${VERBOSE} ${_conf:+-F "$_conf"} ${_ident:+-i "$_ident"} "$@"
    }


    _ssh "$@"
    _ssh "$@"
  2. tb3088 created this gist Sep 29, 2017.
    69 changes: 69 additions & 0 deletions ssh-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    #!/bin/bash

    # Usage:
    #
    # [VERBOSE='-v[vv]'] [PROFILE=profile] ssh-wrapper.sh [cmd] <host> [args]
    #
    # just symlink to the wrapper to automatically set PROFILE
    # when 'cmd' is any of 'scp, sftp, ssh' or PROFILE ends in '.git', Screen is disabled

    [ ${#@} -gt 0 ] || { >&2 echo ' insufficient arguments'; exit 1; }

    case `uname -o` in
    Cygwin*)
    WHICH='\which --skip-functions --skip-alias'
    ;;
    esac
    : ${WHICH:=which}
    : ${SSH:=`$WHICH ssh 2>/dev/null`}
    : ${SCP:=`$WHICH scp 2>/dev/null`}
    : ${SFTP:=`$WHICH sftp 2>/dev/null`}

    [ -x "$SSH" -a -x "$SCP" -a -x "$SFTP" ] || { >&2 echo " missing binaries: SSH=$SSH, SCP=$SCP, SFTP=$SFTP"; exit 2; }


    # strip common script suffix off
    : ${PROFILE:=`basename -s .sh "$0"`}

    # disable Screen
    [ "${PROFILE##*.}" = "git" -o "${PROFILE##*.}" = "rsync" ] && SCREEN=""


    function _ssh() {
    # environment:
    # SCREEN - if set but empty, disable use of screen
    # PROFILE - name of SSH configuration file

    shopt -s extglob
    local _screen _conf _cmd

    if [ "${TERM%.*}" = "screen" ]; then
    _screen=${SCREEN-`\which --skip-functions --skip-alias screen 2>/dev/null`};
    TERM=${TERM/screen./}
    TERM=${TERM/screen/}
    : ${TERM:=linux}
    fi

    if [ -n "$PROFILE" ]; then
    #FIXME detect paths instead of assume default
    _conf="${HOME:-\~}/.ssh/config.$PROFILE"
    [ -r "$_conf" ] || { >&2 echo " config ($_conf) not found"; exit 1; }
    fi

    _cmd=SSH
    case ${1^^} in
    SCP|SFTP)
    _cmd=${1^^}
    ;&
    SSH)
    shift; unset _screen
    ;;
    esac

    ${DEBUG:+set -x}
    ${_screen:+$_screen -t "${PROFILE:+${PROFILE#adfs-ftf-}:}$1" ${TERM:+-T ${TERM:-linux}}} \
    ${!_cmd} ${VERBOSE} ${_conf:+-F $_conf} "$@"
    }


    _ssh "$@"