-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxcgen2.sh
146 lines (110 loc) · 2.98 KB
/
lxcgen2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
clear
echo "#### LXC generator by John Mark C."
echo "#"
if [ "$1" == "clean" ]
then
# - START - Clean up ssh keys with lxc string
#
if [[ $(ls $HOME/.ssh/ | grep lxc) ]];
then
echo "# LXC ssh files found! Deleting.."
rm $HOME/.ssh/*lxc*
else
echo "# No LXC SSH key found. Already clean!"
fi
#
# - END - Clean up ssh keys with lxc string
# - START - Clean up LXC containers
#
if [[ $(lxc list | awk '!/NAME/{print $2}') ]];
then
echo "# LXC Containers found! Deleting.."
lxc delete $(lxc list | awk '!/NAME/{print $2}' | awk NF) --force
else
echo "# No LXC Containers found. Already clean!"
fi
#
# - END - Clean up LXC containers
lxc list
ls -al $HOME/.ssh/
echo "#"
echo "# Done!"
exit 1
fi
echo "# Hello! Enter the LXC container name please:"
read -p "# Enter LXC name: " lxcname
echo "# Alright! Let's generate the LXC container: $lxcname"
echo "#"
echo "#"
# 18.04
lxc launch ubuntu:18.04 $lxcname
# 16.04
#lxc launch ubuntu:16.04 $lxcname
echo "#"
echo "#Let's generate SSH-KEY gen for this LXC"
echo "#"
ssh-keygen -f $HOME/.ssh/id_lxc_$lxcname -N '' -C 'key for local LXC'
echo "#"
echo "# - START - Details from ssh key gen"
ls $HOME/.ssh/
cat $HOME/.ssh/id_lxc_$lxcname.pub
echo "#"
echo "#"
echo "# START - Info of LXC: ${lxcname}"
echo "#"
echo "# Trying to get the LXC IP Address.."
LXC_IP=$(lxc list | grep ${lxcname} | awk '{print $6}')
VALID_IP=^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
# START - SPINNER
#
sp="/-\|"
sc=0
spin() {
printf "\b${sp:sc++:1}"
((sc==${#sp})) && sc=0
}
endspin() {
printf "\r%s\n" "$@"
}
#
# - END SPINNER
while ! [[ "${LXC_IP}" =~ ${VALID_IP} ]]; do
# sleep 1
# echo "LXC ${lxcname} has still no IP "
# echo "Checking again.."
# echo "#"
# echo "#"
# lxc list
LXC_IP=$(lxc list | grep ${lxcname} | awk '{print $6}')
spin
# echo "IP is: ${LXC_IP}"
done
endspin
echo "# IP Address found! ${lxcname} LXC IP: ${LXC_IP}"
#lxc info $lxcname
echo "# "
echo "# Checking status of LXC list again.."
lxc list
echo "# Sending public key to target LXC: " ${lxcname}
echo "#"
#echo lxc file push $HOME/.ssh/id_lxc_${lxcname}.pub ${lxcname}/root/.ssh/authorized_keys
#Pause for 2 seconds to make sure we get the IP and push the file.
sleep 2
# Send SSH key file from this those to the target LXC
lxc file push $HOME/.ssh/id_lxc_${lxcname}.pub ${lxcname}/root/.ssh/authorized_keys --verbose
echo "#"
echo "# Fixing root permission for authorized_keys file"
lxc exec ${lxcname} -- chmod 600 /root/.ssh/authorized_keys --verbose
lxc exec ${lxcname} -- chown root:root /root/.ssh/authorized_keys --verbose
echo "#"
echo "# Adding SSH-key for this host so we can SSH to the target LXC."
eval $(ssh-agent);
ssh-add $HOME/.ssh/id_lxc_$lxcname
echo "#"
echo "# Done! Ready to connect?"
echo "#"
echo "# Connect to this: ssh -i ~/.ssh/id_lxc_${lxcname} root@${LXC_IP}"
echo "#"
echo "#"
echo "# Thank you for using this basic LXC SSH setup!"