-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpassstore-vm-ssh.sh
executable file
·79 lines (66 loc) · 1.99 KB
/
passstore-vm-ssh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
die () {
echo ""
echo >&2 "$@"
echo ""
echo "Required parameters:"
echo " 1 - VM_PROVIDER: Provider which must be one of [hetzner,openstack]"
echo " 2 - VM_NAME: Name of the vm to connect to"
echo ""
echo "Optional parameters:"
echo " 3 - PASSWORD_STORE_DIR: If required if PASSWORD_STORE_DIR env var is not already defined."
echo ""
exit 1
}
VM_PROVIDER=$1
VM_NAME=$2
SSH_KEY=~/.ssh/id_rsa_snowdrop_${VM_PROVIDER}_${VM_NAME}
if [ -z ${PASSWORD_STORE_DIR+x} ];
then
[ "$#" -ge 3 ] || die "ERROR: 3 arguments required, $# provided"
PASSWORD_STORE_DIR=$3
else
[ "$#" -ge 2 ] || die "ERROR: 2 arguments required, $# provided"
fi
if [ "${VM_PROVIDER}" != 'hetzner' ] && [ "${VM_PROVIDER}" != 'openstack' ];
then
die "\$1: Provider must be one of [hetzner,openstack], ${VM_PROVIDER} provided";
fi
if [ ! -d ${PASSWORD_STORE_DIR} ];
then
die "Pass store directory ${PASSWORD_STORE_DIR} doesn't exist"
fi
if [ ! -f ${SSH_KEY} ];
then
SSH_KEY=~/.ssh/id_rsa_snowdrop_${VM_PROVIDER}
if [ ! -f ${SSH_KEY} ];
then
SSH_KEY=~/.ssh/id_rsa_snowdrop
if [ ! -f ${SSH_KEY} ];
then
echo "pass show ${VM_PROVIDER}/${VM_NAME}/id_rsa)" > ${SSH_KEY}
chmod 600 ${SSH_KEY}
fi
fi
fi
#set +e
#pass show ${VM_PROVIDER}/${VM_NAME}/floating_ip | awk 'NR==1{print $1}'
IP=$(pass show ${VM_PROVIDER}/${VM_NAME}/floating_ip | awk 'NR==1{print $1}')
if [ "$IP" = "" ];
#if [ ! "$?" = 0 ];
then
IP=$(pass show ${VM_PROVIDER}/${VM_NAME}/ansible_ssh_host | awk 'NR==1{print $1}')
fi
#set -e
PORT=$(pass show ${VM_PROVIDER}/${VM_NAME}/ansible_ssh_port | awk 'NR==1{print $1}')
if [ "$PORT" = "" ];
then
PORT=22
fi
USER=$(pass show ${VM_PROVIDER}/${VM_NAME}/os_user | awk 'NR==1{print $1}')
if [ "$USER" = "" ];
then
USER=$(pass show ${VM_PROVIDER}/${VM_NAME}/ansible_user | awk 'NR==1{print $1}')
fi
echo "### SSH COMMAND: ssh -i ${SSH_KEY} ${USER}@${IP} -p ${PORT} ${@:4}"
ssh -i ${SSH_KEY} ${USER}@${IP} -p ${PORT} "${@:4}"