473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Incrementing a Switch

I want to create a script that increments a value I specify a certian amount of times that I specify. for instance

somescript.pl -i 192.168.1.1 -n 254

-i is the value that I want to increment
-n is how many times I want it to increment

the output of the file would be to a file such as something like this.

my ip is 192.168.1.1
my ip is 192.168.1.2
my ip is 192.168.1.3
and so on and so
...
...
my ip is 192.168.1.254
Feb 23 '07 #1
19 2331
KevinADC
4,059 Expert 2GB
Post your current code and someone can see where you're stuck at.
Feb 24 '07 #2
@corefile

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. $i = '192.168.1.1';
  2. $n = 254;
  3.  
  4. for ( 1..$n ) {
  5.   $i =~ s/\.(\d+)$/.$_/;
  6.   printf("$i\n");
  7. }
Greetz, Doc
Feb 26 '07 #3
@corefile

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. $i = '192.168.1.1';
  2. $n = 254;
  3.  
  4. for ( 1..$n ) {
  5.   $i =~ s/\.(\d+)$/.$_/;
  6.   printf("$i\n");
  7. }
Greetz, Doc
Getting closer, when I run this I get:
192.168.1.1/n 192.168.1.2/n 192.168.1.3/n 192.168.1.4/n 192.168.1.5/n 192.168.1.6/n 192.168.1.7/n 192.168.1.8/n 192.168.1.9/n 192.168.1.10/n 192.168.1.11/n 192.168.1.12/n 192.168.1.13/n 192.168.1.14/n 192.168.1.15/n 192.168.1.16/n 192.168.1.17/n 192.168.1.18/n 192.168.1.19/n 192.168.1.20/n 192.168.1.21

Couple things, its running all togther. And instead of hard coding the value of the variable in the file, ie, $i = 192.168.1.1, I would like to be able to pass that value on a command line, such as somescript.sh -i 192.168.1.1 -n 254
Feb 26 '07 #4
Getting closer, when I run this I get:

Couple things, its running all togther. And instead of hard coding the value of the variable in the file, ie, $i = 192.168.1.1, I would like to be able to pass that value on a command line, such as somescript.sh -i 192.168.1.1 -n 254

Strike that, I fat fingered the code, it does not run together, it increments correcly, but I still need to have this take the variable value from a command line switch instead of hardcoding it in the script. and I also need to have the output to a file rather then to standard out.
Feb 26 '07 #5
@corefile

1) If you get an output like this:
Expand|Select|Wrap|Line Numbers
  1. 192.168.1.1/n 192.168.1.2/n ...
it looks like, that you have coded "\n" of my example as "/n".

2) Getting variables from the command line could be done like this (as just one of thousands possibilities):
Expand|Select|Wrap|Line Numbers
  1. while ( $opt=shift ) {
  2.   if ( $opt eq "-i" ) {
  3.     $i = $opt;
  4.   }
  5.   elsif ( $opt eq "-n" ) {
  6.     $n = $opt;
  7.   }
  8.   else {
  9.     <some kind of error message, e.g. "invalid option">
  10.   }
  11.  
  12.   if ( ! $i or ! $n ) {
  13.     <some kind of error message, e.g. "usage: arguments missing">
  14.   }
  15. }
3) Printing your output into a file instead to STDOUT:
Expand|Select|Wrap|Line Numbers
  1. open(FD, ">>", $file);
  2. printf(FD "<your output>");
  3. close(FD);
Greetz, Doc
Feb 26 '07 #6
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. my $ip = $ARGV[0];
  2. my $n  = $ARGV[1];
  3. my ($r,$s) = $ip =~ /(.*\.)(\d+)/; 
  4. print $r, $s++, "\n"  for ( 1..$n);
call it like so:

perl somescript.pl 192.168.1.1 254 >output.txt

of course you may want to add error messages and input validation if this is to be a general use application. You don't need the -i and -n "switches" unless you really wanted to use them (or need to use them for other reasons).
Feb 26 '07 #7
Expand|Select|Wrap|Line Numbers
  1. my $ip = $ARGV[0];
  2. my $n  = $ARGV[1];
  3. my ($r,$s) = $ip =~ /(.*\.)(\d+)/; 
  4. print $r, $s++, "\n"  for ( 1..$n);
call it like so:

perl somescript.pl 192.168.1.1 254 >output.txt

of course you may want to add error messages and input validation if this is to be a general use application. You don't need the -i and -n "switches" unless you really wanted to use them (or need to use them for other reasons).
I don't want to use $ARGV because I then have to remember the correct order, and I could mix them up, if I use explicit -i or whatever, I can use them in any order and it does not matter.
Feb 26 '07 #8
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. my $args = "@ARGV";
  2. my ($ip) = $args =~ /-i\s*(\S+)/;
  3. my ($n) = $args =~ /-n\s*(\S+)/;
  4. my ($r,$s) = $ip =~ /(.*\.)(\d+)/; 
  5. print $r, $s++, "\n"  for ( 1..$n);
all my previous caveats still apply. Is this class work or school work?
Feb 26 '07 #9
@corefile

Yes, Kevin's suggestion would work well! At least if you don't have to concern about misinvokements.

Consider this: if you, for example, call your script from another script, this kind of argument passing/parsing is quite save (in this cases, you should, for ease, follow the approach suggested), because it's under your controll (more or less). But, if you (or "anybody else") is allowed to call your script, then it could be (in general) of a high importance, whether the caller is aware about the concrete syntax of the called command or not.

If you enable a feature, like command line options, and if you have to verify whether the caller is quite concious about what he is doing or not (i.e. by introducing command line options introduced by "-", that's why they are invented!), it is pleasant to require to mark options (which argument means which thing?).

This remark is really not intended to oppose to Kevin's approach (I would like to emphasize this point). It just should give you a vague idea on "trade-offs", i.e. considerations which you have to take into account whenever you have to decide about command line options.

Just take it as one (of millions) suggestions.

Gretz, Doc
Feb 26 '07 #10
Oops!

My considerations could not have been taken serious so far, because I have made a mistake!

I would like to correct myself: The answer should clearly not be:
Expand|Select|Wrap|Line Numbers
  1.   if ( $opt eq "-i" ) {
  2.     $i = $opt;
  3.   }
but
Expand|Select|Wrap|Line Numbers
  1.   if ( $opt eq "-i" ) {
  2.     $i = shift;
  3.   }
But, I am happy so far that nobody has recognized it, yet.

Greetz, Doc
Feb 26 '07 #11
Expand|Select|Wrap|Line Numbers
  1. my $args = "@ARGV";
  2. my ($ip) = $args =~ /-i\s*(\S+)/;
  3. my ($n) = $args =~ /-n\s*(\S+)/;
  4. my ($r,$s) = $ip =~ /(.*\.)(\d+)/; 
  5. print $r, $s++, "\n"  for ( 1..$n);
all my previous caveats still apply. Is this class work or school work?
ok, I see. Starting to think it might be better to just leave them in the script itself.

As for what this is for, no not for school. its for my use in creating some rules. what i need to do is create a script that will out put to a file that I then can run as a batch file. (think creating a list of ACL's for my home router)

I need to create a statement such that I can pass a IP or number of IP's and the ports on those ip's and create a statement which uses these vairables

-i = 192.168.1.1 #staring ip
-n = 254 #number of ip's
-p = 80 443 22 #ports used for each ip (needs to be a seperate line for each port and ip)


And this would output to a file the following

add ip 192.168.1.1 80
add ip 192.168.1.1 443
add ip 192.168.1.1 22
add ip 192.168.1.2 80
add ip 192.168.1.2 443
add ip 192.168.1.2.22
...
...
add ip 192.168.1.254 80
add ip 192.168.1.254 443
add ip 192.168.1.254 22

Does this make sense?
Feb 26 '07 #12
KevinADC
4,059 Expert 2GB
it makes sense. but with the code that has been posted so far I think you can figure out how to do what you want. You can also look into using one of the GetOpt modules:

http://search.cpan.org/search?query=getopt&mode=all
Feb 27 '07 #13
it makes sense. but with the code that has been posted so far I think you can figure out how to do what you want. You can also look into using one of the GetOpt modules:

http://search.cpan.org/search?query=getopt&mode=all
First thanks for all the help, I'm trucking along, the one thing I'm stuck on is using the list of variables,

-p = 80,443,21

How do use those variable in my script so that each of the 254 ip has a ling for each port I specify?
Feb 27 '07 #14
KevinADC
4,059 Expert 2GB
for me to continue helping I will need to see the existing code you are using.
Feb 27 '07 #15
for me to continue helping I will need to see the existing code you are using.
Sure no problem

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3. $i = '192.168.1.1';     #Starting IP
  4. $n = 254;               #Number of IP's
  5. @p = ("80", "443", "22" , "21";     #Ports for each IP
  6. $sp = '10001';          #Starting port
  7. $splus = $sp++;      #increasing by 1
  8. $sip = 10.10.10.1    # static server ip
  9. $file = test_file;      #file output name
  10. chomp($time = 'date'_1);  #attempt to get month/day/time/year from date command
  11.  
  12. for ( 1..$n ) {
  13.   $i =~ s/\.(\d+)$/.$_/;
  14.   $splus = $sp++;
  15.   $time = $time++;
  16.         open(FD, ">>", $file);
  17.         printf (FD "rule$time $i @p is forwarded to $sip on port $splus\n");
  18.         close (FD);
  19. }

What I'm look for this to do is output to a file:

rule_022712392007_1 192.168.1.1 80 is forwarded to 10.10.10.1 on port 10001
rule_022712392007_2 192.168.1.1 443 is forwarded to 10.10.10.1 on port 10002
rule_022712392007_3 192.168.1.1 22 is forwarded to 10.10.10.1 on port 10003
rule_022712392007_4 192.168.1.1 21 is forwarded to 10.10.10.1 on port 10004
rule_022712392007_5 192.168.1.2 80 is forwarded to 10.10.10.1 on port 10005
rule_022712392007_6 192.168.1.2 443 is forwarded to 10.10.10.1 on port 10006
rule_022712392007_7 192.168.1.2 22 is forwarded to 10.10.10.1 on port 10007
rule_022712392007_8 192.168.1.2 21 is forwarded to 10.10.10.1 on port 10008
.....
rule_022712392007_1016 192.168.1.254 21 is forwarded to 10.10.10.1 on port 11016


having problems with the date portion and formating it, and how to handle the list of ports and inserting them. Also I'm sure there is a better way to hand the text, but that is the way I made that part work.
Feb 27 '07 #16
KevinADC
4,059 Expert 2GB
The code you posted is full of errors.

Is this "month-day-hour-minute-year" all strung together? 022712392007
Feb 28 '07 #17
KevinADC
4,059 Expert 2GB
the only thing you need do is figure out how to format your date/time string, I used epoch time for this example:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3. $i = '192.168.1.1';     #Starting IP
  4. $n = 254;               #Number of IP's
  5. @p = ("80", "443", "22" , "21");     #Ports for each IP
  6. $sp = '10001';          #Starting port
  7. $sip = '10.10.10.1';    # static server ip
  8. $file = 'test_file';      #file output name
  9. #chomp($time = 'date_1');  #attempt to get month/day/time/year from date command
  10. $time = time;
  11.  
  12. my ($ip,$r) = $i =~ /^(.*\.)(\d)$/;
  13. my $z = 1;
  14. open (my $fh, ">>", $file) or die "$!";  
  15. for ( 1..$n ) { 
  16.     foreach my $p (@p) {      
  17.         print $fh "rule_${time}_",$z++," $ip$r $p is forwarded to $sip on port ",$sp++,"\n";
  18.     }
  19.     $r++;
  20. }
  21. undef $fh;

output:

Expand|Select|Wrap|Line Numbers
  1. rule_1172626245_1 192.168.1.1 80 is forwarded to 10.10.10.1 on port 10001
  2. rule_1172626245_2 192.168.1.1 443 is forwarded to 10.10.10.1 on port 10002
  3. rule_1172626245_3 192.168.1.1 22 is forwarded to 10.10.10.1 on port 10003
  4. rule_1172626245_4 192.168.1.1 21 is forwarded to 10.10.10.1 on port 10004
  5. rule_1172626245_5 192.168.1.2 80 is forwarded to 10.10.10.1 on port 10005
  6. rule_1172626245_6 192.168.1.2 443 is forwarded to 10.10.10.1 on port 10006
  7. rule_1172626245_7 192.168.1.2 22 is forwarded to 10.10.10.1 on port 10007
  8. rule_1172626245_8 192.168.1.2 21 is forwarded to 10.10.10.1 on port 10008
  9. rule_1172626245_9 192.168.1.3 80 is forwarded to 10.10.10.1 on port 10009
  10. rule_1172626245_10 192.168.1.3 443 is forwarded to 10.10.10.1 on port 10010
  11. rule_1172626245_11 192.168.1.3 22 is forwarded to 10.10.10.1 on port 10011
  12. rule_1172626245_12 192.168.1.3 21 is forwarded to 10.10.10.1 on port 10012
  13. rule_1172626245_13 192.168.1.4 80 is forwarded to 10.10.10.1 on port 10013
  14. rule_1172626245_14 192.168.1.4 443 is forwarded to 10.10.10.1 on port 10014
  15. rule_1172626245_15 192.168.1.4 22 is forwarded to 10.10.10.1 on port 10015
  16. rule_1172626245_16 192.168.1.4 21 is forwarded to 10.10.10.1 on port 10016
  17. .
  18. .
  19. .
  20. .
  21. rule_1172626245_1009 192.168.1.253 80 is forwarded to 10.10.10.1 on port 11009
  22. rule_1172626245_1010 192.168.1.253 443 is forwarded to 10.10.10.1 on port 11010
  23. rule_1172626245_1011 192.168.1.253 22 is forwarded to 10.10.10.1 on port 11011
  24. rule_1172626245_1012 192.168.1.253 21 is forwarded to 10.10.10.1 on port 11012
  25. rule_1172626245_1013 192.168.1.254 80 is forwarded to 10.10.10.1 on port 11013
  26. rule_1172626245_1014 192.168.1.254 443 is forwarded to 10.10.10.1 on port 11014
  27. rule_1172626245_1015 192.168.1.254 22 is forwarded to 10.10.10.1 on port 11015
  28. rule_1172626245_1016 192.168.1.254 21 is forwarded to 10.10.10.1 on port 11016
  29.  
  30.  
Feb 28 '07 #18
the only thing you need do is figure out how to format your date/time string, I used epoch time for this example:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3. $i = '192.168.1.1';     #Starting IP
  4. $n = 254;               #Number of IP's
  5. @p = ("80", "443", "22" , "21");     #Ports for each IP
  6. $sp = '10001';          #Starting port
  7. $sip = '10.10.10.1';    # static server ip
  8. $file = 'test_file';      #file output name
  9. #chomp($time = 'date_1');  #attempt to get month/day/time/year from date command
  10. $time = time;
  11.  
  12. my ($ip,$r) = $i =~ /^(.*\.)(\d)$/;
  13. my $z = 1;
  14. open (my $fh, ">>", $file) or die "$!";  
  15. for ( 1..$n ) { 
  16.     foreach my $p (@p) {      
  17.         print $fh "rule_${time}_",$z++," $ip$r $p is forwarded to $sip on port ",$sp++,"\n";
  18.     }
  19.     $r++;
  20. }
  21. undef $fh;

output:

Expand|Select|Wrap|Line Numbers
  1. rule_1172626245_1 192.168.1.1 80 is forwarded to 10.10.10.1 on port 10001
  2. rule_1172626245_2 192.168.1.1 443 is forwarded to 10.10.10.1 on port 10002
  3. rule_1172626245_3 192.168.1.1 22 is forwarded to 10.10.10.1 on port 10003
  4. rule_1172626245_4 192.168.1.1 21 is forwarded to 10.10.10.1 on port 10004
  5. rule_1172626245_5 192.168.1.2 80 is forwarded to 10.10.10.1 on port 10005
  6. rule_1172626245_6 192.168.1.2 443 is forwarded to 10.10.10.1 on port 10006
  7. rule_1172626245_7 192.168.1.2 22 is forwarded to 10.10.10.1 on port 10007
  8. rule_1172626245_8 192.168.1.2 21 is forwarded to 10.10.10.1 on port 10008
  9. rule_1172626245_9 192.168.1.3 80 is forwarded to 10.10.10.1 on port 10009
  10. rule_1172626245_10 192.168.1.3 443 is forwarded to 10.10.10.1 on port 10010
  11. rule_1172626245_11 192.168.1.3 22 is forwarded to 10.10.10.1 on port 10011
  12. rule_1172626245_12 192.168.1.3 21 is forwarded to 10.10.10.1 on port 10012
  13. rule_1172626245_13 192.168.1.4 80 is forwarded to 10.10.10.1 on port 10013
  14. rule_1172626245_14 192.168.1.4 443 is forwarded to 10.10.10.1 on port 10014
  15. rule_1172626245_15 192.168.1.4 22 is forwarded to 10.10.10.1 on port 10015
  16. rule_1172626245_16 192.168.1.4 21 is forwarded to 10.10.10.1 on port 10016
  17. .
  18. .
  19. .
  20. .
  21. rule_1172626245_1009 192.168.1.253 80 is forwarded to 10.10.10.1 on port 11009
  22. rule_1172626245_1010 192.168.1.253 443 is forwarded to 10.10.10.1 on port 11010
  23. rule_1172626245_1011 192.168.1.253 22 is forwarded to 10.10.10.1 on port 11011
  24. rule_1172626245_1012 192.168.1.253 21 is forwarded to 10.10.10.1 on port 11012
  25. rule_1172626245_1013 192.168.1.254 80 is forwarded to 10.10.10.1 on port 11013
  26. rule_1172626245_1014 192.168.1.254 443 is forwarded to 10.10.10.1 on port 11014
  27. rule_1172626245_1015 192.168.1.254 22 is forwarded to 10.10.10.1 on port 11015
  28. rule_1172626245_1016 192.168.1.254 21 is forwarded to 10.10.10.1 on port 11016
  29.  
  30.  

perfect, epoch time will work just fine, thanks alot for all the help kevin.
Feb 28 '07 #19
KevinADC
4,059 Expert 2GB
You're welcome.
Feb 28 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
Anyone help on this? PHP/MySQL I have a repeat region displaying records from orders in a CSV format: CalJoe33,18,08/23/2004,FED EX,PREPAID,WEBSITE,CA,Book,25.95,??? CalJoe33,18,08/23/2004,FED...
3
by: Mothra | last post by:
Here's what I'm trying to do (kill off old Unix logins): --------------------- $i=0; while (<$who>) { chomp($_); my @line = split(/\s+/, $_); # Split it into an array next unless ($line...
2
by: Mike Brearley | last post by:
I have a counter (alright one I found on asp101.com) that checks for a session variable to prevent the counter from incrmenting if a user refreshes the page or returns to the page during the same...
6
by: J. Campbell | last post by:
Hi everyone. I'm sure that this is common knowledge for many/most here. However, it was rather illuminating for me (someone learning c++) and I thought it might be helpful for someone else. I'm...
2
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
2
by: Payson Books | last post by:
Where can I find some info on grabbing the recordID of the last record, incrementing it by one, and using the new number in a new record.
26
by: Bas Wassink | last post by:
Hi there, Does the ANSI standard say anything about incrementing variables past their limits ? When I compile code like this: unsigned char x = 255; x++; printf ( "%d\n", x );
53
by: subramanian100in | last post by:
I saw this question from www.brainbench.com void *ptr; myStruct myArray; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr +...
9
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.