network.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/sh
  2. ETHPATH=/etc/network
  3. ETH0=$ETHPATH/interfaces
  4. NETCONF=/etc/asterisk/network.conf
  5. case $1 in
  6. getconfig)
  7. cat /etc/rc.conf | sed 's/\[network\]/\;\[network\]/g' | tr \; \# | sed '/#!/d' | tr -d ' ' > /tmp/network.conf
  8. . /tmp/network.conf
  9. echo [network] > $NETCONF
  10. echo HOSTNAME="`hostname`">> $NETCONF
  11. if [ "$IPASSIGN" = "dhcp" ]
  12. then
  13. echo IPASSIGN=dhcp >> $NETCONF
  14. ADDR="`/sbin/ifconfig eth0|grep 'inet addr:'|cut -d: -f2|awk '{print $1}'`"
  15. echo IPADDRESS=$ADDR >> $NETCONF
  16. MASK="`/sbin/ifconfig eth0|grep 'Mask:'|cut -d: -f4|awk '{print $1}'`"
  17. echo NETMASK="$MASK" >> $NETCONF
  18. GATEWAY="`route -n|grep '^0.0.0.0'|grep 'eth0'|awk '{print $2}'`"
  19. echo GATEWAY=$GATEWAY >> $NETCONF
  20. DNS1="`cat /etc/resolv.conf|grep '^nameserver'|sed '1!d;s/.* //'`"
  21. echo PrimaryDNS="$DNS1" >> $NETCONF
  22. DNS2="`cat /etc/resolv.conf|grep '^nameserver'|sed '2!d;s/.* //'`"
  23. echo AlternateDNS="$DNS2" >> $NETCONF
  24. else
  25. echo IPASSIGN=static >> $NETCONF
  26. echo IPADDRESS="$IPADDRESS" >> $NETCONF
  27. echo NETMASK="$NETMASK" >> $NETCONF
  28. echo GATEWAY="$GATEWAY" >> $NETCONF
  29. DNS1="`cat /etc/resolv.conf|grep '^nameserver'|sed '1!d;s/.* //'`"
  30. echo PrimaryDNS="$DNS1" >> $NETCONF
  31. DNS2="`cat /etc/resolv.conf|grep '^nameserver'|sed '2!d;s/.* //'`"
  32. echo AlternateDNS="$DNS2" >> $NETCONF
  33. fi
  34. echo IPADDRESSLAN="$IPADDRESSLAN" >> $NETCONF
  35. echo NETMASKLAN="$NETMASKLAN" >> $NETCONF
  36. echo LANV1ENABLE="$LANV1ENABLE" >> $NETCONF
  37. echo IPADDRESSLV1="$IPADDRESSLV1" >> $NETCONF
  38. echo NETMASKLV1="$NETMASKLV1" >> $NETCONF
  39. echo LANV2ENABLE="$LANV2ENABLE" >> $NETCONF
  40. echo IPADDRESSLV2="$IPADDRESSLV2" >> $NETCONF
  41. echo NETMASKLV2="$NETMASKLV2" >> $NETCONF
  42. ;;
  43. setconfig)
  44. cat /etc/rc.conf | sed 's/\[network\]/\;\[network\]/g' | tr \; \# | sed '/#!/d' | tr -d ' ' > /tmp/network.conf
  45. . /tmp/network.conf
  46. #Set WAN Interface
  47. touch /tmp/interfaces.tmp
  48. echo "auto lo" > /tmp/interfaces.tmp
  49. echo "iface lo inet loopback" >> /tmp/interfaces.tmp
  50. if [ "$IPASSIGN" = "dhcp" ]
  51. then
  52. echo "auto eth0" >> /tmp/interfaces.tmp
  53. echo "iface eth0 inet dhcp" >> /tmp/interfaces.tmp
  54. else
  55. echo "auto eth0" >> /tmp/interfaces.tmp
  56. echo "iface eth0 inet static" >> /tmp/interfaces.tmp
  57. echo " address $IPADDRESS" >> /tmp/interfaces.tmp
  58. echo " netmask $NETMASK" >> /tmp/interfaces.tmp
  59. echo " gateway $GATEWAY" >> /tmp/interfaces.tmp
  60. [ "$PrimaryDNS" ] && echo "nameserver $PrimaryDNS" > /etc/resolv.conf
  61. [ "$AlternateDNS" ] && echo "nameserver $AlternateDNS" >> /etc/resolv.conf
  62. fi
  63. #Set LAN Interface
  64. echo "auto eth1" >> /tmp/interfaces.tmp
  65. echo "iface eth1 inet static" >> /tmp/interfaces.tmp
  66. echo " address $IPADDRESSLAN" >> /tmp/interfaces.tmp
  67. echo " netmask $NETMASKLAN" >> /tmp/interfaces.tmp
  68. #Set LANv1 Interface
  69. if [ "$LANV1ENABLE" = "yes" ]
  70. then
  71. ifconfig eth1:0 down
  72. echo "auto eth1:0" >> /tmp/interfaces.tmp
  73. echo "iface eth1:0 inet static" >> /tmp/interfaces.tmp
  74. echo " address $IPADDRESSLV1" >> /tmp/interfaces.tmp
  75. echo " netmask $NETMASKLV1" >> /tmp/interfaces.tmp
  76. else
  77. ifconfig eth1:0 down
  78. fi
  79. #Set LANv2 Interface
  80. if [ "$LANV2ENABLE" = "yes" ]
  81. then
  82. ifconfig eth1:1 down
  83. echo "auto eth1:1" >> /tmp/interfaces.tmp
  84. echo "iface eth1:1 inet static" >> /tmp/interfaces.tmp
  85. echo " address $IPADDRESSLV2" >> /tmp/interfaces.tmp
  86. echo " netmask $NETMASKLV2" >> /tmp/interfaces.tmp
  87. else
  88. ifconfig eth1:1 down
  89. fi
  90. mv -f /tmp/interfaces.tmp $ETH0
  91. ;;
  92. setvlan)
  93. cat /etc/rc.conf | sed 's/\[network\]/\;\[network\]/g' | tr \; \# | sed '/#!/d' | tr -d ' ' > /tmp/network.conf
  94. . /tmp/network.conf
  95. #RM ALL VLAN
  96. cat /etc/network/interfaces > /tmp/interfaces.tmp
  97. #sed -i 12',$d' /etc/network/interfaces
  98. for i in `grep "eth" /proc/net/vlan/config|cut -d ' ' -f1`
  99. do
  100. vconfig rem $i
  101. done
  102. #Set WAN VLAN 1
  103. if [ "$VLANENABLE" = "yes" ]
  104. then
  105. vconfig add eth0 $VLANID
  106. echo "auto eth0.$VLANID" >> /tmp/interfaces.tmp
  107. echo "iface eth0.$VLANID inet static" >> /tmp/interfaces.tmp
  108. echo " address $VLANIPADDRESS" >> /tmp/interfaces.tmp
  109. echo " netmask $VLANNNETMASK" >> /tmp/interfaces.tmp
  110. fi
  111. #Set WAN VLAN 2
  112. if [ "$VLANENABLE2" = "yes" ]
  113. then
  114. vconfig add eth0 $VLANID2
  115. echo "auto eth0.$VLANID2" >> /tmp/interfaces.tmp
  116. echo "iface eth0.$VLANID2 inet static" >> /tmp/interfaces.tmp
  117. echo " address $VLANIPADDRESS2" >> /tmp/interfaces.tmp
  118. echo " netmask $VLANNNETMASK2" >> /tmp/interfaces.tmp
  119. fi
  120. #Set LAN VLAN 1
  121. if [ "$VLANENABLEL" = "yes" ]
  122. then
  123. vconfig add eth1 $VLANIDL
  124. echo "auto eth1.$VLANIDL" >> /tmp/interfaces.tmp
  125. echo "iface eth1.$VLANIDL inet static" >> /tmp/interfaces.tmp
  126. echo " address $VLANIPADDRESSL" >> /tmp/interfaces.tmp
  127. echo " netmask $VLANNNETMASKL" >> /tmp/interfaces.tmp
  128. fi
  129. #Set LAN VLAN 2
  130. if [ "$VLANENABLEL2" = "yes" ]
  131. then
  132. vconfig add eth1 $VLANIDL2
  133. echo "auto eth1.$VLANIDL2" >> /tmp/interfaces.tmp
  134. echo "iface eth1.$VLANIDL2 inet static" >> /tmp/interfaces.tmp
  135. echo " address $VLANIPADDRESSL2" >> /tmp/interfaces.tmp
  136. echo " netmask $VLANNNETMASKL2" >> /tmp/interfaces.tmp
  137. fi
  138. mv -f /tmp/interfaces.tmp $ETH0
  139. rm -rf /tmp/interfaces.tmp
  140. ;;
  141. restart)
  142. ifconfig eth0 down
  143. ifconfig eth1 down
  144. ifconfig lo down
  145. $0 setconfig
  146. $0 setvlan
  147. /etc/init.d/networking restart
  148. ifconfig eth0 up
  149. ifconfig eth1 up
  150. ifconfig lo up
  151. /etc/scripts/phonesinit.sh
  152. /etc/scripts/pnp.sh
  153. ;;
  154. setstaticroute)
  155. #set WAN static routes
  156. if [ -f /etc/asterisk/staticroutes.conf ];then
  157. i=`cat /etc/asterisk/staticroutes.conf |grep "^[\[A-Za-z]"|tr -d " "|wc -l`
  158. if [ "$i" -ge 4 ];then
  159. i=`expr $i / 4`
  160. j=0
  161. while [ "$i" -gt 0 ]
  162. do
  163. line_Start=`expr $j \* 4 + 2`
  164. line_End=`expr $line_Start + 2`
  165. cat /etc/asterisk/staticroutes.conf |grep "^[\[A-Za-z]"|tr -d " "|sed -n "${line_Start},${line_End}p" >/tmp/staticroutes.conf.tmp
  166. . /tmp/staticroutes.conf.tmp
  167. if [ -z "$Destination" -o -z "$Netmask" -o -z "$Gateway" ];then
  168. echo "error"
  169. else
  170. /sbin/route add -net ${Destination} netmask ${Netmask} gw ${Gateway} 2>/dev/null
  171. fi
  172. i=`expr $i - 1`
  173. j=`expr $j + 1`
  174. done
  175. fi
  176. fi
  177. ;;
  178. esac