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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Perl script - parse file - rcp results

Status
Not open for further replies.

limester

Technical User
Dec 29, 2004
69
CA
I would like to parse the contents of a text file. This text file is built from a sql query.

/text_file.txt

Contents of text_file.txt would be:

0123456789YYYYMMDDHHMMSS
0123456789YYYYMMDDHHMMSS
0123456789YYYYMMDDHHMMSS

each line will have a unique 'YYYYMMDDHHMMSS'

I will need to append .tar to each line and rcp to another server.

But, I will need to use the YYYYMMDD to specify target and source directory for the rcp command.

I also need to break up the YYYYMMDD into /YYYY/MM/DD/ as the file is stored this way in the filesystem (/YYYY/MM/DD/012YYYYMMDDHHMMSS.tar

Then use this to copy the file using rcp, then changing the permissions once it has copied to the other server.

For example:

rcp -p file.tar user@host:file.tar
rsh -l user host "chmod 755 file.tar"

I am new to Perl. With some help, I have been able to piece together a way to open the text_file.txt and make the filename.tar with appropriate source/destination directories (they are the same).

I need to add a subroutine to rcp and change permissions (as noted above).

Here is what I have so far:

Code:
#! /usr/bin/perl -w

sub copyDSTfile($);

# rsh user and host information
my $rshUser = "user";
my $rshHost = "192.168.1.1";

# destination paths
my $baseDSTpath = "/directory";
my $DSThost = "host";
     

my $infh = "";
my $name;
my $filepath;
my $filename;
my $fileyear;
my $filemonth;
my $fileday;

my $infile = "./text_file.txt";


	# -----------------------------------------
	# open files
	open ($infh, "<".$infile) || die "Couldn't open $infile for reading!";
	
	
	# -----------------------------------------
	# start loop
	while (<$infh>)
	{
		$name = $_;
		chomp $name;
		
		$fileyear = substr($name, 10, 4);
		$filemonth =substr($name, 14, 2);
		$fileday = substr($name, 16, 2);
		
		
		$filename = $baseDSTpath."/".$DSThost."/".$fileyear."/".$filemonth."/".$fileday."/".$name.".tar";
		logDebug($filename."\n");

Any help would be greatly appreciated!

Thanks!
 
you could use system() to run rcp -
$return_code = system ("rcp -p $filename user@host:$filename");
 
I have tried to add the following subroutine:
Code:
sub copyDSTfile($)
{
        # copy file to server
        my $copy;
        my $chperm;

        $copy = qx{ rcp -p $filename $rshUser $rshHost":"$filename };
        $chperm = qx{ rsh -l $rshUser $rshHost 'chmod 755 $filename' };

)

not sure if this will work, when I tried to add it to the main script, it fails with compilation errors. is there a simpler method to perform rcp from within a perl script?

Any help would be greatly appreciated!

Thanks!
 
Thanks for the reply Chazoid, I just missed your response earlier.

Can I just add this command to main script, not sure how to close it off, etc..

Cheers!
 
why are you adding quotes around the colon?
qx{ rcp -p $filename $rshUser $rshHost":"$filename }

that will end up being hostname":"filename just like that
I would think you would want hostname:filename
You should take your whole qx line and print it using qq{} instead of qx{} to see what it translates to.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
No prob.. You should be able to just put it inside the while loop after you build $filename, or put it in a sub and pass $filename to it.
To check your return codes, I think you have to bitshift the result - $return_code = $return_code >> 8;
then something like:
if ($return_code != 0) { print "something went wrong" }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top