How do I copy data from a remote system without using ssh or FTP Perl modules? - perl

I have to write a Perl script to automatically copy data from remote server to my local system. The directory structure on remote systems is:
../log/D1/<date>.tar.gz
../log/D2/<date>.gz
../log/D3/<date>.tar.gz
../log/D4/<date>
and same on other server. I want to copy the data on local system in below format.
../log/S1/D1/<date>.tar.gz
../log/S1/D2/<date>.gz
../log/S1/D3/<date>.tar.gz
../log/S1/D4/<date>
and same for other servers i.e. S2, S3, etc
Also, no ssh supported Perl modules are available on remote server as well on local server and I dont have permission to install any Perl modules. The only good thing is that the connectivity is through password-less ssh keys.
Can anyone please suggest me any Perl code to get this done?

I believe you can access to shell command from perl.
So you can do this:
$cmd = "/usr/bin/scp remotefile localfile";
system $cmd;
NOTE: scp is secure-copy -- a buddy of ssh.
This does not require ssh-perl module but it require ssh support on both (which I have).
Hope this helps.

I started to suggest the scp command line program, but it seems that there's a CPAN module for that (no surprise). Check out Net::SCP.
By using scp on your client (where you can install new Perl modules) you can copy files without having to install any new software on the remote system. It just needs to have the ssh server running - which you've said it does.

I'd say stop trying to make life difficult for yourself and get the system to support the features you require.
Trying to develop for such a limited/ locked down platform is not going to be cost-effective in the long run - you'll develop stuff more slowly and it will have more bugs.
A little developer time is way more expensive than a decent hosted VM / hardware box.
Get a proper host, it will definitely save money (talk to your manager about this).

From your query above I understand that you don't have much permissions to install perl modules or do any changes which require administrative privileges. I love perl but to automate things like this you should use bash instead of perl. Below is the sample code I am using with password less ssh keys.
#!/bin/bash
DATE=`date`
BASEDIR="/basedir"
cd $BASEDIR
for HOST in S1 S2 S3
do
scp -q $HOST:$BASEDIR/D1/$DATE.tar.gz $HOST/D1/
echo "Data copy from $HOST done"
done
exit 0
You can use different date formats like date +%Y%m%d for current date in format YYYYMMDD. Also you can use this link to learn different date formats.
Hope this helps.

You may not be able to install anything in system-wide lib directories, but there is nothing preventing you from installing modules in a location to which you have write-access. See How do I keep my own module/library directory?
This creates no more of a security issue than allowing you to write scripts on this system in the first place.
So, go forth and install Net::SCP.

It sounds like you want rsync. You shouldn't have to do any programming at all.

Related

How to send a file using scp from a perl script using only ftp hostname, login and password, WITHOUT ppk file

Hoe to send a file using scp from a perl script using only ftp hostname, login and password, WITHOUT ppk file
This is what I use when I do have ppk file.
open scp://username#hostname -privatekey="\path\to\ppkfile\ppkfile.ppk "
put filename.csv /home/destination_flder
exit
Thank you!
one of the possible solution is that u can configure destination machine for passwordless ssh and then
use the following command to transfer file or copy
scp $source_filepath username#machinename:$destination_filepath
Suic's recommendation is very good, looks like exactly what you're looking for. Personally I haven't used that particular module, though.
I've had good success with Net::SFTP::Foreign, which is a perl wrapper for SFTP client. It supports password-based login in most situations (see documentation for details). In my experience SFTP is usually available whenever SCP is and gives greater level of control.

Create scripts that run in different servers

I have three servers that are used to manage a bunch of other client servers. One of the managing servers has nagios, the other has a web proxy, another has an ldap and MySQL server.
Whenever I need to include a new client server, I have to log into Server A, and create the SQL entry, go to nagios and create the entry, go to the web server and add the proxy. You get the picture. What I would like is to be able have all servers share a scripts directory, say '/opt/boxes/scripts` and in there have a bunch of scripts that know where they can run. Say I'm in server A and run script X, that should run on server B, it will actually run in server B.
Is there a simple way to do this? Preferably perl bases since that is something i know a little bit about.
One easy way may be to make a directory for each script on each of the machines.
In one directory, the actual script runs.
In all the other directories, the script does a ssh to the appropriate server and runs the actual script.
E.g.
Script to add client
ssh servera -c servera-addclient-to-sql
ssh serverb -c serverb-addclient-to-nagios
ssh serverc -c serverc-addclient-to-webproxy
Adding scripts that know where they run is fairly easy too.
In loose form
$RunWhere = "XXXX"
$ScriptName = "YYYY"
if `hostname` ne "XXXX" {ssh $RunWhere -c $Scriptname #ARGS ; exit; }
else {
Do the actual stuff
}
So run from anywhere, it runs locally if thats the right thing to do, otherwise does a secure shell to the right place and runs the same command there.

Powershell script to copy files to remote server

I'd like to have files on a local machine and copy them to a remote machine on the internet. For this reason, I can't use UNC files shares. I'd also like to avoid using MSDeploy or FTP if possible. Does powershell have an easy way to copy a bunch of files to a remote server?
Look at BitsTransfer module, might help you - http://technet.microsoft.com/en-us/library/dd819420.aspx
When the remote server is a Linux or UNIX system, I use PSCP.EXE, the Windows SCP client created by the developer of Putty. I also use Puttygen to create a key pair that can be used instead of interactive password authentication.

Is NET::SCP uses multiple connections to transfer multiple files?

HI,
Actually i am trying to reduce the time taken to transfer N-number of files from remote machine local machine using secure file transfer, previously i used scp system command it establishes a connection for each file transfer.
Thanks in advance.
Unless you have a bandwidth cap on each individual TCP connection, you are not going to get a significant reduction in download time by using multiple SCP connections.
You can check whether you get a speed up by putting a separate scp command for each file in a shell script and timing the script. Then rerun the shell script with & at the end of each scp line. If this speeds up the transfer and you want to really do this in Perl, look into fork or Parallel::ForkManager.
I think this would create a separate connection each time. However, scp with the -r flag (which Net::SCP uses) recursively copies all of the files in a directory with a single connection. That might be the way to go, if you have your files in a few directories and you want to copy all of the files in those directories.
Otherwise, rsync with the --files-from option should use only one connection. (Don't forget -z for compression, or -a).
If the only reason you're considering using perl to do this is that you want a single session, then just use the command line rsync to get this effect (with --files-from). If you want perl power to generate the list of files-from, File::Rsync supports that.

Portable PostgreSQL for development off a usb drive

In order to take some development work home I have to be able to run a PostgreSQL database.
I don't want to install anything on the machine at home. Everything should run off the usb drive.
What development tools do you carry on your USB drive?
That question covers pretty much everything else, but I have yet to find a guide to getting postgresql portable. It doesn't seem easy if it's even possible.
So how do I get PostgreSQL portable? Is it even possible?
EDIT:
PostgreSQL Portable works. It's very slow on the usb-drive I have, but it works. I can't recommend doing constant development with it but for what I need it's great.
Perhaps if I pick up a full speed external drive I'll try out virtualization. Given the poor performance of just running the database off this drive, a full virtual OS running off of it would be unusable.
Here's how you can do this on your own:
http://www.postgresonline.com/journal/archives/172-Starting-PostgreSQL-in-windows-without-install.html
An alternate route would be to use something like VirtualBox and just install your development environment (database, whatever) on there.
There are 2 projects to try in 2014: http://sourceforge.net/projects/pgsqlportable/ and http://sourceforge.net/projects/postgresqlportable/?source=recommended.
I can't vouch for the second, but I'm using the first and it works right out of the box.
After unzipping using 7-zip (http://www.7-zip.org/download.html):
1) Run "start service without usuario.bat" ( english translation )
2) Then run "pgadmin3.bat"
The only minimal problem for me was that its in spanish. I've been able to change the language to english by following Change language of system and error messages in PostgreSQL. Using google translate the instructions are:
Description
This is a zip to automatically run postgresql 9.1.0.1 for windows. This version already has pgagent and pldebugger. To run must: 1) unzip
the zip 2) run the "start service without usuario.bat" found in the
pgsql directory within the folder you just unzipped. 3) Optional. If
you want to run the agent works postgresql (pgagent) should only run
the "start pgagent.bat" found in the pgsql directory inside the folder
you just unzipped. 4) Optional. To manage and / or develop the bd you
can run the pgadmin3.bat 5 files) Optional. To stop and / or restart
the server correctly use file "service without stopping usuario.bat"
usuario.bat or restart service without depending on the case.
Now option for Linux (file. Tar.gz). Postgresql portable Linux 9.2
Please use the tickets for your answer bugs.
Username: postgres Password: 123
Just a Note : on a new computer , to get pgadminIII working you may need to add a db. The settings are in attached screenshot.
Hope it helps.
I agree with virtualization solution, but maybe you can find useful this link from portable freeware collection, I have used this locally, not from usb though
1.download and extract : zip version
2.inside pgsql folder create data folder(put any name,I used 'data')
3.initalize data folder: c:\pgsql\bin\initdb.exe -D c:\pgsql\data -U postgres -W -E UTF8 -A scram-sha-256
4.to start/stop see next cmd code that I use (press any key inside it to stop)
c:\pgsql\bin\pg_ctl.exe -D c:\pgsql\data -l logfile start
pause
c:\pgsql\bin\pg_ctl.exe -D c:\pgsql\data stop
more info