Skip to content

Instantly share code, notes, and snippets.

@taylor
Created November 17, 2011 11:09
Show Gist options
  • Select an option

  • Save taylor/1372925 to your computer and use it in GitHub Desktop.

Select an option

Save taylor/1372925 to your computer and use it in GitHub Desktop.

Revisions

  1. taylor revised this gist Nov 18, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ssh-srv-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/sh
    #!/bin/bash
    # Name: ssh-srv-wrapper
    #
    # Purpose: Check DNS SRV records and use the entry when possible.
    @@ -21,7 +21,7 @@ if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/$mydir:} which ssh`
    fi

    function usage() {
    usage() {
    #echo "usage: $me [args] [user@<host>] [args]"
    #echo "See man ssh (1)"
    $SSH
  2. taylor revised this gist Nov 18, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ssh-srv-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ DEBUG_ME=0
    me=`basename $0`
    mydir=$(dirname $0)

    function _dmsg() { if [ "${DEBUG_ME}" = 1 ] ; then echo $* ; fi ; }
    _dmsg() { if [ "${DEBUG_ME}" = 1 ] ; then echo $* ; fi ; }

    SSH=`which ssh`
    if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
  3. taylor revised this gist Nov 18, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions ssh-srv-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,9 @@ if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
    fi

    function usage() {
    echo "usage: $me [args] [user@<host>] [args]"
    echo "See man ssh (1)"
    #echo "usage: $me [args] [user@<host>] [args]"
    #echo "See man ssh (1)"
    $SSH
    }

    if [ "$1" = "" ] ; then
  4. taylor revised this gist Nov 17, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ssh-srv-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,6 @@ if [ "$check_srv" = "true" ] ; then
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host}|grep "has SRV")
    if [ $? = 0 ] ; then
    echo "srv: '$srv'"
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
  5. taylor revised this gist Nov 17, 2011. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ssh-srv-wrapper.sh
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,6 @@ user=${userhost%%@*}
    [ "$user" = "$host" ] && user=

    if [ "$check_srv" = "true" ] ; then
    _dmsg "Checking SRV"
    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host}|grep "has SRV")
    @@ -108,5 +107,5 @@ fi

    args=("${preargs[@]}" "${userat}${host}" "${postargs[@]}")

    _dmsg $SSH ${args[@]}
    _dmsg Executing $SSH ${args[@]}
    exec $SSH ${args[@]}
  6. taylor revised this gist Nov 17, 2011. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions ssh-srv-wrapper.sh
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,6 @@
    # Taylor Carpenter <[email protected]>


    #echo $* > /tmp/fooo
    #exit
    DEBUG_ME=0

    me=`basename $0`
  7. taylor revised this gist Nov 17, 2011. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -16,10 +16,7 @@ Run the script directly or feel free to rename or symlink to the name ssh. It w

    * Fork the project.
    * Make your feature addition or bug fix.
    * Add tests for it. This is important so I don't break it in a future version unintentionally.
    * Commit, do not mess with rakefile, version, or history.
    * If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
    * Send me a pull request. Bonus points for topic branches.
    * Send me a pull request -- not sure if that will work in a Gist...

    # Copyright

  8. taylor revised this gist Nov 17, 2011. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # Introduction

    *ssh-srv-wrapper* is bash shell script which tries to find a SSH SRV record for
    the first host and uses what is found rather than what was passed (if a valid
    record is found).

    # Install

    Run the script directly or feel free to rename or symlink to the name ssh. It will look for another ssh in your path to execute.

    # TODO

    * Validate host found in DNS. Potential security risk if DNS server returned shell esacpe sequences, etc.

    # Note on Patches/Pull Requests

    * Fork the project.
    * Make your feature addition or bug fix.
    * Add tests for it. This is important so I don't break it in a future version unintentionally.
    * Commit, do not mess with rakefile, version, or history.
    * If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
    * Send me a pull request. Bonus points for topic branches.

    # Copyright

    Released into the public domain.
  9. taylor renamed this gist Nov 17, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. taylor revised this gist Nov 17, 2011. No changes.
  11. taylor revised this gist Nov 17, 2011. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion myssh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,9 @@
    #
    # Taylor Carpenter <[email protected]>


    #echo $* > /tmp/fooo
    #exit
    DEBUG_ME=0

    me=`basename $0`
    @@ -82,10 +85,12 @@ user=${userhost%%@*}
    [ "$user" = "$host" ] && user=

    if [ "$check_srv" = "true" ] ; then
    _dmsg "Checking SRV"
    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host}|grep "has SRV")
    if [ $? = 0 ] ; then
    echo "srv: '$srv'"
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
  12. taylor revised this gist Nov 17, 2011. 1 changed file with 5 additions and 20 deletions.
    25 changes: 5 additions & 20 deletions myssh
    Original file line number Diff line number Diff line change
    @@ -44,26 +44,26 @@ i=0
    # Only parse args up to [user@]host
    for (( i=0; i<${#args[*]}; i++ )) ; do
    case ${args[i]} in
    -[$nao]|-[$nao]*[$nao]) _dmsg "arg with out opts '${args[i]}'"
    -[$nao]|-[$nao]*[$nao])
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    -*p) _dmsg "ignoring SRV"
    -*p)
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    check_srv=false # ignore SRV record
    ;;
    -*) _dmsg "option that takes arg"
    -*)
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    *) _dmsg "arg should be host '${args[i]}'"
    *)
    userhost="${args[i]}"
    args[i]=
    postargs=($(echo "${args[@]}"))
    @@ -77,21 +77,15 @@ if [ -z "$userhost" ] ; then
    exit 1
    fi

    _dmsg Should be good
    _dmsg ${newargs[@]}
    host=${userhost##*@}
    user=${userhost%%@*}

    [ "$user" = "$host" ] && user=
    _dmsg "host: $host"
    _dmsg "user: $user"

    if [ "$check_srv" = "true" ] ; then
    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    if [ $? = 0 ] ; then
    #_dmsg SRV: $srv
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
    @@ -100,25 +94,16 @@ if [ "$check_srv" = "true" ] ; then
    if [ -z "$host" ] ; then
    echo "Could not figure out hostname in SRV record"
    exit 1
    #else
    #echo "HOST: $host PORT: $port"
    fi
    fi
    fi
    #[ "$port" != "" ] && portopt="-p ${port}" || portopt=""
    [ "$port" != "" ] && preargs[${#preargs[*]}]="-p ${port}"
    _dmsg "After SRV lookup -- HOST: $host PORT: $port"
    fi

    [ "$user" != "" ] && userat="${user}@" || userat=""

    _dmsg "srv done"
    _dmsg "preargs '${preargs[@]}'"
    _dmsg "user@host: ${userat}${host}"
    _dmsg "postargs '${postargs[@]}'"

    args=("${preargs[@]}" "${userat}${host}" "${postargs[@]}")

    #echo $SSH ${portopt} ${userat}${host} $@
    #exec $SSH ${portopt} ${userat}${host} $@
    _dmsg $SSH ${args[@]}
    exec $SSH ${args[@]}
  13. taylor revised this gist Nov 17, 2011. 1 changed file with 36 additions and 18 deletions.
    54 changes: 36 additions & 18 deletions myssh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,27 @@
    #!/bin/sh
    # Name: ssh-srv-wrapper
    #
    # Purpose: Check DNS SRV records and use the entry when possible.
    #
    # This code is hereby released to the public domain and may be used for any
    # purpose whatsoever without permission or acknowledgment.
    #
    # Taylor Carpenter <[email protected]>

    DEBUG_ME=0

    me=`basename $0`
    mydir=$(dirname $0)

    function _dmsg() { if [ "${DEBUG_ME}" = 1 ] ; then echo $* ; fi ; }

    SSH=`which ssh`
    if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/$mydir:} which ssh`
    fi

    function usage() {
    echo "usage: $me [args] [user@<host>] [args]"
    #echo "usage: $me [user@<host>] [args]"
    echo "See man ssh (1)"
    }

    @@ -33,30 +44,29 @@ i=0
    # Only parse args up to [user@]host
    for (( i=0; i<${#args[*]}; i++ )) ; do
    case ${args[i]} in
    -[$nao]|-[$nao]*[$nao]) echo "arg with out opts '${args[i]}'"
    -[$nao]|-[$nao]*[$nao]) _dmsg "arg with out opts '${args[i]}'"
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    -*p) echo "ignoring SRV"
    #preargs=("$preargs" "${args[i]}" ${args[i+1]})
    -*p) _dmsg "ignoring SRV"
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    check_srv=false # ignore SRV record
    ;;
    -*) echo "option that takes arg"
    -*) _dmsg "option that takes arg"
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    *) echo "arg should be host '${args[i]}'"
    *) _dmsg "arg should be host '${args[i]}'"
    userhost="${args[i]}"
    args[i]=
    preargs[${#preargs[*]}]="${args[@]}"
    postargs=($(echo "${args[@]}"))
    break
    ;;
    esac
    @@ -67,21 +77,21 @@ if [ -z "$userhost" ] ; then
    exit 1
    fi

    echo Should be good
    echo ${newargs[@]}
    _dmsg Should be good
    _dmsg ${newargs[@]}
    host=${userhost##*@}
    user=${userhost%%@*}

    [ "$user" = "$host" ] && user=
    echo "host: $host"
    echo "user: $user"
    _dmsg "host: $host"
    _dmsg "user: $user"

    if [ "$check_srv" = "true" ] ; then
    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    if [ $? = 0 ] ; then
    #echo SRV: $srv
    #_dmsg SRV: $srv
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
    @@ -90,17 +100,25 @@ if [ "$check_srv" = "true" ] ; then
    if [ -z "$host" ] ; then
    echo "Could not figure out hostname in SRV record"
    exit 1
    else
    echo "HOST: $host PORT: $port"
    #else
    #echo "HOST: $host PORT: $port"
    fi
    fi
    fi
    #[ "$port" != "" ] && portopt="-p ${port}" || portopt=""
    [ "$port" != "" ] && preargs[${#preargs[*]}]="-p ${port}"
    fi
    echo "srv done"
    exit

    [ "$port" != "" ] && portopt="-p ${port}" || portopt=""
    [ "$user" != "" ] && userat="${user}@" || userat=""

    _dmsg "srv done"
    _dmsg "preargs '${preargs[@]}'"
    _dmsg "user@host: ${userat}${host}"
    _dmsg "postargs '${postargs[@]}'"

    args=("${preargs[@]}" "${userat}${host}" "${postargs[@]}")

    #echo $SSH ${portopt} ${userat}${host} $@
    exec $SSH ${portopt} ${userat}${host} $@
    #exec $SSH ${portopt} ${userat}${host} $@
    _dmsg $SSH ${args[@]}
    exec $SSH ${args[@]}
  14. taylor revised this gist Nov 17, 2011. 1 changed file with 81 additions and 21 deletions.
    102 changes: 81 additions & 21 deletions myssh
    Original file line number Diff line number Diff line change
    @@ -8,39 +8,99 @@ if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/$mydir:} which ssh`
    fi

    function usage() {
    echo "usage: $me [args] [user@<host>] [args]"
    #echo "usage: $me [user@<host>] [args]"
    echo "See man ssh (1)"
    }

    if [ "$1" = "" ] ; then
    echo "usage: $me [user@<host>] [args]"
    usage
    exit 1
    elif [ "$1" = "-V" -o "$1" = "-version" ] ; then
    exec $SSH -V
    else
    args=("$@")
    fi

    preargs=()
    postargs=()
    nao=1246AaCfgKkMNnqsTtVvXxYy
    userhost=
    check_srv=true
    i=0

    # Only parse args up to [user@]host
    for (( i=0; i<${#args[*]}; i++ )) ; do
    case ${args[i]} in
    -[$nao]|-[$nao]*[$nao]) echo "arg with out opts '${args[i]}'"
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    -*p) echo "ignoring SRV"
    #preargs=("$preargs" "${args[i]}" ${args[i+1]})
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    check_srv=false # ignore SRV record
    ;;
    -*) echo "option that takes arg"
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ((i++))
    preargs[${#preargs[*]}]="${args[i]}"
    args[i]=
    ;;
    *) echo "arg should be host '${args[i]}'"
    userhost="${args[i]}"
    args[i]=
    preargs[${#preargs[*]}]="${args[@]}"
    break
    ;;
    esac
    done

    if [ -z "$userhost" ] ; then
    usage
    exit 1
    fi

    host=${1##*@}
    user=${1%%@*}
    [ "$user" = "$host" ] && user=""
    echo Should be good
    echo ${newargs[@]}
    host=${userhost##*@}
    user=${userhost%%@*}

    echo "host = '$host'"
    shift
    [ "$user" = "$host" ] && user=
    echo "host: $host"
    echo "user: $user"

    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    if [ "$check_srv" = "true" ] ; then
    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    echo SRV: $srv
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
    port=${sport##* }

    if [ -z "$host" ] ; then
    echo "Could not figure out hostname in SRV record"
    exit 1
    else
    echo "HOST: $host PORT: $port"
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    if [ $? = 0 ] ; then
    #echo SRV: $srv
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
    port=${sport##* }

    if [ -z "$host" ] ; then
    echo "Could not figure out hostname in SRV record"
    exit 1
    else
    echo "HOST: $host PORT: $port"
    fi
    fi
    fi
    fi
    echo "srv done"
    exit

    [ "$port" != "" ] && portopt="-p ${port}" || portopt=""
    [ "$user" != "" ] && userat="${user}@" || userat=""

    echo $SSH ${portopt} ${userat}${host} $@
    #echo $SSH ${portopt} ${userat}${host} $@
    exec $SSH ${portopt} ${userat}${host} $@
  15. taylor revised this gist Nov 17, 2011. No changes.
  16. taylor revised this gist Nov 17, 2011. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions myssh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,11 @@
    #!/bin/sh

    me=`basename $0`
    mefull="$0"
    mydir=$(dirname $0)

    SSH=`which ssh`
    if [ "$me" = "ssh" -a "${mefull}" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/:$mydir} which ssh`
    if [ "$me" = "ssh" -a "$0" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/$mydir:} which ssh`
    fi

    if [ "$1" = "" ] ; then
    @@ -44,4 +43,4 @@ fi
    [ "$user" != "" ] && userat="${user}@" || userat=""

    echo $SSH ${portopt} ${userat}${host} $@
    exec $SSH ${portopt} ${userat}${host} $@
    exec $SSH ${portopt} ${userat}${host} $@
  17. taylor created this gist Nov 17, 2011.
    47 changes: 47 additions & 0 deletions myssh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/sh

    me=`basename $0`
    mefull="$0"
    mydir=$(dirname $0)

    SSH=`which ssh`
    if [ "$me" = "ssh" -a "${mefull}" = "${SSH}" ] ; then
    SSH=`PATH=${PATH/:$mydir} which ssh`
    fi

    if [ "$1" = "" ] ; then
    echo "usage: $me [user@<host>] [args]"
    exit 1
    fi

    host=${1##*@}
    user=${1%%@*}
    [ "$user" = "$host" ] && user=""

    echo "host = '$host'"
    shift

    echo $host | grep '[A-Za-z]' > /dev/null
    if [ $? = 0 ] ; then
    srv=$(host -s -W 1 -t SRV _ssh._tcp.${host})
    if [ $? = 0 ] ; then
    echo SRV: $srv
    shost=${srv##* }
    host=${shost%*.}
    sport=${srv%* $host.}
    port=${sport##* }

    if [ -z "$host" ] ; then
    echo "Could not figure out hostname in SRV record"
    exit 1
    else
    echo "HOST: $host PORT: $port"
    fi
    fi
    fi

    [ "$port" != "" ] && portopt="-p ${port}" || portopt=""
    [ "$user" != "" ] && userat="${user}@" || userat=""

    echo $SSH ${portopt} ${userat}${host} $@
    exec $SSH ${portopt} ${userat}${host} $@