Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- usage() {
- cat <<EOF
- $1 [-h|--help] [-u|--username user] [-p|--password pass] [-k|--key key] [-i|--ip ip] hostname
- EOF
- return 0
- }
- # get 'new' ip from the last entry in /etc/lxc/hosts.conf
- get_new_ip() {
- last=$(tail -n 1 /etc/lxc/hosts.conf | cut -d',' -f2)
- if [ -z "$last" ]; then
- echo "192.168.2.2"
- return 0
- fi
- fourth=$(echo $last | cut -d'.' -f4-)
- ((fourth++))
- added="$(echo $last | cut -d'.' -f-3).$fourth"
- echo $added
- }
- # defaults
- username="cnuser"
- password="cnuser"
- key=""
- ip=$(get_new_ip)
- # getopt for cli options
- options=$(getopt -o h,u:p:k:i -l help,username:,password:,key:,ip:, -- "$@")
- while true
- do
- case "$1" in
- -h|--help) usage $0 && exit 0;;
- -u|--username) username=$2; shift 2;;
- -p|--password) password=$2; shift 2;;
- -k|--key) key=$2; shift 2;;
- -i|--ip) ip=$2; shift 2;;
- --) shift 1; break;;
- *) break;;
- esac
- done
- # check for hostname pos arg
- if [ -z "$1" ]; then
- usage $0 && exit 1
- fi
- # create new container with values
- lxc-create -t ubuntu --username $username --password $password --auth-key $key -B zfs --zfsroot zfs/root/thing
- # add new IP for container, restart lxc-net
- echo "$hostname,$ip" >> /etc/lxc/hosts.conf
- systemctl restart lxc-net
- # ask to start container
- read -p "Start container and connect? [y/n]: " -n 1 -r
- if [[ $REPLY =~ ^[Yy]$ ]]; then
- lxc-start -n $hostname
- ssh -i $key $username@$ip
- exit 1
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement