Advertisement
metalx1000

Open external Ports with UPNPC

May 7th, 2023
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.93 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2023  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. which upnpc >/dev/null || sudo apt install miniupnpc
  20.  
  21. function getinput(){
  22.   read -p "Internal Port: " in_port
  23.   [[ $in_port ]] || exit
  24.  
  25.   read -p "Extertnal Port: " ext_port
  26.   [[ $ext_port ]] || exit
  27.  
  28.   ip="$(ip a|grep inet|grep "[0-9].[0-9].[0-9].[0-9]"|awk '{print $2}'|cut -d\/ -f1|fzf --prompt "Internal IP: ")"
  29.   [[ $ip ]] || exit
  30.  
  31.   openport
  32. }
  33.  
  34. function openport(){
  35.   [[ $in_port ]] || in_port=$2
  36.   [[ $ext_port ]] || ext_port=$3
  37.   [[ $ip ]] || ip=$1
  38.  
  39.   echo -n "Opening port $ext_port to port $in_port on $ip..."
  40.   router="$(ip r | grep default | cut -d " " -f 3)"
  41.   echo -n "."
  42.   gateway="$(upnpc -l|grep 'desc:'|awk '{print $2}')"
  43.   echo "."
  44.   #open port
  45.   echo ": $(date +%s):0;upnpc -u $gateway -a $ip $in_port $ext_port tcp" >> $HOME/.zsh_history
  46.   echo ": $(date +%s):0;upnpc -u $gateway -d $ext_port tcp" >> $HOME/.zsh_history
  47.   upnpc -u $gateway -a $ip $in_port $ext_port tcp
  48.  
  49.   exit
  50. }
  51.  
  52. function closeport(){
  53.   command="$(grep upnpc ~/.zsh_history|grep '\-d'|grep -v 'grep'|cut -d\; -f2|fzf)"
  54.   echo "Closing Port"
  55.   $command
  56.   exit
  57. }
  58.  
  59. [[ "$1" == "close" ]] && closeport
  60. [[ $# == 3 ]] && openport $*
  61. getinput
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement