-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathsetup-new-server.sh
executable file
·62 lines (53 loc) · 1.82 KB
/
setup-new-server.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
#!/usr/bin/env bash
function bail {
echo "************** ${1} **************"
exit 1
}
if [[ ${#} != 4 && ${#} != 5 ]]; then
echo "Usage: setup-new-server.sh <root@host> <local-ssh-public-key-file> <local-iptable-config-file> <ordinary-username> [ordinary-user-password]"
echo ""
echo " for example: setup-new-server.sh [email protected] ~/.ssh/id_rsa.pub output/iptables-application-server.cfg myuser password"
echo ""
echo " If the password is not specified, it will be input by the user interactively"
exit 1
fi
root_at_host=$1
ssh_key_file=$2
iptable_cfg_file=$3
ordinary_user=$4
ordinary_user_password=""
ordinary_user_password_confirm="other"
if [[ ${#} == 5 ]]; then
ordinary_user_password=$5
else
while [[ ${ordinary_user_password} != ${ordinary_user_password_confirm} ]]; do
echo -n "Password: "
read -s ordinary_user_password
echo ""
echo -n "Password (again): "
read -s ordinary_user_password_confirm
echo ""
if [[ ${ordinary_user_password} != ${ordinary_user_password_confirm} ]]; then
echo "Passwords don't match"
fi
done
fi
if ! [ -f ${ssh_key_file} ]; then
bail "Invalid SSH public key file"
fi
if ! [ -f ${iptable_cfg_file} ]; then
bail "Invalid IPTables configuration file"
fi
# Prepare the bundle to go to the server
rm -rf bundle
mkdir bundle
cp output/* bundle
cp ${iptable_cfg_file} bundle/iptables.cfg
cp ${ssh_key_file} bundle/ssh-public-key
rm bundle/iptables-*-server.cfg
# Transfer the bundle and execute it
scp bundle/* ${root_at_host}:/root
echo "Setting up the server. This might take a few minutes. There will be a log file in /root/setup.log after this is complete in case anything failed."
ssh -t ${root_at_host} "/root/setup-server.sh ssh-public-key iptables.cfg '${ordinary_user}' '${ordinary_user_password}' > /root/setup.log 2>&1"
# Clean up
rm -rf bundle