BashScriptToFindPortsUsed

From MLDonkey
Jump to: navigation, search

Which TCP or UDP ports are opened by MLDonkey ?

Maybe you're looking for which ports are opened by MLDonkey?

This small bash script will try to find and display TCP and UDP ports by parsing MLDonkey's configuration files.

How to use this script ?

#!/bin/bash
#######################################################################
# 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., 675 Mass Ave, Cambridge, MA 02139, USA
#######################################################################
# This script check what networks are enabled/disabled and display port
# numbers binded by the clients
#######################################################################
# Changelog:
# * Sun Nov 29 2009 - v0.4
# - Attempt to find the MLdonkey installation, or let them specify it
#   in the BASEDIR variable on the command line.
# - Rewrite checkNetwork() and findPort() for efficiency and handling
#   of newer version of MLDonkey.
#
# * Sun Aug 08 2004 - v0.3
# - Added an error message when the network configuration file exist
#   but no port number was found.
#
# * Sat Aug 07 2004 - v0.2
# - Bugfix, ${BASEDIR} was missing when calling the findPort() function
# - Code cleanup
#
# * Sat Aug 07 2004 - v0.1
# - Initial Release
#######################################################################
# (C)2004/08 - GCN (http://gcnweb.free.fr/) - gcnweb(at)free(dot)fr
#######################################################################

## Some vars
# MLDonkey config dir
# See if it's already set
[ -n "${BASEDIR}" ] && [ "${BASEDIR: -1}" != "/" ] && BASEDIR="${BASEDIR}/"
# Try user's .mldonkey dir
[ -z "${BASEDIR}"  -o  ! -e "${BASEDIR}downloads.ini" ] && BASEDIR="${HOME}/.mldonkey/"
# Look in the directory containing this script
[ -e "${BASEDIR}downloads.ini" ] || BASEDIR="$(dirname $0)/"
# Try the current directory
[ -e "downloads.ini" ] && BASEDIR="$(pwd)/"
# No dice...
if [ ! -e "${BASEDIR}downloads.ini" ]; then
  echo "Can't find your MLdonkey data directory!  Re-invoke the script as:"
  echo "BASEDIR=\"path_to_mldonkey_inis\" $0"
  exit 255
fi

########### YOU SHOULD NOT MODIFY SOMETHING BELOW THIS LINE ###########

# Colors and positionning
MOVE_TO_COL="\\033[22G"
COLOR_ENABLED="\\033[1;32m"
COLOR_DISABLED="\\033[1;31m"
COLOR_WARNING="\\033[1;33m"
COLOR_NORMAL="\\033[0;39m"

# MLDonkey main configuration file
MAINCFGFILE="${BASEDIR}downloads.ini"

## Network section
cnt=0
# NAME     == Network name
# GSECTION == Section name in the main configuration file
# FILE     == Network's configuration file
# FESCTION == Section name in the network configuration file
# TCP      == Unused
# UDP      == Tell if the network also needs UDP
#             value: 0      => UDP not used
#             value: 1      => UDP used. Port number is equal to TCP Port
#             value: +/-xxx => UDP port value is +/- relative to TCP port

# BitTorrent - Client
let cnt++
NAME[$cnt]="BitTorrent [Client]"
GSECTION[$cnt]="enable_bittorrent"
FILE[$cnt]="${BASEDIR}bittorrent.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=0

# BitTorrent - Tracker
let cnt++
NAME[$cnt]="BitTorrent [Tracker]"
GSECTION[$cnt]="enable_bittorrent"
FILE[$cnt]="${BASEDIR}bittorrent.ini"
FSECTION[$cnt]="tracker_port"
TCP[$cnt]=1
UDP[$cnt]=0

# Direct Connect
let cnt++
NAME[$cnt]="Direct Connect"
GSECTION[$cnt]="enable_directconnect"
FILE[$cnt]="${BASEDIR}directconnect.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=1

# eDonkey 2000
let cnt++
NAME[$cnt]="eDonkey 2000"
GSECTION[$cnt]="enable_donkey"
FILE[$cnt]="${BASEDIR}donkey.ini"
FSECTION[$cnt]="port"
TCP[$cnt]=1
UDP[$cnt]=+4

# FastTrack
let cnt++
NAME[$cnt]="FastTrack"
GSECTION[$cnt]="enable_fasttrack"
FILE[$cnt]="${BASEDIR}fasttrack.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=0

# Gnutella
let cnt++
NAME[$cnt]="Gnutella"
GSECTION[$cnt]="enable_gnutella"
FILE[$cnt]="${BASEDIR}gnutella.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=1

# Gnutella2
let cnt++
NAME[$cnt]="Gnutella2"
GSECTION[$cnt]="enable_gnutella2"
FILE[$cnt]="${BASEDIR}gnutella2.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=1

# Open Napster
let cnt++
NAME[$cnt]="Open Napster"
GSECTION[$cnt]="enable_opennap"
FILE[$cnt]="${BASEDIR}opennap.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=0

# Overnet
let cnt++
NAME[$cnt]="Overnet"
GSECTION[$cnt]="enable_overnet"
FILE[$cnt]="${BASEDIR}donkey.ini"
FSECTION[$cnt]="overnet_port"
TCP[$cnt]=1
UDP[$cnt]=1

# SoulSeek
let cnt++
NAME[$cnt]="SoulSeek"
GSECTION[$cnt]="enable_soulseek"
FILE[$cnt]="${BASEDIR}soulseek.ini"
FSECTION[$cnt]="client_port"
TCP[$cnt]=1
UDP[$cnt]=0


## Functions
# Test if a network is Enabled / Disabled
# $1 == network number
function checkNetwork() {
  sed -n -r -e 's@^[[:space:]]*\<'"${GSECTION[$1]}"'\>[[:space:]]*=[[:space:]]*(true|false)@\1@Ip' "$MAINCFGFILE"
}

# Find the reserved port number
# $1 == network number
function findPort() {
  # Look first in a section matching the name (with spaces removed)
  ret="$(sed -n -r -e '
    /[[:space:]]*\<'"${NAME[$1]// /}"'\>[[:space:]]*=[[:space:]]*\{/I {
      :find_section
      /^[[:space:]]*\<'"${FSECTION[$1]}"'\>[[:space:]]*=[[:space:]]*([0-9]+)$/I {
        s@^[[:space:]]*\<'"${FSECTION[$1]}"'\>[[:space:]]*=[[:space:]]*([0-9]+)$@\1@Ip
        q
      }
      /}/ q
      n; b find_section
    }' "${FILE[$1]}")"

  # If we didn't find it, try looking for just "port"
  if [ -z "$ret" ]; then
    ret="$(sed -n -r -e '
      /[[:space:]]*\<'"${NAME[$1]// /}"'\>[[:space:]]*=[[:space:]]*\{/I {
        :find_section
        /^[[:space:]]*\<port\>[[:space:]]*=[[:space:]]*([0-9]+)$/I {
          s@^[[:space:]]*\<port\>[[:space:]]*=[[:space:]]*([0-9]+)$@\1@Ip
          q
        }
        /}/ q
        n; b find_section
      }' "${FILE[$1]}")"
  fi

  # If we still didn't find it, look for the name not in a section
  if [ -z "$ret" ]; then
    ret="$(sed -n -r -e '
      :find_fsection
        :find_section_end
          /\{/ {
            n
            /\}/ b find_section_end
          }
        /^[[:space:]]*\<'"${FSECTION[$1]}"'\>[[:space:]]*=[[:space:]]*([0-9]+)$/I {
          s@^[[:space:]]*\<'"${FSECTION[$1]}"'\>[[:space:]]*=[[:space:]]*([0-9]+)$@\1@Ip
          q
        }
        n; b find_fsection' "${FILE[$1]}")"
  fi

  echo $ret
}

#
# Main Routine
#
echo -e "\nChecking MLDonkey configuration files:\n"

for (( i=1; $i <= $cnt; i++ )); do

  echo -en "${NAME[$i]}${MOVE_TO_COL}: "
  VALUE=`checkNetwork $i`

  # If $VALUE == True, check if the configuration file exists
  if [ "$VALUE" == "true" ]; then

    if [ ! -f ${FILE[$i]} ]; then
      echo -en ${COLOR_WARNING}
      echo -en "WARNING  Configuration file \"${FILE[$i]}\" is missing"
      echo -e  ${COLOR_NORMAL}
    else

      # Assume there is _ALWAYS_ a TCP port
      PORTNUMBER=`findPort $i`

      if [ "x${PORTNUMBER}" == "x" ]; then

        echo -en ${COLOR_WARNING}
        echo -en "WARNING  No ports found in the file \"${FILE[$i]}\""
        echo -e  ${COLOR_NORMAL}

      else

        echo -en ${COLOR_ENABLED}
        echo -en "ENABLED "
        echo -en "${COLOR_NORMAL} ["

        echo -n "TCP: ${PORTNUMBER}"

        # Check if a UDP port exists and display it
        if [ ${UDP[$i]} -eq 1 ]; then
          echo -n " / UDP: ${PORTNUMBER}"
        elif [ ${UDP[$i]} -ne 0 ]; then
          echo -n " / UDP: $(( ${PORTNUMBER} ${UDP[$i]} ))"
        fi

        echo "]"

      fi

    fi

  else
      echo -en ${COLOR_DISABLED}
      echo -en "DISABLED"
      echo -e  ${COLOR_NORMAL}
  fi

done

Interesting pages

You can check this page about WhatFirewallPortsToOpen

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox