Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | HOSTNAME="$1" |
| 6 | |
| 7 | if [[ ! "${HOSTNAME}" =~ ^pi-[0-9]*-[0-9]$ ]]; then |
| 8 | echo "Invalid hostname ${HOSTNAME}, needs to be pi-[team#]-[pi#]" |
| 9 | exit 1 |
| 10 | fi |
| 11 | |
| 12 | TEAM_NUMBER="$(echo ${HOSTNAME} | sed 's/pi-\(.*\)-.*/\1/')" |
| 13 | PI_NUMBER="$(echo ${HOSTNAME} | sed 's/pi-.*-\(.*\)/\1/')" |
| 14 | IP_BASE="$(echo ${TEAM_NUMBER} | sed 's/\(.*\)\(..\)/10.\1.\2/')" |
| 15 | IP="${IP_BASE}.$(( 100 + ${PI_NUMBER}))" |
| 16 | |
| 17 | echo "Changing to team number ${TEAM_NUMBER}, IP ${IP}" |
| 18 | |
| 19 | sed -i "s/^static ip_address=.*$/static ip_address=${IP}\/24/" /etc/dhcpcd.conf |
| 20 | |
| 21 | sed -i "s/\(127\.0\.1\.1\t\).*$/\1${HOSTNAME}/" /etc/hosts |
| 22 | |
| 23 | echo "${HOSTNAME}" > /etc/hostname |
| 24 | |
Austin Schuh | 97ad447 | 2020-02-26 19:54:25 -0800 | [diff] [blame] | 25 | if grep '^10\.[0-9]*\.[0-9]*\.[0-9]*\s*pi-[0-9]*-[0-9] pi[0-9]$' /etc/hosts >/dev/null ; |
Austin Schuh | ab8c0c5 | 2020-02-22 13:42:37 -0800 | [diff] [blame] | 26 | then |
| 27 | sed -i "s/^10\.[0-9]*\.[0-9]*\(\.[0-9]*\s*pi-\)[0-9]*\(-[0-9] pi[0-9]\)$/${IP_BASE}\1${TEAM_NUMBER}\2/" /etc/hosts |
| 28 | else |
| 29 | for i in {1..6}; do |
| 30 | echo -e "${IP_BASE}.$(( i + 100 ))\tpi-${TEAM_NUMBER}-${i} pi${i}" >> /etc/hosts |
| 31 | done |
| 32 | fi |
| 33 | |
| 34 | if grep '^10\.[0-9]*\.[0-9]*\.2\s*roborio$' /etc/hosts >/dev/null; |
| 35 | then |
| 36 | sed -i "s/^10\.[0-9]*\.[0-9]*\(\.2\s*roborio\)$/${IP_BASE}\1/" /etc/hosts |
| 37 | else |
| 38 | echo -e "${IP_BASE}.2\troborio" >> /etc/hosts |
| 39 | fi |
| 40 | |