Advertisement
metalx1000

UPNP Client Script Example

Jun 20th, 2025
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.80 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2025 Kris Occhipinti
  4. #https://filmsbykris.com
  5. #This program is free software: you can redistribute it and/or modify
  6. #it under the terms of the GNU General Public License as published by
  7. #the Free Software Foundation version 3 of the License.
  8.  
  9. #This program is distributed in the hope that it will be useful,
  10. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. #GNU General Public License for more details.
  13.  
  14. #You should have received a copy of the GNU General Public License
  15. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ######################################################################
  17.  
  18. # fatal uses SIGUSR1 to allow clean fatal errors
  19. trap "exit 1" 10
  20. PROC=$$
  21.  
  22. function error() {
  23.   help
  24.   red=$(echo -en "\e[31m")
  25.   normal=$(echo -en "\e[0m")
  26.  
  27.   echo -e "${red}$@${normal}" >&2
  28.   #  exit 1
  29.   kill -10 $PROC
  30. }
  31.  
  32. function help() {
  33.   self="$(basename $0)"
  34.   echo "
  35.  Useage: $self <state> <external port>  <internal port>
  36.  Example: $self open 8080 80
  37.  Example: $self close 80
  38.  
  39.  "
  40.  
  41.   exit
  42. }
  43.  
  44. state="$1"
  45. [[ "$state" == "open" ]] || [[ "$state" == "close" ]] || help
  46.  
  47. external="$2"
  48. [[ $external ]] || error "No external port given."
  49.  
  50. ip="$(ip a | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | cut -d\/ -f1)"
  51. server="$(upnpc -l | grep desc: | awk '{print $2}')"
  52.  
  53. if [[ "$state" == "open" ]]; then
  54.   internal="$3"
  55.   [[ $internal ]] || error "No internal port given."
  56.   cmd="upnpc -u $server -a $ip $internal $external tcp"
  57. elif [[ "$state" == "close" ]]; then
  58.   cmd="upnpc -u $server -d $external tcp"
  59. else
  60.   error "State needs to be 'open' or 'close'"
  61. fi
  62.  
  63. echo "$cmd"
  64. $cmd
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement