Script para adicionar ip estático a interfaces de rede

5

Eu tenho um pequeno roteiro aqui que deve ser bastante autoexplicativo, mas não consigo entender porque o loop while não lida corretamente com as funções. Ele deve mostrar a função getinfo novamente se o usuário pressionar N por não e, se o usuário pressionar sim, deve executar a função writeinterfacefile .

O que está errado com o meu loop?

#!/bin/bash
echo "Let's set up a static ip address for your site"
echo""

getinfo()
{
read -p "Enter the IP of your router:           (looks like 192.168.1.1) " routerip
read -p "Enter the netmask for your network:    (looks like 255.255.255  " netmask
read -p "Enter the ip address for your server:  (looks like 192.168.1.22 " staticip
}

writeinterfacefile()
{ 
cat << EOF >> /home/ubuntu/test 
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp

#Your static network configuration  
iface eth0 inet static
address echo $staticip
netmask echo $netmask
gateway echo $routerip 
EOF 

}

clear
getinfo
echo""
echo"So your settings are"
echo "Address of your Router is    " echo -n $routerip
echo "The Mask fpr the Network is  " echo -n $netmask
echo "Your decided Server IP is    " echo -n $istaticp
echo""
echo "Are these informations correct? (Y/n)"

while true; do
 read -p "Are these informations correct? [y/N]" yn? 
 case $yn in
   [Yy]* ) writeinterfacefile ;;
   [Nn]* ) getinfo ;;
       * ) echo "Pleas enter Y or n";;
 esac
done
    
por Josh 10.08.2013 / 20:28

1 resposta

1

Encontrei alguns erros no seu script. Aqui está como eu acho que deveria ser (supondo que estas são as coisas que você quer fazer este script):

#!/bin/bash

getinfo()
{
  read -p "Enter the IP of your router:          (looks like 192.168.1.1)   " routerip
  read -p "Enter the netmask for your network:   (looks like 255.255.255.0) " netmask
  read -p "Enter the ip address for your server: (looks like 192.168.1.22)  " staticip
}

writeinterfacefile()
{ 
cat << EOF >  
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet dhcp

#Your static network configuration  
iface eth0 inet static
address $staticip
netmask $netmask
gateway $routerip 
EOF
#don't use any space before of after 'EOF' in the previous line

  echo ""
  echo "Your informatons was saved in '' file."
  echo ""
  exit 0
}

file="/home/radu/test"
if [ ! -f $file ]; then
  echo ""
  echo "The file '$file' doesn't exist!"
  echo ""
  exit 1
fi

clear
echo "Let's set up a static ip address for your site"
echo ""

getinfo
echo ""
echo "So your settings are:"
echo "Address of your Router is:   $routerip"
echo "The Mask for the Network is: $netmask"
echo "Your decided Server IP is:   $staticip"
echo ""

while true; do
  read -p "Are these informations correct? [y/n]: " yn 
  case $yn in
    [Yy]* ) writeinterfacefile $file;;
    [Nn]* ) getinfo;;
        * ) echo "Pleas enter y or n!";;
  esac
done
    
por Radu Rădeanu 20.08.2013 / 17:32