[c8fd992] | 1 | #!/bin/bash |
---|
| 2 | |
---|
[df7d8f0] | 3 | #/** |
---|
| 4 | # setserveraddr {str_ipaddress | str_netiface} |
---|
| 5 | #@file setserveraddr |
---|
| 6 | #@brief Command the modifies configuration files to assign the default network interface. |
---|
| 7 | #@param str_ipaddress IP address assigned to a network interface |
---|
| 8 | #@param str_netiface network interface name defined by the operating system. |
---|
| 9 | #@version Initial version. |
---|
| 10 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
| 11 | #@date 2011-01-25 |
---|
| 12 | #@version 1.0.5 - Regenerate configuration files. |
---|
| 13 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
| 14 | #@date 2014-06-06 |
---|
| 15 | #@version 1.1.1 - Updating menu URLs, PXE files, and repository API key. |
---|
| 16 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
| 17 | #@date 2018-11-15 |
---|
| 18 | #*/ ## |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | # Variables. |
---|
| 22 | PROG="$(basename "$0")" |
---|
| 23 | OPENGNSYS=/opt/opengnsys |
---|
| 24 | PXEDIR=$OPENGNSYS/tftpboot/menu.lst |
---|
| 25 | DEFAULTFILE=/etc/default/opengnsys |
---|
| 26 | |
---|
| 27 | # Checking parameters. |
---|
[c8fd992] | 28 | if [ $# -ne 1 ]; then |
---|
[df7d8f0] | 29 | echo "$PROG: Incorrect operand. Format: $PROG ipaddress|netiface" >&2 |
---|
[c8fd992] | 30 | exit 1 |
---|
| 31 | fi |
---|
| 32 | if [ "$USER" != "root" ]; then |
---|
| 33 | echo "$PROG: Need to be root." >&2 |
---|
| 34 | exit 1 |
---|
| 35 | fi |
---|
| 36 | |
---|
[df7d8f0] | 37 | # Showing warning to inform that initiated clients may hang. |
---|
| 38 | read -rp "WARNING: initiated clients can hang. Continue? (y/n): " ANSWER |
---|
| 39 | if [ "${ANSWER,,}" != "y" ]; then |
---|
[8f70b5b] | 40 | echo "Operation canceled." |
---|
| 41 | exit 0 |
---|
| 42 | fi |
---|
| 43 | |
---|
[df7d8f0] | 44 | # Detecting network interfaces. |
---|
| 45 | DEVICES=$(ip -o link show up | awk -F: '$2!~/lo/ {print $2}') |
---|
[8f70b5b] | 46 | for DEV in $DEVICES; do |
---|
[df7d8f0] | 47 | # If the network interface is found, get its IP address. |
---|
| 48 | IP=$(ip -o addr show dev "$DEV" | awk '$3~/inet$/ {sub (/\/.*/, ""); print ($4)}') |
---|
| 49 | if [ "$DEV" == "$1" ] || [ "$IP" == "$1" ]; then |
---|
| 50 | SERVERIP="$IP" |
---|
| 51 | SERVERDEV="$DEV" |
---|
| 52 | fi |
---|
[c8fd992] | 53 | done |
---|
| 54 | |
---|
[df7d8f0] | 55 | # Checking if IP address has been detected. |
---|
[8f70b5b] | 56 | if [ -n "$SERVERIP" ]; then |
---|
[df7d8f0] | 57 | # Temporary files. |
---|
[8f70b5b] | 58 | tmpfile=$(mktemp /tmp/og.XXXXX) |
---|
| 59 | MYCNF=$(mktemp /tmp/.my.cnf.XXXXX) |
---|
| 60 | trap "rm -f $tmpfile $MYCNF" 1 2 3 6 9 15 |
---|
| 61 | |
---|
[df7d8f0] | 62 | # Checking whether the DHCP settings need to be changed. |
---|
[c8fd992] | 63 | CHANGE=0 |
---|
[8f70b5b] | 64 | for f in /etc/{dhcp,hcp3}/dhcpd.conf; do |
---|
| 65 | if [ -f $f ]; then |
---|
[df7d8f0] | 66 | # Changing DHCP "next-server" parameter. |
---|
| 67 | file="${f/./-$SERVERDEV.}" |
---|
[e0314d93] | 68 | sed -e "s/next-server.*/next-server $SERVERIP;/" \ |
---|
| 69 | -e "s/option routers ;/option routers ${SERVERIP%.*}.1;/" $file >$tmpfile |
---|
[df7d8f0] | 70 | # Copying and linking file if there are changes. |
---|
[67d0d8c] | 71 | if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then |
---|
[8f70b5b] | 72 | mv $tmpfile $file |
---|
| 73 | chmod 644 $file |
---|
| 74 | ln -f $file $f |
---|
| 75 | CHANGE=1 |
---|
| 76 | fi |
---|
[c8fd992] | 77 | fi |
---|
| 78 | done |
---|
[df7d8f0] | 79 | # Restarting DHCP service if its configuration has changed. |
---|
[c8fd992] | 80 | if [ $CHANGE == 1 ]; then |
---|
| 81 | for f in /etc/init.d/{isc-dhcp-server,dhcp3-server,dhcpd}; do |
---|
| 82 | [ -x $f ] && $f restart |
---|
| 83 | done |
---|
| 84 | else |
---|
[2cbfd3f] | 85 | echo "DHCP configuration has not changed." |
---|
[c8fd992] | 86 | fi |
---|
[8f70b5b] | 87 | |
---|
[df7d8f0] | 88 | # Saving old IP address. |
---|
[8f70b5b] | 89 | source $OPENGNSYS/etc/ogAdmRepo.cfg |
---|
| 90 | OLDSERVERIP=$IPlocal |
---|
| 91 | |
---|
[df7d8f0] | 92 | # Checking if configuration files need to be modified. |
---|
[c8fd992] | 93 | CHANGE=0 |
---|
[8f70b5b] | 94 | for f in $OPENGNSYS/{etc/{ogAdmServer,ogAdmRepo,ogAdmAgent}.cfg,www/controlacceso.php,client/etc/ogAdmClient.cfg}; do |
---|
[df7d8f0] | 95 | # Error if configuration file cannot be found. |
---|
[8f70b5b] | 96 | if [ ! -f $f ]; then |
---|
[c8fd992] | 97 | echo "$PROG: File $file does not exist." >&2 |
---|
| 98 | exit 2 |
---|
| 99 | fi |
---|
[df7d8f0] | 100 | # Updating configuration variables (if URLs does not contain "localhost"). |
---|
[8f70b5b] | 101 | sed -e "s,ServidorAdm=.*,ServidorAdm=$SERVERIP," \ |
---|
| 102 | -e "s,IPlocal=.*,IPlocal=$SERVERIP," \ |
---|
[f535be9] | 103 | -e "s,UrlMenu=https?://\([^/]*\)/\(.*\),UrlMenu=https://$SERVERIP/\2," \ |
---|
[4079733] | 104 | -e '/localhost/!s,https\?://[^/]*/\(.*\),https://'$SERVERIP'/\1,' $f >$tmpfile |
---|
[df7d8f0] | 105 | file="${f/./-$SERVERDEV.}" |
---|
| 106 | # Copying updated file, if needed. |
---|
[2cbfd3f] | 107 | if [ ! $f -ef $file ] || ! diff -q $tmpfile $file &>/dev/null; then |
---|
| 108 | cp $tmpfile $file |
---|
[8f70b5b] | 109 | ln -f $file $f |
---|
[c8fd992] | 110 | CHANGE=1 |
---|
| 111 | fi |
---|
| 112 | done |
---|
[8f70b5b] | 113 | |
---|
[df7d8f0] | 114 | # Processing when something has changed. |
---|
[c8fd992] | 115 | if [ $CHANGE == 1 ]; then |
---|
[df7d8f0] | 116 | # Restart OpenGnsys services. |
---|
[c8fd992] | 117 | /etc/init.d/opengnsys restart |
---|
[df7d8f0] | 118 | source $DEFAULTFILE |
---|
| 119 | # If OpenGnsys Server is active, updating the database. |
---|
| 120 | if [ "$RUN_OGADMSERVER" == "yes" ]; then |
---|
| 121 | source $OPENGNSYS/etc/ogAdmServer.cfg |
---|
| 122 | # Creating credentials file. |
---|
| 123 | cat << EOT > $MYCNF |
---|
[69fc529] | 124 | [client] |
---|
| 125 | user=$USUARIO |
---|
| 126 | password=$PASSWORD |
---|
| 127 | EOT |
---|
[df7d8f0] | 128 | # Updating OpenGnsys Server IP address. |
---|
| 129 | mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \ |
---|
| 130 | "UPDATE entornos |
---|
| 131 | SET ipserveradm='$SERVERIP' |
---|
| 132 | WHERE identorno=1" |
---|
| 133 | # Updating all menu URLs. |
---|
| 134 | mysql --defaults-extra-file=$MYCNF -D "$CATALOG" -e \ |
---|
| 135 | "UPDATE menus |
---|
| 136 | SET htmlmenupub = REPLACE(htmlmenupub, '$OLDSERVERIP', '$SERVERIP'), |
---|
| 137 | htmlmenupri = REPLACE(htmlmenupri, '$OLDSERVERIP', '$SERVERIP');" |
---|
| 138 | # Updating all PXE files. |
---|
| 139 | find $PXEDIR -name "01-*" -exec sed -i -e "s/$OLDSERVERIP/$SERVERIP/g" {} \; |
---|
| 140 | fi |
---|
[8f70b5b] | 141 | |
---|
[df7d8f0] | 142 | # Showing manual task to do after execution. |
---|
[8f70b5b] | 143 | cat << EOT |
---|
[df7d8f0] | 144 | Default server interface set to: $SERVERDEV ($SERVERIP) |
---|
[8f70b5b] | 145 | |
---|
| 146 | Manual tasks: |
---|
[df7d8f0] | 147 | - Check DHCP configuration file and restart service, if needed. |
---|
| 148 | - Check PXE files. |
---|
| 149 | - Log-in as Web Console user: |
---|
| 150 | - Check menu URLs. |
---|
[dde2db1] | 151 | - Note: Run "settoken" script to update authentication tokens. |
---|
[8f70b5b] | 152 | EOT |
---|
[c8fd992] | 153 | else |
---|
[df7d8f0] | 154 | # Showing message if nothing changes. |
---|
[2cbfd3f] | 155 | echo "Default interface has not changed: $1" |
---|
[c8fd992] | 156 | fi |
---|
| 157 | else |
---|
[df7d8f0] | 158 | # Error if network interface is not found. |
---|
| 159 | echo "$PROG: Network device not found. Format: $PROG ipaddress|netiface" >&2 |
---|
[c8fd992] | 160 | exit 1 |
---|
| 161 | fi |
---|
| 162 | |
---|
[df7d8f0] | 163 | # Removing temporary files. |
---|
[2cbfd3f] | 164 | rm -f $tmpfile $MYCNF |
---|
| 165 | |
---|