hostname-set 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/bin/sh
  2. #Barry Kauler Oct. 2011
  3. #this is called from Internet Connection Wizard, script ''.
  4. #111028 have copied dlg code inside 'connectwizard' script, then call here with hostname passed param.
  5. #111028 fully internationalised.
  6. #111103 gcmartin: accept '-' char in hostname.
  7. #111103 gcmartin: current hostname is $HOSTNAME, but if rerun this script before restarting X, need to get from /etc/hostname.
  8. #111106 fix passed hostname from connectwizard.
  9. #111117 shinobar: fix.
  10. #120227 can also be called from quicksetup. now have exit #
  11. #120505 when hostname changed, also need to restart network connection. ref: http://www.murga-linux.com/puppy/viewtopic.php?t=77743
  12. #130511 shinobar: reconnect only if local dynamic DNS is available. See my modification near bottom of script.
  13. export TEXTDOMAIN=hostname-set
  14. export OUTPUT_CHARSET=UTF-8
  15. #111103 current hostname is $HOSTNAME, but if rerun this script before restarting X, need to get from here...
  16. [ -f /etc/hostname ] && HOSTNAME="`cat /etc/hostname`"
  17. NEW_HOSTNAME="$HOSTNAME" #111106
  18. [ $1 ] && NEW_HOSTNAME="$1"
  19. GTKDIALOGEXE="gtkdialog"
  20. [ "`which gtkdialog4`" ] && GTKDIALOGEXE="gtkdialog4"
  21. WINTITLE=$(gettext 'Set Hostname')
  22. M_1=$(gettext 'Type your computer name to identify in the network. Alpha-numeric without spaces.')
  23. M_2=$(gettext "Your computer has been assigned a unique name, known as the 'hostname', by which it identifies itself on a network. However, you might wish to change that to a name that is more meaningful to yourself, such as 'johnsmithpc'. The hostname can have letters and numbers, no spaces.")
  24. if [ "$1" = "" ];then #111028 111117
  25. export HOSTNAME_DIALOG="<window title=\"${WINTITLE}\" window_position=\"1\" icon-name=\"mini-hostname\">
  26. <vbox>
  27. <text><label>${M_2}</label></text>
  28. <hbox>
  29. <text><label>$(gettext 'Hostname'):</label></text>
  30. <entry tooltip-text=\"${M_1}\">
  31. <input>echo -n \"$NEW_HOSTNAME\"</input><variable>ENTRY_HOSTNAME</variable></entry>
  32. </hbox>
  33. <hbox>
  34. <button ok></button>
  35. <button cancel></button>
  36. </hbox>
  37. </vbox>
  38. </window>"
  39. RETVALS="`${GTKDIALOGEXE} --program=HOSTNAME_DIALOG`"
  40. eval "$RETVALS"
  41. [ "$EXIT" != "OK" ] && exit
  42. NEW_HOSTNAME="`echo -n "$ENTRY_HOSTNAME" | sed -e 's%[^0-9a-zA-Z-]%%g'`" #111103 gcmartin: accept '-' char.
  43. fi
  44. RECONNECT=""; M_h3="" #130511 shinobar.
  45. if [ "$NEW_HOSTNAME" != "$HOSTNAME" ];then
  46. #130511 shinobar: check if network is ready...
  47. LANG=C route | grep -q 'default[ ].*[ ]0\.0\.0\.0[ ]' && grep -wq 'nameserver' /etc/resolv.conf && NETREADY="y" || NETREADY=""
  48. if [ "$NETREADY" ]; then
  49. #check if there is local Dynamic DNS...
  50. mv -f /etc/hosts /etc/hosts.bak
  51. nslookup "$HOSTNAME" &>/dev/null && RECONNECT="y" || RECONNECT=""
  52. mv -f /etc/hosts.bak /etc/hosts
  53. fi
  54. if [ "$RECONNECT" ]; then #130511
  55. M_h3="$(gettext 'However, it will not take full effect until after X (the desktop) has been restarted -- see the \ZbShutdown\ZB entry in the menu.')"
  56. #120505 when hostname changed, also need to restart network connection...
  57. IFCONFIG="`ifconfig | grep '^[pwe]' | grep -v 'wmaster'`"
  58. if [ "$IFCONFIG" ];then
  59. networkdisconnect #see /usr/sbin
  60. touch /tmp/simple_network_setup/network_default_reconnect_required_flag #/usr/bin/xwin will read this, will call /usr/sbin/network_default_connect
  61. M_h3b="
  62. $(gettext '\ZbNOTE:\ZB There did appear to be an active network connection, that has now been disconnected. Restart of the network is required for the new hostname to take effect over the network. After restarting X, there should be an automatic reconnection -- if not, you can do it manually by clicking the \Zbconnect\ZB icon on the desktop')"
  63. else
  64. M_h3b=""
  65. fi
  66. fi
  67. hostname $NEW_HOSTNAME
  68. echo -n $NEW_HOSTNAME > /etc/hostname
  69. echo "127.0.0.1 $NEW_HOSTNAME localhost" > /tmp/hostname-set-hosts
  70. grep -vw 'localhost' /etc/hosts >> /tmp/hostname-set-hosts
  71. [ -s /tmp/hostname-set-hosts ] && mv -f /tmp/hostname-set-hosts /etc/hosts
  72. M_h1="$(gettext 'Set Hostname: done')"
  73. M_h2a="$(gettext 'New hostname')"
  74. M_h2b="$(gettext 'has been set.')"
  75. pupdialog --colors --title "${M_h1}" --msgbox "${M_h2a} \Zb${NEW_HOSTNAME}\ZB ${M_h2b}
  76. ${M_h3}${M_h3b}" 0 0
  77. [ "$M_h3" = "" ] && exit 2 #130511 BK
  78. #130511 ...i have added the above line, in addition to shinobar's changes. my memory of this stuff
  79. # is a bit vague. quicksetup calls hostname-set, if returns with 0 then will require a restart of X.
  80. # however, shinobar has modified $M_h3 in case of no dynamic DNS, no reconnection is needed
  81. # -- but I am still unclear whether that really means can avoid X restart -- will have to study that Forum link again.
  82. # This has also required a change to /usr/sbin/quicksetup, for that "2" return value.
  83. exit 0
  84. else #111028
  85. M_h4="$(gettext 'Set Hostname')"
  86. M_h5a="$(gettext 'The hostname is:')"
  87. M_h5b="$(gettext 'You have not changed it.')"
  88. pupdialog --colors --title "${M_h4}" --msgbox "${M_h5a} \Zb${NEW_HOSTNAME}\ZB
  89. ${M_h5b}" 0 0
  90. exit 1
  91. fi
  92. ###END###