diff --git a/.travis.yml b/.travis.yml index d4745aa..9961317 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ language: bash sudo: false script: - - shellcheck $(grep -rIzl '^#![[:blank:]]*/bin/"bash\|sh"' ./ | grep ".git\|travis.yml" -v | cat) + - shellcheck rssh matrix: fast_finish: true diff --git a/rssh b/rssh index bebf7c5..dd2427b 100755 --- a/rssh +++ b/rssh @@ -21,11 +21,12 @@ # 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) -# 2018-08-28 Adding -F parameter to define the ssh_config file +# 2018-08-28 Adding -F parameter to define the ssh_config file (0.9) +# 2018-09-19 Allow for Include statements in ssh config (1.0) # --------------------------------------------------------------------------- PROGNAME=${0##*/} -VERSION="0.9" +VERSION="1.0" # define variables hops=0 @@ -35,7 +36,7 @@ declare -a hostname declare -a port declare -a user declare -a options -cfile="${HOME}/.ssh/config" +SSH_CF="${HOME}/.ssh/config" conffile=$(mktemp) ignore_default_route=0 ignore_proxies=0 @@ -43,12 +44,23 @@ host_list="" use_ssh=0 _USE_DEBUG=0 opt="" +Full_SSH_CF=$(mktemp) + +while read -r line; do + if [[ "$line" = Include* ]] ; then + incfile=$(echo "$line" | awk '{print $2}') + cat "$incfile" >> "$Full_SSH_CF" + else + echo "$line" >> "$Full_SSH_CF" + fi +done < "$SSH_CF" clean_up() { # Perform pre-exit housekeeping debug "" debug "removing $conffile" debug "" rm -f "$conffile" + rm -f "$Full_SSH_CF" return } @@ -72,7 +84,7 @@ add_hop() { l_host=$(echo "$l_host" | awk -F: '{print $1}') fi host[${hops}]=$l_host; - hostdata[${hops}]=$(sed -n "/[Hh]ost.* ${l_host}\( \|$\)/,/^[ ]*$/p" "${cfile}") + hostdata[${hops}]=$(sed -n "/[Hh]ost.* ${l_host}\( \|$\)/,/^[ ]*$/p" "${Full_SSH_CF}") 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}' ) @@ -219,7 +231,7 @@ while [[ -n $1 ]]; do -d | --debug) _USE_DEBUG=1 ;; -F | --file) - shift;cfile=$1 ;; + shift;SSH_CF=$1 ;; -id | --ignore-default) ignore_default_route=1 ;; -ip | --ignore-proxies) @@ -247,7 +259,7 @@ fi if [ ! -z "$DEFAULT_SSH_ROUTE" ] && [ "$ignore_default_route" -eq "0" ]; then 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" "${cfile}" | grep -c "Ignore_DEFAULT_SSH_ROUTE") + ignore_dr=$(sed -n "/[Hh]ost.* ${first_hop}\( \|$\)/,/^[ ]*$/p" "${Full_SSH_CF}" | 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 @@ -262,9 +274,10 @@ if [ ! -z "$DEFAULT_SSH_ROUTE" ] && [ "$ignore_default_route" -eq "0" ]; then fi fi +# if proxies are not ignored, check all hosts and add the proxy hops. for h in ${host_list}; do if [[ "$ignore_proxies" -eq 0 ]]; then - hdata=$(sed -n "/[Hh]ost.* ${h}\( \|$\)/,/^[ ]*$/p" "${cfile}") + hdata=$(sed -n "/[Hh]ost.* ${h}\( \|$\)/,/^[ ]*$/p" "${Full_SSH_CF}") 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}') @@ -278,7 +291,6 @@ for h in ${host_list}; do add_hop "$h" done - { i=${hops} while [ $i -gt 1 ]; do @@ -306,7 +318,7 @@ done fi echo "${options[${i}]}" echo " " - sed -n "/^Host \*\( \|$\)/,/^$/p" "${cfile}" | grep -v ProxyCommand + sed -n "/^Host \*\( \|$\)/,/^$/p" "${Full_SSH_CF}" | grep -v ProxyCommand } >> "$conffile" if [ ${_USE_DEBUG} -eq 1 ]; then