Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.14 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. F=/tmp/out-$RANDOM
  4. C=/etc/network/interfaces
  5. H=/etc/hostname
  6. R=/etc/resolv.conf
  7. HN=$(hostname)
  8. IP=$(ifconfig eth0 | grep inet\  | awk '{print $2}' | cut -f 2 -d :)
  9. MSK=$(ifconfig eth0 | grep inet\  | awk '{print $4}' | cut -f 2 -d :)
  10. GW=$(route -n|tail -1| awk '{print $2}')
  11. NS=$(cat $R|grep nameserver|awk '{print $2}')
  12. function c() { echo $1 | tr '.' '\n' | wc -l; }
  13.  
  14. function inp() {
  15.   OK=1
  16.   while [ $OK -eq 1 ] ; do
  17.     dialog --no-collapse --nocancel --title "IP configuration" \
  18.       --form "Please enter network settings"\
  19.       0 0 0\
  20.       "Hostname (FQDN)" 1 1 "$HN" 1 20 24 0 \
  21.       "IP address" 2 1 "$IP" 2 20 20 0 \
  22.       "Netmask" 3 1 "$MSK" 3 20 20 0 \
  23.       "Gateway" 4 1 "$GW" 4 20 20 0 \
  24.       "Name server" 5 1 "$NS" 5 20 20 0 \
  25.       2> $F && {
  26.     HN=$(cat $F | head -1)
  27.     IP=$(cat $F | head -2 | tail -1)
  28.     MSK=$(cat $F | head -3 | tail -1)
  29.     GW=$(cat $F | head -4 | tail -1)
  30.     NS=$(cat $F | head -5 | tail -1)
  31.     OK=0
  32.     [ $(c $HN) -lt 2 ] && { dialog --msgbox "Hostname must be FQDN" 6 32 ; OK=1; }
  33.     [ $OK=0 -a $(c $IP) -lt 4 ] && { dialog --msgbox "Invalid IP address" 6 32 ; OK=1; }
  34.     [ $OK=0 -a $(c $MSK) -lt 4 ] && { dialog --msgbox "Invalid network mask" 6 32  ; OK=1; }
  35.     [ $OK=0 -a $(c $GW) -lt 4 ] && { dialog --msgbox "Invalid gateway address" 6 32  ; OK=1; }
  36.     [ $OK=0 -a $(c $NS) -lt 4 ] && { dialog --msgbox "Invalid name server address" 6 32 ; OK=1; }
  37.    }
  38.    if [ $? -eq 255 ] ; then { rm $F ; return; } fi
  39.   done
  40.  
  41.   echo -e "auto lo\niface lo inet loopback\n\nauto eth0\niface eth0 inet static" > $C
  42.   echo "        address $IP">>$C
  43.   echo "        netmask $MSK">>$C
  44.   echo "        gateway $GW">>$C
  45.   chgrp www-data $C
  46.   chmod 664 $C
  47.  
  48.   echo $HN > $H
  49.   hostname -F $H
  50.   perl -p -i -e "s/^$IP\s.*/$IP $HN $(echo $HN|cut -f 1 -d .)/" /etc/hosts
  51.   grep -q "$IP[         ]" /etc/hosts || echo "$IP $HN $(echo $HN|cut -f 1 -d .)" >>/etc/hosts
  52.   perl -p -i -e "s/option domain-name \"[^\"]*\";/option domain-name \"`dnsdomainname`\";/" /etc/dhcpd.conf
  53.   perl -p -i -e "s/option domain-name-servers .*/option domain-name-servers $NS;/" /etc/dhcpd.conf
  54.   perl -p -i -e "s/option subnet-mask .*/option subnet-mask $MSK;/" /etc/dhcpd.conf
  55.   perl -p -i -e "s/next-server .*/next-server $IP;/" /etc/dhcpd.conf
  56.   perl -p -i -e "s/option routers .*/option routers $GW;/" /etc/dhcpd.conf
  57.   echo search $(dnsdomainname) > $R
  58.   echo nameserver $NS >> $R
  59.   /etc/init.d/networking restart
  60.   rm $F
  61. }
  62.  
  63. [ -s /etc/network/interfaces ] || {
  64.   inp
  65. }
  66.  
  67. while [ true ]; do
  68.   dialog --colors --no-collapse --nocancel --title Configuration --menu \
  69.     "\nhostname: $(hostname)\n\nPlease connect to \n   http://$IP" 17 0 6 \
  70.     "Access log" "" "Error log" "" Reconfigure "" Reboot "" Shutdown "" 2> $F
  71.   O=$(head -1 $F)
  72.   case $O in
  73.   "Access log") dialog --title "scroll left/right with 'h' and 'l' or arrows" --tailbox /var/log/lighttpd/access.log 0 0;;
  74.   "Error log") dialog --title "scroll left/right with 'h' and 'l' or arrows" --tailbox /var/log/lighttpd/error.log 0 0;;
  75.   "Reconfigure") inp;;
  76.   "Reboot") shutdown -r now;;
  77.   "Shutdown") shutdown -h now;;
  78.   *) sleep 1;;
  79.   esac
  80. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement