Bash wrapper to salt-ssh with SSH bastion support

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash -e
  2. # Compatible with OpenSSH >= 7.3 (August 2016)
  3. # Check OpenSSH version.
  4. less_than_required="7.2"
  5. installed=`ssh -V 2>&1 | grep -Eo "OpenSSH_[0-9]+\.[0-9]+" | grep -Eo "[0-9]+\.[0-9]+"`
  6. # Retrieve the highest between the installed version and the requirement.
  7. highest=`echo -e "$less_than_required\n$installed" | sort -rV | head -n 1`
  8. if [ $highest == $less_than_required ];then
  9. echo "Your OpenSSH version is too old for the script to run as it is, please upgrade to OpenSSH 7.3 (August 2016) or higher, or edit the script to adapt it to your version."
  10. exit 1
  11. fi
  12. # Extract the target domain.
  13. domain=$1
  14. # Get the bastion to use for that domain from the SSH configuration.
  15. # The -G flag is only available with OpenSSH 6.8 (March 2015) and higher: https://www.openssh.com/txt/release-6.8
  16. # The proxyjump option is only available with OpenSSH 7.3 (August 2016) and higher: https://www.openssh.com/txt/release-7.3
  17. bastion=`ssh -G $domain | grep proxyjump | cut -d' ' -f2`
  18. if [ -z $bastion ]; then
  19. echo "Couldn't determine the bastion to use to contact $domain, are you sure a \"ProxyJump\" instruction is set in a configuration block matching this host?"
  20. exit 1
  21. fi
  22. # SSH logs the connection closing to stderr, so we need to get rid of that.
  23. # Plus, dig via ssh appends a '\r' to the variable, which we want to get rid of too.
  24. ip=`ssh $bastion dig +short $domain 2> /dev/null | tr -d '\r'`
  25. if [ -z "$ip" ]; then
  26. echo "Couldn't lookup $domain..."
  27. exit 1
  28. fi
  29. shift # Removes $1 (the FQDN) from $@
  30. # Run the salt-ssh command with the right IP address and going through the bounce.
  31. # The proxyjump option is only available with OpenSSH 7.3 (August 2016) and higher: https://www.openssh.com/txt/release-7.3
  32. salt-ssh --roster=scan $ip --ssh-option=ProxyJump=$bastion $@