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
19 2331
Post your current code and someone can see where you're stuck at.
@corefile
Try something like this: - $i = '192.168.1.1';
-
$n = 254;
-
-
for ( 1..$n ) {
-
$i =~ s/\.(\d+)$/.$_/;
-
printf("$i\n");
-
}
Greetz, Doc
@corefile
Try something like this: - $i = '192.168.1.1';
-
$n = 254;
-
-
for ( 1..$n ) {
-
$i =~ s/\.(\d+)$/.$_/;
-
printf("$i\n");
-
}
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
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.
@corefile
1) If you get an output like this: - 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): - while ( $opt=shift ) {
-
if ( $opt eq "-i" ) {
-
$i = $opt;
-
}
-
elsif ( $opt eq "-n" ) {
-
$n = $opt;
-
}
-
else {
-
<some kind of error message, e.g. "invalid option">
-
}
-
-
if ( ! $i or ! $n ) {
-
<some kind of error message, e.g. "usage: arguments missing">
-
}
-
}
3) Printing your output into a file instead to STDOUT: - open(FD, ">>", $file);
-
printf(FD "<your output>");
-
close(FD);
Greetz, Doc
- my $ip = $ARGV[0];
-
my $n = $ARGV[1];
-
my ($r,$s) = $ip =~ /(.*\.)(\d+)/;
-
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).
- my $ip = $ARGV[0];
-
my $n = $ARGV[1];
-
my ($r,$s) = $ip =~ /(.*\.)(\d+)/;
-
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.
- my $args = "@ARGV";
-
my ($ip) = $args =~ /-i\s*(\S+)/;
-
my ($n) = $args =~ /-n\s*(\S+)/;
-
my ($r,$s) = $ip =~ /(.*\.)(\d+)/;
-
print $r, $s++, "\n" for ( 1..$n);
all my previous caveats still apply. Is this class work or school work?
@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
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: - if ( $opt eq "-i" ) {
-
$i = $opt;
-
}
but - if ( $opt eq "-i" ) {
-
$i = shift;
-
}
But, I am happy so far that nobody has recognized it, yet.
Greetz, Doc
- my $args = "@ARGV";
-
my ($ip) = $args =~ /-i\s*(\S+)/;
-
my ($n) = $args =~ /-n\s*(\S+)/;
-
my ($r,$s) = $ip =~ /(.*\.)(\d+)/;
-
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?
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?
for me to continue helping I will need to see the existing code you are using.
for me to continue helping I will need to see the existing code you are using.
Sure no problem - #! /usr/bin/perl -w
-
-
$i = '192.168.1.1'; #Starting IP
-
$n = 254; #Number of IP's
-
@p = ("80", "443", "22" , "21"; #Ports for each IP
-
$sp = '10001'; #Starting port
-
$splus = $sp++; #increasing by 1
-
$sip = 10.10.10.1 # static server ip
-
$file = test_file; #file output name
-
chomp($time = 'date'_1); #attempt to get month/day/time/year from date command
-
-
for ( 1..$n ) {
-
$i =~ s/\.(\d+)$/.$_/;
-
$splus = $sp++;
-
$time = $time++;
-
open(FD, ">>", $file);
-
printf (FD "rule$time $i @p is forwarded to $sip on port $splus\n");
-
close (FD);
-
}
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.
The code you posted is full of errors.
Is this "month-day-hour-minute-year" all strung together? 022712392007
the only thing you need do is figure out how to format your date/time string, I used epoch time for this example: - #! /usr/bin/perl -w
-
-
$i = '192.168.1.1'; #Starting IP
-
$n = 254; #Number of IP's
-
@p = ("80", "443", "22" , "21"); #Ports for each IP
-
$sp = '10001'; #Starting port
-
$sip = '10.10.10.1'; # static server ip
-
$file = 'test_file'; #file output name
-
#chomp($time = 'date_1'); #attempt to get month/day/time/year from date command
-
$time = time;
-
-
my ($ip,$r) = $i =~ /^(.*\.)(\d)$/;
-
my $z = 1;
-
open (my $fh, ">>", $file) or die "$!";
-
for ( 1..$n ) {
-
foreach my $p (@p) {
-
print $fh "rule_${time}_",$z++," $ip$r $p is forwarded to $sip on port ",$sp++,"\n";
-
}
-
$r++;
-
}
-
undef $fh;
output: -
rule_1172626245_1 192.168.1.1 80 is forwarded to 10.10.10.1 on port 10001
-
rule_1172626245_2 192.168.1.1 443 is forwarded to 10.10.10.1 on port 10002
-
rule_1172626245_3 192.168.1.1 22 is forwarded to 10.10.10.1 on port 10003
-
rule_1172626245_4 192.168.1.1 21 is forwarded to 10.10.10.1 on port 10004
-
rule_1172626245_5 192.168.1.2 80 is forwarded to 10.10.10.1 on port 10005
-
rule_1172626245_6 192.168.1.2 443 is forwarded to 10.10.10.1 on port 10006
-
rule_1172626245_7 192.168.1.2 22 is forwarded to 10.10.10.1 on port 10007
-
rule_1172626245_8 192.168.1.2 21 is forwarded to 10.10.10.1 on port 10008
-
rule_1172626245_9 192.168.1.3 80 is forwarded to 10.10.10.1 on port 10009
-
rule_1172626245_10 192.168.1.3 443 is forwarded to 10.10.10.1 on port 10010
-
rule_1172626245_11 192.168.1.3 22 is forwarded to 10.10.10.1 on port 10011
-
rule_1172626245_12 192.168.1.3 21 is forwarded to 10.10.10.1 on port 10012
-
rule_1172626245_13 192.168.1.4 80 is forwarded to 10.10.10.1 on port 10013
-
rule_1172626245_14 192.168.1.4 443 is forwarded to 10.10.10.1 on port 10014
-
rule_1172626245_15 192.168.1.4 22 is forwarded to 10.10.10.1 on port 10015
-
rule_1172626245_16 192.168.1.4 21 is forwarded to 10.10.10.1 on port 10016
-
.
-
.
-
.
-
.
-
rule_1172626245_1009 192.168.1.253 80 is forwarded to 10.10.10.1 on port 11009
-
rule_1172626245_1010 192.168.1.253 443 is forwarded to 10.10.10.1 on port 11010
-
rule_1172626245_1011 192.168.1.253 22 is forwarded to 10.10.10.1 on port 11011
-
rule_1172626245_1012 192.168.1.253 21 is forwarded to 10.10.10.1 on port 11012
-
rule_1172626245_1013 192.168.1.254 80 is forwarded to 10.10.10.1 on port 11013
-
rule_1172626245_1014 192.168.1.254 443 is forwarded to 10.10.10.1 on port 11014
-
rule_1172626245_1015 192.168.1.254 22 is forwarded to 10.10.10.1 on port 11015
-
rule_1172626245_1016 192.168.1.254 21 is forwarded to 10.10.10.1 on port 11016
-
-
the only thing you need do is figure out how to format your date/time string, I used epoch time for this example: - #! /usr/bin/perl -w
-
-
$i = '192.168.1.1'; #Starting IP
-
$n = 254; #Number of IP's
-
@p = ("80", "443", "22" , "21"); #Ports for each IP
-
$sp = '10001'; #Starting port
-
$sip = '10.10.10.1'; # static server ip
-
$file = 'test_file'; #file output name
-
#chomp($time = 'date_1'); #attempt to get month/day/time/year from date command
-
$time = time;
-
-
my ($ip,$r) = $i =~ /^(.*\.)(\d)$/;
-
my $z = 1;
-
open (my $fh, ">>", $file) or die "$!";
-
for ( 1..$n ) {
-
foreach my $p (@p) {
-
print $fh "rule_${time}_",$z++," $ip$r $p is forwarded to $sip on port ",$sp++,"\n";
-
}
-
$r++;
-
}
-
undef $fh;
output: -
rule_1172626245_1 192.168.1.1 80 is forwarded to 10.10.10.1 on port 10001
-
rule_1172626245_2 192.168.1.1 443 is forwarded to 10.10.10.1 on port 10002
-
rule_1172626245_3 192.168.1.1 22 is forwarded to 10.10.10.1 on port 10003
-
rule_1172626245_4 192.168.1.1 21 is forwarded to 10.10.10.1 on port 10004
-
rule_1172626245_5 192.168.1.2 80 is forwarded to 10.10.10.1 on port 10005
-
rule_1172626245_6 192.168.1.2 443 is forwarded to 10.10.10.1 on port 10006
-
rule_1172626245_7 192.168.1.2 22 is forwarded to 10.10.10.1 on port 10007
-
rule_1172626245_8 192.168.1.2 21 is forwarded to 10.10.10.1 on port 10008
-
rule_1172626245_9 192.168.1.3 80 is forwarded to 10.10.10.1 on port 10009
-
rule_1172626245_10 192.168.1.3 443 is forwarded to 10.10.10.1 on port 10010
-
rule_1172626245_11 192.168.1.3 22 is forwarded to 10.10.10.1 on port 10011
-
rule_1172626245_12 192.168.1.3 21 is forwarded to 10.10.10.1 on port 10012
-
rule_1172626245_13 192.168.1.4 80 is forwarded to 10.10.10.1 on port 10013
-
rule_1172626245_14 192.168.1.4 443 is forwarded to 10.10.10.1 on port 10014
-
rule_1172626245_15 192.168.1.4 22 is forwarded to 10.10.10.1 on port 10015
-
rule_1172626245_16 192.168.1.4 21 is forwarded to 10.10.10.1 on port 10016
-
.
-
.
-
.
-
.
-
rule_1172626245_1009 192.168.1.253 80 is forwarded to 10.10.10.1 on port 11009
-
rule_1172626245_1010 192.168.1.253 443 is forwarded to 10.10.10.1 on port 11010
-
rule_1172626245_1011 192.168.1.253 22 is forwarded to 10.10.10.1 on port 11011
-
rule_1172626245_1012 192.168.1.253 21 is forwarded to 10.10.10.1 on port 11012
-
rule_1172626245_1013 192.168.1.254 80 is forwarded to 10.10.10.1 on port 11013
-
rule_1172626245_1014 192.168.1.254 443 is forwarded to 10.10.10.1 on port 11014
-
rule_1172626245_1015 192.168.1.254 22 is forwarded to 10.10.10.1 on port 11015
-
rule_1172626245_1016 192.168.1.254 21 is forwarded to 10.10.10.1 on port 11016
-
-
perfect, epoch time will work just fine, thanks alot for all the help kevin.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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 );
|
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 +...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |