-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-remote-action.sh
executable file
·81 lines (64 loc) · 2.45 KB
/
ssh-remote-action.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
80
#!/usr/bin/env bash
# Check if SSH_USERNAME is set
if [ -z "${SSH_USERNAME}" ]; then
echo "with.username is not set. Exiting..."
exit 1;
fi
# Check if SSH_HOST is set
if [ -z "${SSH_HOST}" ]; then
echo "with.host is not set. Exiting..."
exit 1;
fi
# Check if SSH_COMMAND is set
if [ -z "${SSH_COMMAND}" ]; then
echo "with.command is not set. Exiting..."
exit 1;
fi
# Default to SSH port 22
SSH_PORT=${SSH_PORT:-22}
# Define the default SSH arguments that apply for both key and password flows
SSH_ARGS="-o StrictHostKeyChecking=no -o LogLevel=ERROR -p ${SSH_PORT} ${SSH_USERNAME}@${SSH_HOST}"
eval "$(ssh-agent -s)" &>/dev/null
echo "SSH Agent started"
if [ -n "${SSH_KEY}" ]; then
echo "Using SSH key..."
# Write the private key to a file
echo "${SSH_KEY}" > ./private.key
# Set the permissions
chmod 600 ./private.key
# Check if the SSH_KEY_PASSPHRASE is empty
if [ -z "${SSH_KEY_PASSPHRASE}" ]; then
echo "SSH Key Passphrase is empty, just adding the key to the SSH agent..."
# Add the private key to the SSH agent
ssh-add -q ./private.key &>/dev/null
echo "SSH Identity Added"
else
echo "SSH Key Passphrase is set, creating a script to echo the passphrase and adding the key to the SSH agent..."
# Create a script that will echo the passphrase
printf "%s\n" "#!/usr/bin/env bash" "echo ${SSH_KEY_PASSPHRASE}" > ./ssh-passphrase
chmod +x ./ssh-passphrase
# Set the permissions
DISPLAY=1 SSH_ASKPASS="./ssh-passphrase" ssh-add -q ./private.key &>/dev/null
echo "SSH Identity Added"
fi
if [ "${SILENT}" == "true" ]; then
echo "Executing the SSH command silently..."
ssh ${SSH_ARGS} "sh -c '${SSH_COMMAND}' > /dev/null 2>&1"
else
echo "Executing the SSH command..."
ssh ${SSH_ARGS} "${SSH_COMMAND}"
fi
elif [ -n "${SSH_PASSWORD}" ]; then
echo "Using an SSH password. It is recommended you instead setup a public/private key pair and use SSH keys instead"
# Execute the SSH command
if [ "${SILENT}" == "true" ]; then
echo "Executing the SSH command silently..."
sshpass -p "${SSH_PASSWORD}" ssh ${SSH_ARGS} "sh -c '${SSH_COMMAND}' > /dev/null 2>&1"
else
echo "Executing the SSH command..."
sshpass -p "${SSH_PASSWORD}" ssh ${SSH_ARGS} "${SSH_COMMAND}"
fi
else
echo "Neither inputs.key nor inputs.password are set. Exiting..."
exit 1;
fi