1

I'd like to Port forward a few ranges on my Windows 7 Desktop. Ideally I'd like to use a desktop utility for this purpose but I couldn't find one except this: http://www.quantumg.net/portforward.php

However, it accepts port numbers and not port ranges. So if I have to forward 10000 ports, i'll have to add 10,000 entries.

Is there a better way? Command line utilities and scripts are as good if any are available.

3
  • What is your router?
    – soandos
    Aug 1, 2011 at 9:15
  • Strange question: Port forward them from where to where: A router to your Windows 7 computer? Then how would you use software? Do you have a computer that is handling your routing that would then route to the Windows 7 box? You need to add more information to really get a good answer.
    – KCotreau
    Aug 1, 2011 at 11:48
  • My Windows 7 box has multiple NICs and some Virtual Machines running on it. I'd like to forward ports ON my Windows Desktop to certain VMs on another NIC. There is no router; except that the Windows box is a router itself.
    – sharjeel
    Aug 1, 2011 at 14:06

2 Answers 2

3

I didn't see any good alternatives, but I have an idea.

The program that you mentioned lets you load from a text file filled with port, destination, and local port information.

It would be pretty easy to write a program that outputs this information to a text file given some ranges. You can then load the text file into the port forwarding program that you mentioned.

Here is a program in Java that I just stuck together (sorry i'm a novice programmer)

package portforwardranges;
import java.io.File;
import java.io.PrintWriter;

public class PortForwardRanges {

    PortForwardRanges(){
        try {
            PrintWriter pw = new PrintWriter(new File("output.txt"));
            for (int i = 50; i <= 100; i++){
            pw.print(i);
            pw.print(" 192.168.1.2 ");
            pw.print(i);
            pw.println();
            }

            pw.close();

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public static void main(String[] args) {
        PortForwardRanges app = new PortForwardRanges();
    }
}

And creates a file called output.txt with this in it:

50 192.168.1.2 50
51 192.168.1.2 51
52 192.168.1.2 52
53 192.168.1.2 53
54 192.168.1.2 54
...

It goes from 50 to 100 and the local and destination ports are the same. Modify it as you wish.

3
  • Seems like an OK'ish solution. However the original program spawns a new thread for each redirection. So having 20,000 redirections simply kills the system. I think I need a lower level service for this.
    – sharjeel
    Aug 1, 2011 at 14:28
  • @sharjeel You could give rinetd a try. It claims to be able to do more than one port redirection per process. boutell.com/rinetd
    – James T
    Aug 1, 2011 at 17:34
  • Threads aren't processes though. But correct, 20.000 threads would also kill the system
    – sinni800
    Aug 1, 2011 at 20:55
3

Extending on James T's solution, here is a batch script:

something.cmd

FOR /L %G IN (50, 1, 100) DO ECHO %G 192.168.1.2 %G >> hi.txt

Explanation:

  • 50 is the starting number
  • 1 is the step for every loop
  • 100 is the end number

If you want to "move" the port range on the destination (ex: 50-100 on source, 100-150 on destination) use this batch script:

setlocal ENABLEDELAYEDEXPANSION 
FOR /L %%G IN (50, 1, 100) DO ( 
set /a dest=%%G+50 
ECHO %%G 192.168.1.2 !dest! >> hi.txt) 

The line set /a dest=%%G+50 sets your distance from the source ports.

If you want a longer, but easier to maintain script:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set start=150
set step=5
set end=200
set destdistance=-50
set ip=192.168.1.2
set outputfile=output.txt


FOR /L %%G IN (%start%, %step%, %end%) DO ( 
set /a dest=%%G%destdistance%
ECHO %%G %ip% !dest! >> %outputfile%) 
1
  • Beautifully simple little script
    – James T
    Aug 1, 2011 at 17:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .