| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  | #!/bin/sh | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # sip_nat_settings: generate NAT settings for sip.conf of an Asterisk system | 
					
						
							|  |  |  | #                   that is behind a NAT router. | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2017-12-22 09:23:22 -05:00
										 |  |  | # This is a script to generate sane defaults for externip and localnet | 
					
						
							|  |  |  | # of sip.conf. The output should be included in the [general] section of | 
					
						
							| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  | # sip.conf . | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2017-12-22 09:23:22 -05:00
										 |  |  | # Multiple network interfaces: If you have multiple network interfaces, | 
					
						
							|  |  |  | # this script will generate a 'localnet' line for each of them that has a | 
					
						
							|  |  |  | # broadcast (ipv4) address, except the loopback interface (lo). You can | 
					
						
							| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  | # later rem-out all of those you don't need. | 
					
						
							| 
									
										
										
										
											2017-12-22 09:23:22 -05:00
										 |  |  | # | 
					
						
							|  |  |  | # Alternatively, provide a network interface as a parameter an a localnet | 
					
						
							| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  | # line will only be generated for its network. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2005 by Tzafrir Cohen <tzafrir.cohen@xorcom.com> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  | # it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | # the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  | # (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 17:43:13 +02:00
										 |  |  | # see http://unix.stackexchange.com/q/22615 | 
					
						
							|  |  |  | externip=`dig @resolver1.opendns.com -4 myip.opendns.com A +short` | 
					
						
							| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # optional parameter: network interface to use. By default: none. | 
					
						
							|  |  |  | IFACE="$1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | OS=`uname -s` | 
					
						
							|  |  |  | case "$OS" in | 
					
						
							|  |  |  | Linux) | 
					
						
							|  |  |  |   echo "externip = $externip" | 
					
						
							| 
									
										
										
										
											2020-08-26 17:43:13 +02:00
										 |  |  |   if [ -x "${IFACE}" ]; then | 
					
						
							|  |  |  | 	  ip --brief -family inet address show scope global up dev $IFACE | awk '{print "localnet = " $3}' | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  | 	  ip --brief -family inet address show scope global up | awk '{print "localnet = " $3}' | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2009-01-09 23:04:46 +00:00
										 |  |  |   ;; | 
					
						
							|  |  |  | OpenBSD|FreeBSD) | 
					
						
							|  |  |  |   if [ "${OS}" = "FreeBSD" ]; then | 
					
						
							|  |  |  | 	  VER=`uname -r | cut -d . -f 1` | 
					
						
							|  |  |  | 	  if [ ${VER} -lt 7 ]; then | 
					
						
							|  |  |  | 		  echo "Unsupported OS" | 
					
						
							|  |  |  | 		  exit 1 | 
					
						
							|  |  |  | 	  fi | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   echo "externip = $externip" | 
					
						
							|  |  |  |   ip=`/sbin/ifconfig $IFACE | awk '/\tinet .* broadcast/{print $6}'` | 
					
						
							|  |  |  |   x=`/sbin/ifconfig $IFACE | awk '/\tinet .* broadcast/{print $4}'` | 
					
						
							|  |  |  |   printf 'localnet = %s/%u.%u.%u.%u\n' $ip $(($x>>24&0xff)) $(($x>>16&0xff)) $(($x>>8&0xff)) $(($x&0xff)) | 
					
						
							|  |  |  |   ;; | 
					
						
							|  |  |  | *) | 
					
						
							|  |  |  |   echo >&2 "$0: Unsupported OS $OS" | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  |   ;; | 
					
						
							|  |  |  | esac |