Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy stuff in increments of 10 2

Status
Not open for further replies.

Stiddy

IS-IT--Management
Dec 5, 2001
781
US
I have a little routine where I read in a text file full of workstations or servers to perform some kinda of action on. Maybe copy a file, create a directory, etc. So I started thinking about trying to improve on it a little. For example: If I have five thousand workstations that I have to copy a file to I know I can use the File::Copy module and read in my list of wks's and copy the file to each machine one by one. But what if I could copy say in increments of 10 or 15 wks's at a time. That would increase the speed of this routine. I don't really know where to start. Could someone point me in the right direction. Here is my normal routine.

use File::Copy;
use Net::ping;
my $host = Net::ping->new("icmp");


##### Open logfiles for input and Read in machines to be tested #####
open (COPIED_FILE, ">>Copied_FIle.log");
open (UNREACHABLE, ">>UnReachable.log");
open (WKS, "<d:All_Workstations.log") || die ("Cannot open All_Workstations.log: $!");
while(<WKS>) {
chomp $_;
$_ =~ tr/a-z/A-Z/;
if ($host->ping($_, 5)){&COPY_DATA}
else {
print "$_ unreachable\n";
print UNREACHABLE ("$_\n")
}
}
close COPIED_FILE;
close UNREACHABLE;
close WKS;
$host->close();

sub COPY_DATA {
copy("blah.txt","\\\\$_\\c\$\\DIRECTORY");
}
###################################################
ALL_Workstations.log

workstation1
workstation2
workstation3
workstation4
workstation5
workstation6
workstation7
workstation8
.....

HTH - Stiddy
 
Hmm...wouldn't it be easier to make this as a normal shell script?
e.g.

scp myfile myuser@somehost:/some/directory/ &

just a thought.

--
M.Sc. in Computer Science
Freelancing software engineer
visit to contact me regarding software projects
 
I guess I was trying to figure out how to read in the text file pass ten arguments at a time to a subroutine...Any one have any idea how to accomplish this?

HTH - Stiddy
 
you can pass a list of scalars to subroutines

my @args = ($first, $second , $third , .......... $tenth);
do_somthing(@args);

or like this:

do_something($first, $second , $third , .......... $tenth);


and in the sub routine:

sub do_something {
my @args = @_;
.....
}
 
Could you give me a little more to start with. I am not so good with arrays...

HTH - Stiddy
 
Could someone please explain this one a little futher. I have tried to make it work with Kevin's suggestions but coming up dry....

HTH - Stiddy
 
What do you mean "coming up dry"? What do you have and what is it doing or not doing?


Trojan.
 
This is all I have and it prints all of the characters in text.log and it should only be printing the first two words.

@ARGS = ("text.log");
while ($ARGS = <>) {
my @ARGS = ($one, $two);
print $one $two;
`sleep 5`;
}


### text.log ###
red
yellow
green
purple
fruit
salad
down
up
left
right
tall
short
fat
skinny
back
forward

HTH - Stiddy
 
I don't understand any of that!
If you are trying to print just the first 2 records:
Code:
my $one = <>;
my $two = <>;
print $one,$two;


Trojan.
 
I am trying to read in a text file with say 1000 workstations in it and perform some action on them 10 at a time. Once the actions has be performed on those 10 then go to the next 10 and do the same action, loop, loop. Sorry that you could not understand my code as i am not that great at perl yet...

HTH - Stiddy
 
OK,
Try this:
Code:
#!/usr/bin/perl -w
use strict;

my $batchsize = 4;
my $count     = $batchsize;
my @items     = ();
while(<DATA>) {
  chomp;
  if($count--) {
    push @items, $_;
    next;
  }
  process_items(\@items);
  @items = ();
  $count = $batchsize;
}
process_items(\@items) if(@items);

sub process_items {
  my $arrayref = shift;
  print "<", join("><", @$arrayref), ">\n";
}

__DATA__
red
yellow
green
purple
fruit
salad
down
up
left
right
tall
short
fat
skinny
back
forward


Trojan.
 
#!d:\\perl\\bin\\perl -w

use strict;

@DATA = ("text.log");
my $batchsize = 4;
my $count = $batchsize;
my @items = ();
while(<DATA>) {
chomp;
if($count--) {
push @items, $_;
next;
}
process_items(\@items);
@items = ();
$count = $batchsize;
}
process_items(\@items) if(@items);

sub process_items {
my $arrayref = shift;
print "<", join("><", @$arrayref), ">\n";
}


I get :


Global symbol "@DATA" requires explicit package name at D:\scripts\Test\Stuff.pl
line 5.
Execution of D:\scripts\Test\Stuff.pl aborted due to compilation errors.


HTH - Stiddy
 
You added the error in the line you added.
If you ran the code I gave you it will work.
If you want to run it against a file then change "<DATA>" for "<>" and run it with your filename on the command line after the name of the perl script. If the script is called "process.pl" then call it like this:
"perl process.pl text.log"


Trojan.
 
So why does it skip in the text.log file the words fruit, right, and back?

HTH - Stiddy
 
open (DATA, "text.log") || die ("Cannot open All_Workstations.log: $!");
my $batchsize = 10;


Try that
 
Sorry, my fault.
Try this:
Code:
#!/usr/bin/perl -w
use strict;

my $batchsize = 4;
my $count     = $batchsize;
my @items     = ();
while(<DATA>) {
  chomp;
  if($count--) {
    push @items, $_;
    next;
  }
  process_items(\@items);
  @items = ($_);
  $count = $batchsize;
}
process_items(\@items) if(@items);

sub process_items {
  my $arrayref = shift;
  print "<", join("><", @$arrayref), ">\n";
}

__DATA__
red
yellow
green
purple
fruit
salad
down
up
left
right
tall
short
fat
skinny
back
forward


Trojan.
 
use strict;
open (DATA, "text.log") || die ("Cannot open All_Workstations.log: $!");
my $batchsize = 10;
my $count = $batchsize;
my @items = ();
while(<DATA>) {
#while(<>) {
chomp;
if($count--) {
push @items, $_;
next;
}
process_items(\@items);
@items = ();
$count = $batchsize;
}
process_items(\@items) if(@items);

sub process_items {
my $arrayref = shift;
print "<", join("><", @$arrayref), ">\n";
`sleep 5`;
}

That works great, so now in sub process_items how do I say:

copy file23.txt \\\\some_workstation\\c\$\\a_directory

How do I refer to in input from the text.log file. I am guessing $arrayref

HTH - Stiddy
 
use strict;


open (DATA, "text.log") || die ("Cannot open All_Workstations.log: $!");
my $batchsize = 4;
my $count = $batchsize;
my @items = ();


while(<DATA>) {
chomp;
if($count--) {
push @items, $_;
unless($count) {
process_items(\@items);
@items = ();
$count = $batchsize;
}
next;
}
}
process_items(\@items) if(@items);

sub process_items {
my $arrayref = shift;
print "<", join("><", @$arrayref), ">\n";
}
 
Trojan,

I can't get that code to work correctly. It seperates 4 then its 5 after that.

Did you run that or am I doing something wrong?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top