How to read a file which is on server using perl script - perl

I have a file on my perforce client which i can read from the client but i want to read it from the depot instead from my client and i Have to achieve this using a Perl script and my client name is ata and its root directory is /home/ata/hw
The following code is written for the file on my client
my $clients_file="/home/ata/hw/hard/ip/golden_design_map.cfg";
open(READ,"<$clients_file") or die "Couldn't open the file for reading:$!";
But this is what i want to achieve
my $clients_file="//hw/hard/ip/golden_design_map.cfg";
open(READ,"<$clients_file") or die "Couldn't open the file for reading:$!";
Here //hw/hard/ip/golden_design_map.cfg is the file on the depot or the server.
Is there any module which i can use.Any help is truly appreciated.Thanks in advance.

You can use the p4 command line like this:
open( READ, "p4 print -q $clients_file|" ) or die "Couldn't execute p4:$!";
This assumes that your environment is already set up to run p4 commands (p4 executable in the PATH, valid settings for P4PORT and P4USER, valid login ticket acquired by having run "p4 login" previously, etc).
Or you can use the Perforce Perl module: http://www.perforce.com/perforce/doc.current/manuals/p4script/02_perl.html

You could use File::Remote module.
Synopsis:
use File::Remote;
# read from a remote file
open(REMOTE, "host:/remote/file") or die $!;
print while (<REMOTE>);
close(REMOTE);

Related

Perl SSH to remote server and check file exists

I have developed a script which should login into remote server and do certain operation.
In remote server it will check for sample_file.txt file exists or not, if YES then it should create create_user_file.txt file in home path with certain set of data.
The thing here is I am able to do ssh to remote machine though below script but unable to check for the files since I don't have idea how we can open the remote session and do all the operation.
Below is my code:
#!/usr/bin/perl
use Net::OpenSSH;
.
.
.
foreach my $ip( #list_of_ips){
print "Connecting to $ip...\n";
my $ssh = OpenSSH_Connection( $ip, $user, $passwd );
print "Connected!\n";
$dir = "/remote/server/home/path/";
$file = $dir."sample_file.txt";
if (-e $file){ #if sample_file.txt exists then create user file
open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
print $fh "ID=123&PW=Passw0rd";
close $fh;
}
last if($ssh); #if connection is success no need to login into another server
$ssh->close();
}
sub OpenSSH_Connection {
my ( $host, $user, $passwd ) = #_;
my $ssh = Net::OpenSSH->new("$user:$passwd\#$host");
$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
return $ssh;
}
Here in the code, below part of code should be executed in the remote server. But its been checking for a file from where I am executing this script.
if (-e $file){ #if sample_file.txt exists then create user file
open(my $fh, '>', $dir."create_user_file.txt") || die "Couldn't open file $!";;
print $fh "ID=123&PW=Passw0rd";
close $fh;
}
How can I keep open the remote session of the server and execute above code, or do we have any Perl module which could do this kind of operation from local server. TIA.
Your assumption that Net::SSH will make remote resources available locally is incorrect.
SSH creates encrypted channel to remote system and drops you either into shell, or runs remote command (for example perl/shell script), or allows to copy/retrieve a file.
You can open SSH channel into remote shell and run commands as on local computer, but if you want to check if file exists on remote system then you have to communicate it through remote shell or perl script.
Probably what you are after is SSH FS which may make remote files available locally.
I must make note that users on local and remote system can have different id on system level and it might be not what you want (manipulate files of different user). This approach may work well if your local and remote id is the same and belongs to you.
I guess that most easy way would be write perl script on remote system and pass some arguments to it. The script on remote system will do rest part of the job.
In such situation you could use Net::SSH2 to establish connection into remote shell and then run remote perl script with parameters.
To get a better idea how it works without creating long post I refer you to PerlMonks Net::SSH2 demo.
Other more difficult way would be writing enough perl code to establish connection with remote system over SSH2 protocol to remote perl script with capability to 'translate' your local commands
$chan->command($argument)
into remote perl code blocks to perform particular task.
Net::SSH2

perl novice, need assistance with handling file script

Very new to Perl. Running Perl on Padre and Windows 10 OS.
The script from my book is written for Unix. I don't know how to correct it so that it works with Windows.
Here is the script as written in my book (FOR UNIX):
use warnings;
#write to a file with a filehandle. Sriptname: file.handle
my $file="/home/jody/ellie/perl/newfile";
open(my $fh, ">", $file) || die "Can't open newfile: $!\n";
print $fh "hello world.\n";
print $fh "hello world again.\n";
At the command line
$perl file.handle
$cat newfile
the output should be looking like this:
hello world.
hello world again.
I made the following changes but with no success
use warnings;
#Write to a file with a filehandle. Scriptname: file.handle
my $file='C:\newfile.txt';
open (my $fh, ">", $file) || die "Can't open newfile: $!\n";
print $fh "hello world.\n";
print $fh "hello world again.\n";
When I run script I get the following output:
can't open newfile: permission denied**
When I run the script with debug I get the following information:
uncaught exception for user code
can't open newfile: permission denied
at handlingfiles.pl line 5
press any key to continue
What am i doing wrong?
As #choroba mentioned in a comment, C:\newfile.txt will [try to] write to the Windows root directory (e.g. C:\). So, you probably want just newfile.txt.
Cygwin: If you're using the perl that comes with cygwin, you can probably use /home/jody/newfile.txt as this perl supports the POSIX file syntax. If you installed cygwin at (e.g.) D:\cygwin, then the /home directory will end up in D:cygwin\home. Note you do ls /home to see what users have been defined.
Otherwise, if you want a full path, what is the full path for your .pl script. Obviously, you could write to that directory.
Side note: I've been writing perl for many years and on those rare occasions when I do use it on Windows, I vastly prefer using the cygwin version [that also has many scripts, tools, etc. and functions like a POSIX environment]. YMMV
Perl is asked to open the file for writing, so that is what it does. On Windows, normal users cannot write to the root directory of a drive, and Windows rejects its request. Try something like C:\Users\User\My Documents\newfile.txt.

Open filehandle not working under mod_perl ModPerl::PerlRun

I'm at my first attempt to use mod_perl. I'm totally new to it. I opted for ModPerl::PerlRun because I don't want to make any modification to the scripts I already have
I followed the instructions in Installing Apache2/Modperl on Ubuntu 12.04
I uploaded script.pl to /perl, and the script looks like it's running fine except for this
open(my $fh, '<:encoding(UTF-8)', 'page_template.htm') or die $!;
It won't open the file and dies with the message
No such file or directory at /var/www/perl/script.pl
Update
Note that the documentation for ModPerl::PerlRun has this to say
META: document that for now we don't chdir() into the script's dir, because it affects the whole process under threads.
so it is probably not workable to simply do a chdir in your program's code, and the second option below should be used
Original*
The current working directory of your CGI program isn't what you think. It is most likely to tbe the root directory /
You can either use chdir to set the working directory of the script
use File::Basename 'dirname';
chdir dirname(__FILE__);
or simply add the full path to the name of the file that you want to open, for instance
open my $fh, '<:encoding(UTF-8)', '/perl/page_template.htm' or die $!;
Note that you can't use FindBin, as your program is being run as a subroutine of Apache's main mod_perl process, so $FindBin::Bin will be equal to the directory of the Apache executable httpd and not of your own program file

Write data to a file using CGI-perl

I have a problem in writing data to a file using CGI. the code is as follows:
#!/usr/bin/perl
use strict;
use warnings;
use CGI ":standard";
print "Content-type:text/html \n\n";
open (FILE, '>', "file.txt") or die ("Could not open the file for writing...$!");
print FILE "something";
close(FILE);
The above code is giving the following error:
"Could not open the file for writing...Permission denied at /var/www/cgi-bin/wfile.cgi line 8.
The read and write permission for the file is already set... Please help!!!
The perl script will run as the user that apache/httpd/whatever is running as. You need to make sure that the directory and file is writeable by that user. Why not try writing to /tmp/ first.

How to create a Zip file on a remote server in perl

actualy im using Net::FTP::Recursive to download a directory structure , it works nice for what is required. But since some folders have more than 100/ files, downloading then can take ages. Since a zip file is faster do download, how i could, using perl connect to a remote server via ftp and create a zip file from the remote server/folder to download ?
use Net::Config;
use Net::FTP::Recursive;
$ftp = Net::FTP::Recursive->new("$hostname:$ftp_port", Debug => 0)
or die "Cannot connect to $hostname: $#";
$ftp->login($iLogin,$iPass)
or die "failed ", $ftp->message;
$ftp->binary()
or die "Cannot set to Binary";
$ftp->cwd("/admin/packages/$fileName")
or die "Cannot change working directory ", $ftp->message;
$ftp->rget( $fileName );
#or die "Download Failed ", $ftp->message;
$ftp->quit;
Thank you all for your time
The site(ARGS) method is designed for that. You can send a shell command and make it runs on remote server.
http://perldoc.perl.org/Net/FTP.html#METHODS
However, most of the FTP servers I know disabled that permission, so, good luck
I think you'd need to have SSH access to the system to run a ZIP command. But if that's the case, you could also use SCP to transfer your files more securely. FTP does everything in the open.
Thanks,
F.