using a list

Discussion in 'Perl Misc' started by corefile@gmail.com, Feb 27, 2007.

  1. Guest

    Trying to create a way to create a list of ip's port

    -ip = 192.168.100.1 #first ip
    -nip = 254 #number of ips
    @allports = ("25", "110", "443", "179") #listening ports
    -fip = 10.100.100.1 # static ip everything get forwarded to
    -fport = 10001 # starting port that @allports will forward to running
    on fip


    What I need to do is have it output a list of ips, in this case I'm
    starting with 192.168.100.1 and ending with 192.168.100.254. And for
    each of those ip's I need to associate it with each of the ports in my
    @allports list and out put that to a file. so I will look something
    like:

    192.168.100.1 25 maps to 10.100.100.1 20001
    192.168.100.1 110 maps to 10.100.100.1 20002
    192.168.100.1 443 maps to 10.100.100.1 20003
    192.168.100.1 179 maps to 10.100.100.1 20004
    192.168.100.2 25 maps to 10.100.100.1 20005
    192.168.100.2 110 maps to 10.100.100.1 20006
    192.168.100.2 443 maps to 10.100.100.1 20007
    192.168.100.2 179 maps to 10.100.100.1 20008
    192.168.100.3 25 maps to 10.100.100.1 20009
    .....
    .....
    192.168.100.254 179 maps to 10.100.100.1 21016

    This is what I have so far;


    $ip = 192.168.100.1; #first ip
    $nip = 254; #number of ips
    @allports = ("25", "110", "443", "179"); #listening ports
    $fip = 10.100.100.1; # static ip everything get forwarded to
    $fport = 10001; # starting port that @allports will forward to running
    on fip
    $file = somefile.txt;

    for ( 1..$n ) {
    $ip =~ s/\.(\d+)$/.$_/;
    $fport = $fport++;

    open (FD, ">>", $file);
    printf (FD "$ip maps to $fip $fport\n");
    close (FD);
    }


    I can't figure out how to implement the @allports part. As I'm a total
    noob this is probably not the most efficient way but as long as it
    work thats fine for me.
     
    , Feb 27, 2007
    #1
    1. Advertising

  2. wrote:
    > Trying to create a way to create a list of ip's port
    >
    > -ip = 192.168.100.1 #first ip
    > -nip = 254 #number of ips
    > @allports = ("25", "110", "443", "179") #listening ports
    > -fip = 10.100.100.1 # static ip everything get forwarded to
    > -fport = 10001 # starting port that @allports will forward to running
    > on fip
    >
    >
    > What I need to do is have it output a list of ips, in this case I'm
    > starting with 192.168.100.1 and ending with 192.168.100.254. And for
    > each of those ip's I need to associate it with each of the ports in my
    > @allports list and out put that to a file. so I will look something
    > like:
    >
    > 192.168.100.1 25 maps to 10.100.100.1 20001
    > 192.168.100.1 110 maps to 10.100.100.1 20002
    > 192.168.100.1 443 maps to 10.100.100.1 20003
    > 192.168.100.1 179 maps to 10.100.100.1 20004
    > 192.168.100.2 25 maps to 10.100.100.1 20005
    > 192.168.100.2 110 maps to 10.100.100.1 20006
    > 192.168.100.2 443 maps to 10.100.100.1 20007
    > 192.168.100.2 179 maps to 10.100.100.1 20008
    > 192.168.100.3 25 maps to 10.100.100.1 20009
    > ....
    > ....
    > 192.168.100.254 179 maps to 10.100.100.1 21016
    >
    > This is what I have so far;
    >
    >
    > $ip = 192.168.100.1; #first ip
    > $nip = 254; #number of ips
    > @allports = ("25", "110", "443", "179"); #listening ports
    > $fip = 10.100.100.1; # static ip everything get forwarded to
    > $fport = 10001; # starting port that @allports will forward to running
    > on fip
    > $file = somefile.txt;
    >
    > for ( 1..$n ) {
    > $ip =~ s/\.(\d+)$/.$_/;
    > $fport = $fport++;
    >
    > open (FD, ">>", $file);
    > printf (FD "$ip maps to $fip $fport\n");
    > close (FD);
    > }
    >
    >
    > I can't figure out how to implement the @allports part. As I'm a total
    > noob this is probably not the most efficient way but as long as it
    > work thats fine for me.



    #!/usr/bin/perl
    use warnings;
    use strict;
    use Socket;


    my $ip = unpack 'N', inet_aton '192.168.100.1'; #first ip
    my $number = 254; #number of ips
    my @allports = ( 25, 110, 443, 179 ); #listening ports
    my $forward = '10.100.100.1'; # static ip everything get forwarded to
    my $fport = 10001; # starting port that @allports will
    # forward to running on fip
    my $file = 'somefile.txt';


    open my $FD, '>>', $file or die "Cannot open '$file' $!";


    for ( 1 .. $number ) {
    my $addr = inet_ntoa pack 'N', $ip;
    for my $port ( @allports ) {
    print $FD "$addr $port maps to $forward $fport\n";
    ++$fport;
    }
    ++$ip;
    }

    close $FD;

    __END__





    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Feb 27, 2007
    #2
    1. Advertising

  3. Lambik Guest

    <> wrote in message
    news:...
    > Trying to create a way to create a list of ip's port

    Does this code even run?

    > $ip = 192.168.100.1; #first ip

    I have no idea what this does. I guess you want "192.168.100.1"

    > $nip = 254; #number of ips

    Why this? It isn't used

    > @allports = ("25", "110", "443", "179"); #listening ports
    > $fip = 10.100.100.1; # static ip everything get forwarded to

    Same here.

    > $fport = 10001; # starting port that @allports will forward to running
    > on fip
    > $file = somefile.txt;




    >
    > for ( 1..$n ) {

    What's $n? Where is it declared.

    > $ip =~ s/\.(\d+)$/.$_/;
    > $fport = $fport++;
    >
    > open (FD, ">>", $file);
    > printf (FD "$ip maps to $fip $fport\n");
    > close (FD);
    > }
    >
    >
    > I can't figure out how to implement the @allports part. As I'm a total
    > noob this is probably not the most efficient way but as long as it
    > work thats fine for me.


    Does it? This code runs? Wow.

    If I understand you correctly you want something like:

    #!/usr/bin/perl
    use warnings;
    use strict;
    my $ip = "192.168.100"; #first ip
    my @allports = ("25", "110", "443", "179"); #listening ports
    my $fip = "10.100.100.1"; # static ip everything get forwarded to
    my $fport = 10001; # starting port that @allports will forward to running on
    fip
    my $file = "somefile.txt";

    foreach my $port (@allports) {

    open (FD, ">>", $file);
    print FD "$ip.$port maps to $fip $fport\n";
    close (FD);
    $fport++;

    }
     
    Lambik, Feb 27, 2007
    #3
  4. Guest

    On Feb 27, 2:36 pm, "John W. Krahn" <> wrote:
    > wrote:
    > > Trying to create a way to create a list of ip's port

    >
    > > -ip = 192.168.100.1 #first ip
    > > -nip = 254 #number of ips
    > > @allports = ("25", "110", "443", "179") #listening ports
    > > -fip = 10.100.100.1 # static ip everything get forwarded to
    > > -fport = 10001 # starting port that @allports will forward to running
    > > on fip

    >
    > > What I need to do is have it output a list of ips, in this case I'm
    > > starting with 192.168.100.1 and ending with 192.168.100.254. And for
    > > each of those ip's I need to associate it with each of the ports in my
    > > @allports list and out put that to a file. so I will look something
    > > like:

    >
    > > 192.168.100.1 25 maps to 10.100.100.1 20001
    > > 192.168.100.1 110 maps to 10.100.100.1 20002
    > > 192.168.100.1 443 maps to 10.100.100.1 20003
    > > 192.168.100.1 179 maps to 10.100.100.1 20004
    > > 192.168.100.2 25 maps to 10.100.100.1 20005
    > > 192.168.100.2 110 maps to 10.100.100.1 20006
    > > 192.168.100.2 443 maps to 10.100.100.1 20007
    > > 192.168.100.2 179 maps to 10.100.100.1 20008
    > > 192.168.100.3 25 maps to 10.100.100.1 20009
    > > ....
    > > ....
    > > 192.168.100.254 179 maps to 10.100.100.1 21016

    >
    > > This is what I have so far;

    >
    > > $ip = 192.168.100.1; #first ip
    > > $nip = 254; #number of ips
    > > @allports = ("25", "110", "443", "179"); #listening ports
    > > $fip = 10.100.100.1; # static ip everything get forwarded to
    > > $fport = 10001; # starting port that @allports will forward to running
    > > on fip
    > > $file = somefile.txt;

    >
    > > for ( 1..$n ) {
    > > $ip =~ s/\.(\d+)$/.$_/;
    > > $fport = $fport++;

    >
    > > open (FD, ">>", $file);
    > > printf (FD "$ip maps to $fip $fport\n");
    > > close (FD);
    > > }

    >
    > > I can't figure out how to implement the @allports part. As I'm a total
    > > noob this is probably not the most efficient way but as long as it
    > > work thats fine for me.

    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > use Socket;
    >
    > my $ip = unpack 'N', inet_aton '192.168.100.1'; #first ip
    > my $number = 254; #number of ips
    > my @allports = ( 25, 110, 443, 179 ); #listening ports
    > my $forward = '10.100.100.1'; # static ip everything get forwarded to
    > my $fport = 10001; # starting port that @allports will
    > # forward to running on fip
    > my $file = 'somefile.txt';
    >
    > open my $FD, '>>', $file or die "Cannot open '$file' $!";
    >
    > for ( 1 .. $number ) {
    > my $addr = inet_ntoa pack 'N', $ip;
    > for my $port ( @allports ) {
    > print $FD "$addr $port maps to $forward $fport\n";
    > ++$fport;
    > }
    > ++$ip;
    > }
    >
    > close $FD;
    >
    > __END__
    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you can special-order
    > certain sorts of tools at low cost and in short order. -- Larry Wall


    perfect that worked great, one thing I forgot. I need to also have a
    variable that takes the date command and makes a name for it.
    I need to use the month/date/time/year + a incrementing number, so
    building on what we have:


    rule 022715362007_1 192.168.100.1 25 maps to 10.100.100.1 20001
    rule 022715362007_2 192.168.100.1 110 maps to 10.100.100.1 20002
    rule 022715362007_3 192.168.100.1 443 maps to 10.100.100.1 20003
    rule 022715362007_4 192.168.100.1 179 maps to 10.100.100.1 20004
    rule 022715362007_5 192.168.100.2 25 maps to 10.100.100.1 20005
    rule 022715362007_6 192.168.100.2 110 maps to 10.100.100.1 20006
    rule 022715362007_7 192.168.100.2 443 maps to 10.100.100.1 20007
    rule 022715362007_8 192.168.100.2 179 maps to 10.100.100.1 20008
    rule 022715362007_9 192.168.100.3 25 maps to 10.100.100.1 20009
     
    , Feb 27, 2007
    #4
  5. DJ Stunks Guest

    On Feb 27, 4:40 pm, wrote:
    > On Feb 27, 2:36 pm, "John W. Krahn" <> wrote:
    >
    >
    >
    >
    >
    > > wrote:
    > > > Trying to create a way to create a list of ip's port

    >
    > > > -ip = 192.168.100.1 #first ip
    > > > -nip = 254 #number of ips
    > > > @allports = ("25", "110", "443", "179") #listening ports
    > > > -fip = 10.100.100.1 # static ip everything get forwarded to
    > > > -fport = 10001 # starting port that @allports will forward to running
    > > > on fip

    >
    > > > What I need to do is have it output a list of ips, in this case I'm
    > > > starting with 192.168.100.1 and ending with 192.168.100.254. And for
    > > > each of those ip's I need to associate it with each of the ports in my
    > > > @allports list and out put that to a file. so I will look something
    > > > like:

    >
    > > > 192.168.100.1 25 maps to 10.100.100.1 20001
    > > > 192.168.100.1 110 maps to 10.100.100.1 20002
    > > > 192.168.100.1 443 maps to 10.100.100.1 20003
    > > > 192.168.100.1 179 maps to 10.100.100.1 20004
    > > > 192.168.100.2 25 maps to 10.100.100.1 20005
    > > > 192.168.100.2 110 maps to 10.100.100.1 20006
    > > > 192.168.100.2 443 maps to 10.100.100.1 20007
    > > > 192.168.100.2 179 maps to 10.100.100.1 20008
    > > > 192.168.100.3 25 maps to 10.100.100.1 20009
    > > > ....
    > > > ....
    > > > 192.168.100.254 179 maps to 10.100.100.1 21016

    >
    > > > This is what I have so far;

    >
    > > > $ip = 192.168.100.1; #first ip
    > > > $nip = 254; #number of ips
    > > > @allports = ("25", "110", "443", "179"); #listening ports
    > > > $fip = 10.100.100.1; # static ip everything get forwarded to
    > > > $fport = 10001; # starting port that @allports will forward to running
    > > > on fip
    > > > $file = somefile.txt;

    >
    > > > for ( 1..$n ) {
    > > > $ip =~ s/\.(\d+)$/.$_/;
    > > > $fport = $fport++;

    >
    > > > open (FD, ">>", $file);
    > > > printf (FD "$ip maps to $fip $fport\n");
    > > > close (FD);
    > > > }

    >
    > > > I can't figure out how to implement the @allports part. As I'm a total
    > > > noob this is probably not the most efficient way but as long as it
    > > > work thats fine for me.

    >
    > > #!/usr/bin/perl
    > > use warnings;
    > > use strict;
    > > use Socket;

    >
    > > my $ip = unpack 'N', inet_aton '192.168.100.1'; #first ip
    > > my $number = 254; #number of ips
    > > my @allports = ( 25, 110, 443, 179 ); #listening ports
    > > my $forward = '10.100.100.1'; # static ip everything get forwarded to
    > > my $fport = 10001; # starting port that @allports will
    > > # forward to running on fip
    > > my $file = 'somefile.txt';

    >
    > > open my $FD, '>>', $file or die "Cannot open '$file' $!";

    >
    > > for ( 1 .. $number ) {
    > > my $addr = inet_ntoa pack 'N', $ip;
    > > for my $port ( @allports ) {
    > > print $FD "$addr $port maps to $forward $fport\n";
    > > ++$fport;
    > > }
    > > ++$ip;
    > > }

    >
    > > close $FD;

    >
    > > __END__

    >
    > > John
    > > --
    > > Perl isn't a toolbox, but a small machine shop where you can special-order
    > > certain sorts of tools at low cost and in short order. -- Larry Wall

    >
    > perfect that worked great, one thing I forgot. I need to also have a
    > variable that takes the date command and makes a name for it.
    > I need to use the month/date/time/year + a incrementing number, so
    > building on what we have:
    >
    > rule 022715362007_1 192.168.100.1 25 maps to 10.100.100.1 20001
    > rule 022715362007_2 192.168.100.1 110 maps to 10.100.100.1 20002
    > rule 022715362007_3 192.168.100.1 443 maps to 10.100.100.1 20003
    > rule 022715362007_4 192.168.100.1 179 maps to 10.100.100.1 20004
    > rule 022715362007_5 192.168.100.2 25 maps to 10.100.100.1 20005
    > rule 022715362007_6 192.168.100.2 110 maps to 10.100.100.1 20006
    > rule 022715362007_7 192.168.100.2 443 maps to 10.100.100.1 20007
    > rule 022715362007_8 192.168.100.2 179 maps to 10.100.100.1 20008
    > rule 022715362007_9 192.168.100.3 25 maps to 10.100.100.1 20009


    I suspect you will have no problems modifying John's compact and
    straightforward script to accomplish this simple modification. I
    would probably use POSIX's strftime to create your datetime string
    (see `perldoc POSIX` for details).

    Incidentally, I would also make your output much less chatty so that
    it would be more portable - a tab or comma separated table would be
    more forward-looking IMO.

    -jp
     
    DJ Stunks, Feb 28, 2007
    #5
  6. wrote:
    > On Feb 27, 2:36 pm, "John W. Krahn" <> wrote:
    >>
    >> #!/usr/bin/perl
    >> use warnings;
    >> use strict;
    >> use Socket;
    >>
    >> my $ip = unpack 'N', inet_aton '192.168.100.1'; #first ip
    >> my $number = 254; #number of ips
    >> my @allports = ( 25, 110, 443, 179 ); #listening ports
    >> my $forward = '10.100.100.1'; # static ip everything get forwarded to
    >> my $fport = 10001; # starting port that @allports will
    >> # forward to running on fip
    >> my $file = 'somefile.txt';
    >>
    >> open my $FD, '>>', $file or die "Cannot open '$file' $!";
    >>
    >> for ( 1 .. $number ) {
    >> my $addr = inet_ntoa pack 'N', $ip;
    >> for my $port ( @allports ) {
    >> print $FD "$addr $port maps to $forward $fport\n";
    >> ++$fport;
    >> }
    >> ++$ip;
    >> }
    >>
    >> close $FD;
    >>
    >> __END__

    >
    > perfect that worked great, one thing I forgot. I need to also have a
    > variable that takes the date command and makes a name for it.
    > I need to use the month/date/time/year + a incrementing number, so
    > building on what we have:
    >
    >
    > rule 022715362007_1 192.168.100.1 25 maps to 10.100.100.1 20001
    > rule 022715362007_2 192.168.100.1 110 maps to 10.100.100.1 20002
    > rule 022715362007_3 192.168.100.1 443 maps to 10.100.100.1 20003
    > rule 022715362007_4 192.168.100.1 179 maps to 10.100.100.1 20004
    > rule 022715362007_5 192.168.100.2 25 maps to 10.100.100.1 20005
    > rule 022715362007_6 192.168.100.2 110 maps to 10.100.100.1 20006
    > rule 022715362007_7 192.168.100.2 443 maps to 10.100.100.1 20007
    > rule 022715362007_8 192.168.100.2 179 maps to 10.100.100.1 20008
    > rule 022715362007_9 192.168.100.3 25 maps to 10.100.100.1 20009


    #!/usr/bin/perl
    use warnings;
    use strict;
    use Socket;


    my ( $min, $hour, $day, $mon, $year ) = ( localtime )[ 1 .. 5 ];
    my $date = sprintf '%02d%02d%02d%02d%04d_', $mon + 1, $day, $hour, $min,
    $year + 1900;
    my $ip = unpack 'N', inet_aton '192.168.100.1'; #first ip
    my $number = 254; #number of ips
    my @allports = ( 25, 110, 443, 179 ); #listening ports
    my $forward = '10.100.100.1'; # static ip everything get forwarded to
    my $fport = 10000; # starting port that @allports will
    # forward to running on fip
    my $incr = 1;
    my $file = 'somefile.txt';

    open my $FD, '>>', $file or die "Cannot open '$file' $!";

    for ( 1 .. $number ) {
    my $addr = inet_ntoa pack 'N', $ip;
    for my $port ( @allports ) {
    print $FD "rule $date$incr $addr $port maps to $forward ", $fport +
    $incr, "\n";
    ++$incr;
    }
    ++$ip;
    }

    close $FD;

    __END__



    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Feb 28, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    755
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    491
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    692
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    394
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,302
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page