Browse Source

Add script

Brendan Abolivier 6 years ago
commit
63b2b0c788
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 41 additions and 0 deletions
  1. 41
    0
      salt-ssh-bastion-wrapper.sh

+ 41
- 0
salt-ssh-bastion-wrapper.sh View File

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