Advertisement
Guest User

Untitled

a guest
May 14th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage() {
  4. cat <<EOF
  5. $1 [-h|--help] [-u|--username user] [-p|--password pass] [-k|--key key] [-i|--ip ip] hostname
  6. EOF
  7. return 0
  8. }
  9.  
  10. # get 'new' ip from the last entry in /etc/lxc/hosts.conf
  11. get_new_ip() {
  12. last=$(tail -n 1 /etc/lxc/hosts.conf | cut -d',' -f2)
  13. if [ -z "$last" ]; then
  14. echo "192.168.2.2"
  15. return 0
  16. fi
  17. fourth=$(echo $last | cut -d'.' -f4-)
  18. ((fourth++))
  19. added="$(echo $last | cut -d'.' -f-3).$fourth"
  20. echo $added
  21. }
  22.  
  23. # defaults
  24. username="cnuser"
  25. password="cnuser"
  26. key=""
  27. ip=$(get_new_ip)
  28.  
  29. # getopt for cli options
  30. options=$(getopt -o h,u:p:k:i -l help,username:,password:,key:,ip:, -- "$@")
  31. while true
  32. do
  33. case "$1" in
  34. -h|--help) usage $0 && exit 0;;
  35. -u|--username) username=$2; shift 2;;
  36. -p|--password) password=$2; shift 2;;
  37. -k|--key) key=$2; shift 2;;
  38. -i|--ip) ip=$2; shift 2;;
  39. --) shift 1; break;;
  40. *) break;;
  41. esac
  42. done
  43.  
  44. # check for hostname pos arg
  45. if [ -z "$1" ]; then
  46. usage $0 && exit 1
  47. fi
  48.  
  49. # create new container with values
  50. lxc-create -t ubuntu --username $username --password $password --auth-key $key -B zfs --zfsroot zfs/root/thing
  51.  
  52. # add new IP for container, restart lxc-net
  53. echo "$hostname,$ip" >> /etc/lxc/hosts.conf
  54. systemctl restart lxc-net
  55.  
  56. # ask to start container
  57. read -p "Start container and connect? [y/n]: " -n 1 -r
  58. if [[ $REPLY =~ ^[Yy]$ ]]; then
  59. lxc-start -n $hostname
  60. ssh -i $key $username@$ip
  61. exit 1
  62. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement