Skip to content

Instantly share code, notes, and snippets.

@GillesC
Created September 20, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GillesC/e9e81824bf926d2a1400c0030cd57821 to your computer and use it in GitHub Desktop.
Save GillesC/e9e81824bf926d2a1400c0030cd57821 to your computer and use it in GitHub Desktop.
#!/bin/bash
# ____ ____ _ __ __ ____ ___
# | _ \| _ \ / \ | \/ |/ ___/ _ \
# | | | | |_) | / _ \ | |\/| | | | | | |
# | |_| | _ < / ___ \| | | | |__| |_| |
# |____/|_| \_\/_/ \_\_| |_|\____\___/
# research group
# dramco.be/
#
# KU Leuven - Technology Campus Gent,
# Gebroeders De Smetstraat 1,
# B-9000 Gent, Belgium
#
# File: first_config_mqtt.sh
# Author: Gilles Callebaut
#
# Description: Install MQTT and config when fresh install
# - creates a new user and deletes user pi
# - cofigures new hostname
# - adds duckdns domainname
# - installs and configures firewall
# - installs moqsuitto
#
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
echo "Running configuration for MQTT-server @ PI"
echo "Creating new user"
echo "Enter new user: "
read newuser
adduser $newuser
adduser $newuser sudo
echo "Deleting user pi and its home dir"
deluser pi
sudo deluser -remove-home pi
read -s -n 1 -p "Press any key to reboot"
reboot
echo "Updated and upgrading"
apt-get update
apt-get upgrade
echo "Installing latest ssh server"
apt install openssh-server
echo "Installing latest avahi-daemon"
apt-get install avahi-daemon
echo "Configuring hostname:"
#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)
#Display existing hostname
echo "Existing hostname is $hostn"
#Ask for new hostname $newhost
echo "Enter new hostname: "
read newhost
#change hostname in /etc/hosts & /etc/hostname
sed -i "s/$hostn/$newhost/g" /etc/hosts
sed -i "s/$hostn/$newhost/g" /etc/hostname
#display new hostname
echo "Your new hostname is $newhost"
service hostname.sh stop
service hostname.sh start
echo "Configuring duckdns"
cd /home/$newuser
mkdir duckdns
cd duckdns
echo "Enter domainname: "
read name
echo "Enter domaintoken: "
read token
touch duckdns.sh
# dramco-mqtt-server&token=d51bdc86-0b07-436d-86e3-c215d23642b2
echo -e "echo url=\"https://www.duckdns.org/update?domains=${name}&token=${token}&ip=\" | curl -k -o ~/duckdns/duck.log -K -" >> duckdns.sh
chmod 700 duckdns.sh
echo "*/5 * * * * ~/duckdns/duckdns.sh >/dev/null 2>&1" >> /etc/crontab
echo "Installing firewall"
apt-get install ufw
ufw default deny incoming
ufw default allow outgoing
echo "Allowing ssh, MQTT and MQTT over websockets"
ufw allow ssh
ufw allow 8883 # allow MQTT
ufw allow 8083 # allow over websockets
ufw enable
echo "Firewall status:"
ufw status verbose
echo "Installing Mosquitto"
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
apt-key add mosquitto-repo.gpg.key
touch /etc/apt/sources.list.d/mosquitto.list
echo "deb http://repo.mosquitto.org/debian stretch main" >> /etc/apt/sources.list.d/mosquitto.list
apt-get update
aptitude install mosquitto mosquitto-clients
read -s -n 1 -p "Press any key to reboot"
echo -e "Allright ${newuser}, see you back at ${newhost}.local or ${name}."
sleep 3s
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment