code.fastix.org

Dateiansicht:

Datei:Projekte -> Linux:newMachine -> NewMachine.sh
md5:46c033a3f1af494ed2acbe11fa1e1d71
sha1:a276115454a0b1de0a7a565dd512526cea3e6835
Download-Link:Download
  1. #!/usr/bin/bash
  2.  
  3. if [ ! 0 -eq $(id -u) ]; then
  4.         echo "Error: Sorry. You have to be root to run this script! Exit!" 1>&2;
  5.         exit 1;
  6. fi
  7.  
  8. echo "This Script generate
  9. - a new hostname
  10. - a new mashine-id.
  11. - a set of new ssh-hostkeys
  12.  
  13. Press [STRG] + [c] or let the hostname clear to break!
  14. ";
  15.  
  16. read -p "New hostname: " hostname;
  17.  
  18. if [ "" = "$hostname" ]; then
  19.         echo "O.K. Exit!" 1>&2;
  20.         exit 255;
  21. fi
  22.  
  23. oldHostname=$(hostname);
  24.  
  25. hostname "$hostname";
  26. if [ 0 -ne $? ]; then
  27.         exit 2;
  28. fi
  29.  
  30. echo -n $hostname > /etc/hostname
  31. sed -i".backup" -e "s/$oldHostname/$hostname/g" /etc/hosts
  32. echo "New file /etc/hosts:";
  33. cat /etc/hosts;
  34.  
  35. rm -f /etc/machine-id
  36. if [ -x /usr/bin/systemd-machine-id-setup ]; then
  37.         /usr/bin/systemd-machine-id-setup;
  38. elif  [ -x /usr/bin/dbus-uuidgen ]; then
  39.         /usr/bin/dbus-uuidgen --ensure=/etc/machine-id;
  40. else
  41.         dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump |  head -n1 | cut -d " " -f 2,3,4,5,6,7,8,9 --output-delimiter="" > /etc/machine-id;
  42.         echo "" >> /etc/machine-id;
  43. fi
  44. echo -n "New machine-id: "; cat /etc/machine-id;
  45.  
  46. if [ -w "/var/lib/dbus/machine-id" ]; then
  47.         cat "/etc/machine-id" > "/var/lib/dbus/machine-id"
  48.         echo "... copied to /var/lib/dbus/machine-id":
  49. fi
  50.  
  51. cd /etc/ssh;
  52. keyFiles=$(ls ssh_host_*_key);
  53. for keyFile in $keyFiles; do
  54.         method=$(echo -n $keyFile | cut -d "_" -f3);
  55.         rm $keyFile;
  56.         ssh-keygen -q -N "" -t $method -f $keyFile;
  57. done
  58. cd $OLDPWD;
  59.        
  60. echo "You shold control the output and reboot the machine now."
  61. read -p "reboot now? y/[n]" a;
  62. a=$(echo -n "$a" | tr "YJj" "y");
  63. if [ "y" = $a ]; then
  64.         reboot;
  65. fi
  66.  
  67.