Skip to content

Instantly share code, notes, and snippets.

@Instagraeme
Last active August 29, 2015 14:05
Show Gist options
  • Save Instagraeme/a5537d2afdadaab5a945 to your computer and use it in GitHub Desktop.
Save Instagraeme/a5537d2afdadaab5a945 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Author : Graeme Smith <[email protected]>
# Homepage : http://instagrae.me
# Version: 0.1
# License : BSD http://en.wikipedia.org/wiki/BSD_license
# Copyright (c) 2014, Graeme Smith
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Purpose : Install and configure various utilities on a brand new Raspberry Pi
# with the latest version of Raspbian installed
#
# Usage : sudo ./raspberryPiSetup.sh
#
ask() {
# http://djm.me/ask
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question
read -p "$1 [$prompt] " REPLY
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# header() - echo text in color, if color is not set use red
# 0 - Black, 1 - Red, 2 - Green, 3 - Yellow, 4 - Blue, 5 - Magenta, 6 - Cyan, 7 - White
header() {
if [ -z "$2" ]; then
echo -e "$(tput setaf 1)$1$(tput sgr 0)"
else
echo -e "$(tput setaf $2)$1$(tput sgr 0)"
fi
}
testConnectivity() {
# Test for network conection
local ONLINE=0
for interface in $(ls /sys/class/net/ | grep -v lo);
do
header "Checking $interface for connectivity..." 4
if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then
((ONLINE++))
header "$interface is Online" 2
else
header "$interface is not Online..."
fi
done
if [ "$ONLINE" -gt 0 ]; then
header "Testing we can access google.com..." 4
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
header "Success!" 2
return 0
else
header "Could not access google.com."
return 1
fi
else
header "No interfaces are Online"
return 1
fi
}
newline() {
echo
}
if [[ $EUID -ne 0 ]]; then
header "This script needs to be run as root. Restart with 'sudo $0'"
exit 1
fi
clear
header "\nRaspberry Pi - Initial Configuration Script" 4
header "Graeme Smith - instagrae.me\n" 4
header "This script should only be run on a fresh Raspberry Pi installation"
header "Running this on anything else will have unintended consequences.\n"
if ! ask "Continue?" N; then
exit 0;
fi
newline
header "\nRunning raspi-config. Please expand filesystem..."
sleep 5
/usr/bin/raspi-config
newline
header "Set Hostname" 4
if ask "Change Hostname?" Y; then
read -p "Enter new hostname (press enter to cancel): " HOSTNAME
if [ ! -z "${HOSTNAME// }" ]; then
echo "$HOSTNAME" > /etc/hostname
else
echo "Not Changed"
fi
fi
newline
if ask "Change Locales?" N; then
dpkg-reconfigure locales
fi
newline
if ask "Set Timezone?" Y; then
dpkg-reconfigure tzdata
fi
newline
header "Setup Users" 4
echo "Set Pi User Password"
until passwd pi; do
header "Error. Try again..."
done
newline
if ask "Set Root Password?" Y; then
until passwd; do
header "Error. Try again..."
done
fi
newline
if ask "Add User? " Y; then
read -p "New Username: " USER
adduser --home /home/$USER --shell /bin/bash $USER
if ask "Allow $USER to sudo?" Y; then
echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
fi
fi
newline
header "Configure Network" 4
if ask "Configure Wireless Access" Y; then
read -p "Enter SSID:" SSID
read -p "Enter Wi-Fi Password:" WIFIPASSWORD
header "Configuring Wireless Access..." 4
cat >/etc/network/interfaces <<EOL
auto lo
iface lo inet loopback
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid $SSID
wpa-group TKIP CCMP
wpa-psk $WIFIPASSWORD
wireless-power off
EOL
chmod 0600 /etc/network/interfaces
header "Trying to bring wlan0 up..."
ifup wlan0
fi
header "Package Selection and Update" 4
PKG_EXIST=$(dpkg-query -W --showformat='${Status}\n' wolfram-engine|grep "install ok installed")
if [ "" != "$PKG_EXIST" ]; then
if ask "Remove Wolfram Engine?" Y; then
echo "Removing wolfram-engine..."
apt-get -y remove wolfram-engine
fi
fi
newline
if testConnectivity ; then
header "Updating Apt... This may take some time.." 4
apt-get -y update
apt-get -y dist-upgrade
apt-get -y autoremove
apt-get -y autoclean
newline
header "Updating Raspberry Pi Firmware... This may also take some time..." 4
rpi-update
newline
header "Useful Utilities" 4
if ask "Install Vim, zsh and avahi-daemon?" Y; then
apt-get install -y zsh vim avahi-daemon
insserv avahi-daemon
fi
newline
if ask "Install RPI-Monitor?" Y; then
echo "Install RPI-Monitor"
apt-get -y install apt-transport-https ca-certificates
wget http://goo.gl/rsel0F -O /etc/apt/sources.list.d/rpimonitor.list
apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 2C0D3C0F
apt-get update
apt-get -y install rpimonitor
/usr/share/rpimonitor/scripts/updatePackagesStatus.pl
fi
newline
if ask "Configure SNMP?" Y; then
read -p "Contact Name e.g. Graeme Smith <[email protected]>: " SNMPCONTACT
read -p "Location (Lat/Long) e.g. 51.50077, -0.12465: " SNMPLOCATION
while [[ -z "$SNMPUSERNAME" ]]
do
read -p "Enter SNMP Username: " SNMPUSERNAME
done
while [[ -z "$SNMPPASSWORD" ]]
do
read -p "Enter SNMP Password: " SNMPPASSWORD
done
while [[ -z "$SNMPKEY" ]]
do
read -p "Enter SNMP Encryption Key: " SNMPKEY
done
apt-get -y install snmpd snmp
cat >/etc/snmp/snmpd.conf <<EOL
sysLocation $SNMPLOCATION
sysContact $SNMPCONTACT
# Full read-only access for SNMPv3
rouser authOnlyUser priv
#This line allows Observium to detect the host OS if the distro script is installed
extend .1.3.6.1.4.1.2021.7890.1 distro /usr/bin/distro
EOL
wget -O /usr/bin/distro http://www.observium.org/svn/observer/trunk/scripts/distro
chmod 755 /usr/bin/distro
service snmpd stop
net-snmp-config --create-snmpv3-user -ro -a $SNMPPASSWORD -A SHA -x $SNMPKEY -X AES $SNMPUSERNAME
insserv snmpd
service snmpd start
fi
newline
header "Configure I/O" 4
if ask "Enable i2c?" Y; then
sed -i -e 's/blacklist i2c-bcm2708/# blacklist i2c-bcm2708/g' /etc/modprobe.d/raspi-blacklist.conf
cat >>/etc/modules <<EOL
i2c-bcm2708
i2c-dev
EOL
apt-get -y install python-smbus i2c-tools
fi
fi
newline
if ask "Enable SPI Port?" Y; then
sed -i -e 's/blacklist spi-bcm2708/# blacklist spi-bcm2708/g' /etc/modprobe.d/raspi-blacklist.conf
fi
newline
if ask "Reboot?" Y; then
reboot
fi
# TODO: observium modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment