1 | #!/bin/sh |
---|
2 | # |
---|
3 | # Change the IP/hostname on cluster machines |
---|
4 | # |
---|
5 | |
---|
6 | # Required for recovery-mode scripts |
---|
7 | if [ "$1" = "test" ]; then |
---|
8 | echo "Change IP address or hostname" |
---|
9 | exit 0 |
---|
10 | fi |
---|
11 | |
---|
12 | if [ "$(id -u)" != "0" ]; then |
---|
13 | echo "This script must be run as root." |
---|
14 | exit 0 |
---|
15 | fi |
---|
16 | |
---|
17 | |
---|
18 | # Blatantly stolen from the installer |
---|
19 | ask() { |
---|
20 | answer='' |
---|
21 | while [ y != "$answer" -a n != "$answer" ]; do |
---|
22 | echo -n "$1" |
---|
23 | read answer |
---|
24 | [ Y = answer ] && answer=y |
---|
25 | [ N = answer ] && answer=n |
---|
26 | [ -z "$answer" ] && answer=$2 |
---|
27 | done |
---|
28 | } |
---|
29 | |
---|
30 | bail() { |
---|
31 | echo |
---|
32 | echo "Cancelled. Press Enter to the menu." |
---|
33 | read dummy |
---|
34 | exit 0; |
---|
35 | } |
---|
36 | |
---|
37 | # sulogin gets upset if it gets a ^C |
---|
38 | trap bail INT |
---|
39 | |
---|
40 | PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH" |
---|
41 | |
---|
42 | if [ "$DEBATHENA_DEBUG" = "1" ]; then |
---|
43 | echo "**********\nDEBUG MODE\n**********" |
---|
44 | MASKS="$(pwd)/masks"; |
---|
45 | NETPARAMS="$(pwd)/netparams"; |
---|
46 | . "$(pwd)/require_network.sh" |
---|
47 | else |
---|
48 | MASKS="/usr/share/debathena-recovery-mode-config/masks"; |
---|
49 | NETPARAMS="/usr/share/debathena-recovery-mode-config/netparams"; |
---|
50 | . /usr/share/debathena-recovery-mode-config/require_network.sh |
---|
51 | fi |
---|
52 | |
---|
53 | if [ "$DEBATHENA_DEBUG" != "1" ] && \ |
---|
54 | [ "$(machtype -L)" != "debathena-cluster" ]; then |
---|
55 | echo "WARNING: This script is designed for debathena-cluster machines" |
---|
56 | echo "but this machine is running $(machtype -L).\n"; |
---|
57 | ask "Are you SURE you want to continue? (y/N) " n |
---|
58 | if [ "$answer" = "n" ]; then |
---|
59 | exit 0 |
---|
60 | fi |
---|
61 | fi |
---|
62 | |
---|
63 | echo "Testing networking, please wait..." |
---|
64 | if ! require_network; then |
---|
65 | echo "Can't verify that networking is available. If you continue," |
---|
66 | ask "errors may occur. Continue? (y/N) " n |
---|
67 | if [ "$answer" = "n" ]; then |
---|
68 | exit 0 |
---|
69 | fi |
---|
70 | fi |
---|
71 | |
---|
72 | while true; do |
---|
73 | while [ -z "$IPADDR" ] ; do |
---|
74 | echo -n "Enter IP address: " |
---|
75 | read IPADDR |
---|
76 | done |
---|
77 | |
---|
78 | NETMASK=`$NETPARAMS -f $MASKS $IPADDR|cut -d\ -f 1` |
---|
79 | GATEWAY=`$NETPARAMS -f $MASKS $IPADDR|cut -d\ -f 4` |
---|
80 | |
---|
81 | echo "Address: $IPADDR" |
---|
82 | echo |
---|
83 | echo "Autoconfigured settings:" |
---|
84 | echo " Netmask: $NETMASK" |
---|
85 | echo " Gateway: $GATEWAY" |
---|
86 | echo |
---|
87 | ask "Is this correct? (Y/n) " y |
---|
88 | [ "$answer" = "y" ] || continue |
---|
89 | echo "Looking up hostname..."; |
---|
90 | FQDN="$(dig +short -x $IPADDR 2>/dev/null | sed -e 's/.$//')" |
---|
91 | if [ $? != 0 ]; then |
---|
92 | echo "Unable to look up hostname. You may enter it below" |
---|
93 | echo "but beware that typos may render your machine unusable." |
---|
94 | FQDN= |
---|
95 | while [ -z "$FQDN" ] ; do |
---|
96 | echo -n "Enter hostname: " |
---|
97 | read FQDN |
---|
98 | FQDN="$(echo $FQDN | tr -d ' ')" |
---|
99 | if echo "$FQDN" | egrep -qi "[^a-z0-9_-\.]"; then |
---|
100 | echo "Invalid characters in hostname, try again." |
---|
101 | FQDN= |
---|
102 | fi |
---|
103 | done |
---|
104 | fi |
---|
105 | if ! echo "$FQDN" | grep -qi "MIT.EDU$"; then |
---|
106 | FQDN="$FQDN.MIT.EDU" |
---|
107 | fi |
---|
108 | echo "Hostname: $FQDN" |
---|
109 | ask "Is this correct? (Y/n) " y |
---|
110 | if [ "$answer" = "y" ]; then |
---|
111 | break |
---|
112 | fi |
---|
113 | done |
---|
114 | HOSTNAME="$(echo "$FQDN" | cut -d. -f 1)" |
---|
115 | DOMAIN="$(echo "$FQDN" | sed 's/[^\.]*\.//')" |
---|
116 | if [ "$DEBATHENA_DEBUG" = "1" ]; then |
---|
117 | echo "$IPADDR $NETMASK $GATEWAY $HOSTNAME $DOMAIN $FQDN" |
---|
118 | echo "Done!" |
---|
119 | exit 0 |
---|
120 | fi |
---|
121 | echo "Updating /etc/network/interfaces..." |
---|
122 | mv -f "/etc/network/interfaces" "/etc/network/interfaces.old" |
---|
123 | cat <<EOF >/etc/network/interfaces |
---|
124 | # This file was created by athena-renumber |
---|
125 | |
---|
126 | # The loopback interface |
---|
127 | auto lo |
---|
128 | iface lo inet loopback |
---|
129 | |
---|
130 | # The primary network interface |
---|
131 | auto eth0 |
---|
132 | iface eth0 inet static |
---|
133 | address $IPADDR |
---|
134 | netmask $NETMASK |
---|
135 | gateway $GATEWAY |
---|
136 | dns-nameservers 18.70.0.160 18.71.0.151 18.72.0.3 |
---|
137 | dns-search mit.edu |
---|
138 | EOF |
---|
139 | echo "Updating /etc/hosts..." |
---|
140 | mv -f "/etc/hosts" "/etc/hosts.old" |
---|
141 | cat <<EOF >/etc/hosts |
---|
142 | # This file was created by athena-renumber |
---|
143 | 127.0.0.1 localhost |
---|
144 | 127.0.1.1 $fqdn $hostname |
---|
145 | |
---|
146 | # The following lines are desirable for IPv6 capable hosts |
---|
147 | ::1 localhost ip6-localhost ip6-loopback |
---|
148 | fe00::0 ip6-localnet |
---|
149 | ff00::0 ip6-mcastprefix |
---|
150 | ff02::1 ip6-allnodes |
---|
151 | ff02::2 ip6-allrouters |
---|
152 | ff02::3 ip6-allhosts |
---|
153 | EOF |
---|
154 | echo "Updating /etc/hostname..." |
---|
155 | mv -f /etc/hostname /etc/hostname.old |
---|
156 | echo $HOSTNAME > /etc/hostname |
---|
157 | echo "Setting hostname..."; |
---|
158 | hostname $HOSTNAME |
---|
159 | echo |
---|
160 | echo "Done! You will need to reboot the workstation for the changes to take effect." |
---|
161 | ask "Would you like to reboot now? (Y/n) " y |
---|
162 | if [ "$answer" = "n" ]; then |
---|
163 | echo "Please perform a full shutdown and reboot as soon as possible." |
---|
164 | echo "(Press Enter to return to the menu)" |
---|
165 | exit 0 |
---|
166 | fi |
---|
167 | reboot |
---|