diff --git a/rssh b/rssh index cc071e1..39609c4 100755 --- a/rssh +++ b/rssh @@ -20,14 +20,14 @@ # 2016-06-29 Updated to allow user/ port on command line plus run commands (v0.5) # 2016-06-20 Tidy code, and check with shellcheck (v0.6) # 2017-01-09 Tidy code, and check with latest shellcheck (v.07) +# 2017-04-26 Allow proxycommand in config (0.8) # --------------------------------------------------------------------------- PROGNAME=${0##*/} -VERSION="0.7" +VERSION="0.8" # define variables hops=0 -default_hops=0 declare -a host declare -a hostdata declare -a hostname @@ -36,6 +36,7 @@ declare -a user declare -a options conffile=$(mktemp) ignore_default_route=0 +ignore_proxies=0 host_list="" use_ssh=0 _USE_DEBUG=0 @@ -68,44 +69,21 @@ add_hop() { l_port=$(echo "$l_host" | awk -F: '{print $2}') l_host=$(echo "$l_host" | awk -F: '{print $1}') fi - host[${hops}]=$l_host; - hostdata[${hops}]=$(sed -n "/Host.* ${l_host}\( \|$\)/,/^[ ]*$/p" ~/.ssh/config); - # ignore dulicate hop of DEFAULT_SSH_ROUTE when connecting with "rssh $DEFAULT_SSH_ROUTE" - if [[ "$l_host" == "$DEFAULT_SSH_ROUTE" ]] && [[ $hops -eq $((default_hops+1)) ]]; then - ((hops--)) - debug "ignoring hop $l_host as it is the default route anyway" - else - # ignore default routing if there is first hop has a comment "Ignore_DEFAULT_SSH_ROUTE" in the .ssh/config - if [[ "$(echo "${hostdata[${hops}]}" | grep -o "Ignore_DEFAULT_SSH_ROUTE")" == "Ignore_DEFAULT_SSH_ROUTE" ]]; then - if [ ${hops} -eq $((default_hops+1)) ]; then - debug "ignore default route through $DEFAULT_SSH_ROUTE as config files states Ignore_DEFAULT_SSH_ROUTE" - for (( i=1; i<=$((hops-1)); i++ )); do - host[${i}]="" - hostname[${i}]="" - hostdata[${i}]="" - port[${i}]="" - user[${i}]="" - done - hops=1 - host[${hops}]=$l_host - hostdata[${hops}]=$(sed -n "/Host.* ${l_host}\( \|$\)/,/^[ ]*$/p" ~/.ssh/config); - fi - fi - hostname[${hops}]=$(echo "${hostdata[${hops}]}" | grep -i "Hostname" | awk '{print $2}' ) - hostname[${hops}]=${hostname[${hops}]:=${l_host}} - f_port=$(echo "${hostdata[${hops}]}" | grep -i "^[ ]*port" | awk '{print $2}' ) - port[${hops}]=${l_port:=$f_port} - port[${hops}]=${port[${hops}]:=22} - user[${hops}]=$l_user - options[${hops}]=$(echo "${hostdata[${hops}]}" | \ - grep -iv "^[ ]*host" | \ - grep -iv "^[ ]*port" | \ + hostdata[${hops}]=$(sed -n "/[Hh]ost.* ${l_host}\( \|$\)/,/^[ ]*$/p" ~/.ssh/config); + hostname[${hops}]=$(echo "${hostdata[${hops}]}" | grep -i "Hostname" | awk '{print $2}' ) + hostname[${hops}]=${hostname[${hops}]:=${l_host}} + f_port=$(echo "${hostdata[${hops}]}" | grep -i "^[ ]*port" | awk '{print $2}' ) + port[${hops}]=${l_port:=$f_port} + port[${hops}]=${port[${hops}]:=22} + user[${hops}]=$l_user + options[${hops}]=$(echo "${hostdata[${hops}]}" | \ + grep -iv "^[ ]*host " | \ + grep -iv "^[ ]*hostname " | \ + grep -iv "^[ ]*port " | \ grep -iv "^[ ]*#"| \ grep -iv "^[ ]*ProxyCommand"); -# grep -iv "^[ ]*ProxyCommand"| \ -# grep -iv "^[ ]*DynamicForward"); - fi + debug "added ${hostname[${hops}]} with options ${options[${hops}]}" } error_exit() { @@ -130,6 +108,7 @@ help_message() { -h, --help Display this help message and exit. -v verbose output from ssh -id ignore default routing + -ip ignore proxies defined on config -n use plain ssh, with no copying of rc files -d debug -s nnnn socks port @@ -238,6 +217,8 @@ while [[ -n $1 ]]; do _USE_DEBUG=1 ;; -id | --ignore-default) ignore_default_route=1 ;; + -ip | --ignore-proxies) + ignore_proxies=1 ;; -n | --no-rc) use_ssh=1 ;; -s | --socks) @@ -259,16 +240,35 @@ fi # Main logic if [ ! -z "$DEFAULT_SSH_ROUTE" ] && [ "$ignore_default_route" -eq "0" ]; then - debug "default route is set to $DEFAULT_SSH_ROUTE" - # loop in case there is more than one hop in the default route - for h in ${DEFAULT_SSH_ROUTE}; do - ((default_hops++)) - debug "adding default hop $h" - add_hop "$h" - done + first_default=$(echo "${DEFAULT_SSH_ROUTE}" | awk '{print $1}') + first_hop=$(echo "${host_list}" | awk '{print $1}') + ignore_dr=$(sed -n "/[Hh]ost.* ${first_hop}\( \|$\)/,/^[ ]*$/p" ~/.ssh/config | grep -c "Ignore_DEFAULT_SSH_ROUTE"); + if [[ "$first_default" == "$first_hop" ]]; then + debug "ignoring default route as first hop is default route" + elif [[ "$ignore_dr" -gt 0 ]]; then + debug "ignoring $ignore_dr default route as first hop as comment Ignore_DEFAULT_SSH_ROUTE" + else + debug "unsing default route - $DEFAULT_SSH_ROUTE" + # loop in case there is more than one hop in the default route + for h in ${DEFAULT_SSH_ROUTE}; do + debug "adding default hop $h" + add_hop "$h" + done + fi fi for h in ${host_list}; do + if [[ "$ignore_proxies" -eq 0 ]]; then + hdata=$(sed -n "/[Hh]ost.* ${h}\( \|$\)/,/^[ ]*$/p" ~/.ssh/config); + h_proxy=$(echo "${hdata}" | grep -i "^[ ]*ProxyCommand" | awk '{print $3}') + if [[ "$h_proxy" == "-q" ]]; then + h_proxy=$(echo "${hdata}" | grep -i "^[ ]*ProxyCommand" | awk '{print $4}') + fi + if [[ ! -z "$h_proxy" ]]; then + debug "adding proxy - $h_proxy" + add_hop "$h_proxy" + fi + fi debug "adding hop $h" add_hop "$h" done @@ -289,7 +289,7 @@ done if [ ! -z "$socks" ] && [ $i -eq ${hops} ] ; then echo " DynamicForward localhost:${socks}" fi - echo " ProxyCommand ssh -F ${conffile} ${opt} ${host[${i}-1]} -W ${hostname[${i}]}:${port[${i}]}" + echo " ProxyCommand ssh -q -F ${conffile} ${opt} ${host[${i}-1]} -W ${hostname[${i}]}:${port[${i}]}" echo " " let i=i-1 done @@ -316,6 +316,7 @@ if [ ${_USE_DEBUG} -eq 1 ]; then fi debug "command: sshrc ${opt} -F $conffile ${host[${hops}]}" + if [ "$use_ssh" -eq 1 ]; then # shellcheck disable=SC2029 ssh ${opt} -F "$conffile" "${host[${hops}]}"