sip_nat_settings 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/sh
  2. # sip_nat_settings: generate NAT settings for sip.conf of an Asterisk system
  3. # that is behind a NAT router.
  4. #
  5. # This is a script to generate sane defaults for externip and localnet
  6. # of sip.conf. The output should be included in the [general] section of
  7. # sip.conf .
  8. #
  9. # Multiple network interfaces: If you have multiple network interfaces,
  10. # this script will generate a 'localnet' line for each of them that has a
  11. # broadcast (ipv4) address, except the loopback interface (lo). You can
  12. # later rem-out all of those you don't need.
  13. #
  14. # Alternatively, provide a network interface as a parameter an a localnet
  15. # line will only be generated for its network.
  16. #
  17. # Copyright (C) 2005 by Tzafrir Cohen <tzafrir.cohen@xorcom.com>
  18. #
  19. # This program is free software; you can redistribute it and/or modify
  20. # it under the terms of the GNU General Public License as published by
  21. # the Free Software Foundation; either version 2 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program; if not, write to the Free Software
  31. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. WGET=`which wget`
  33. FETCH=`which fetch`
  34. if [ -x ${WGET} ]; then
  35. externip=`${WGET} -q -O- http://www.whatismyip.org`
  36. elif [ -x ${FETCH} ]; then
  37. externip=`${FETCH} -q -o - http://www.whatismyip.org`
  38. else
  39. echo "no binary found to contact http://www.whatismyip.org"
  40. exit 1
  41. fi
  42. # optional parameter: network interface to use. By default: none.
  43. IFACE="$1"
  44. OS=`uname -s`
  45. case "$OS" in
  46. Linux)
  47. echo "externip = $externip"
  48. /sbin/ifconfig $IFACE | grep 'inet addr:' | grep Bcast \
  49. | sed -e 's/^.*Bcast:\([0-9.]*\)\s*Mask:\([0-9.]*\)\s*$/localnet = \1\/\2/'
  50. ;;
  51. OpenBSD|FreeBSD)
  52. if [ "${OS}" = "FreeBSD" ]; then
  53. VER=`uname -r | cut -d . -f 1`
  54. if [ ${VER} -lt 7 ]; then
  55. echo "Unsupported OS"
  56. exit 1
  57. fi
  58. fi
  59. echo "externip = $externip"
  60. ip=`/sbin/ifconfig $IFACE | awk '/\tinet .* broadcast/{print $6}'`
  61. x=`/sbin/ifconfig $IFACE | awk '/\tinet .* broadcast/{print $4}'`
  62. printf 'localnet = %s/%u.%u.%u.%u\n' $ip $(($x>>24&0xff)) $(($x>>16&0xff)) $(($x>>8&0xff)) $(($x&0xff))
  63. ;;
  64. *)
  65. echo >&2 "$0: Unsupported OS $OS"
  66. exit 1
  67. ;;
  68. esac