Operating System - HP-UX
1822001 Members
3920 Online
109639 Solutions
New Discussion юеВ

Perl script for FTP file transfer...

 
Dodo_5
Frequent Advisor

Perl script for FTP file transfer...

i have some files in a folder "my folder" in machine 1.i want to transfer the files from that folder to a different machine2 through ftp connection.and after finishing the transfer of files from machine1 to machine2,the files should be kept in a different folder called "backup_machine1" in the same directory of machine1 where "my folder" is...for that ftp connection there are username and password also....

so i need a perl/unix script to perform this task.....can you help me doing that???
8 REPLIES 8
gstonian
Trusted Contributor

Re: Perl script for FTP file transfer...

Unix Script:

ftp -vin <open
quote USER
quote PASS
put
quit
END_SCRIPT
) | grep "226 Transfer complete." > /tmp/ftp$SRCFILE

if [[ -s /tmp/ftp$SRCFILE ]]
then
echo "FTP Completed Sucessfully"
mv
EXTSTA=0
else
echo "FTP Failed"
EXTSTA=1
fi
Dodo_5
Frequent Advisor

Re: Perl script for FTP file transfer...

can anyone put a perl script for this???
actually i need this in perl...
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script for FTP file transfer...

The tricky part is the ftp transfer; the local move is easy. I assume this is a UNIX machine and there is no such thing as a folder on a UNIX box. There are directories so I assume that this is what you meant. Since you have whitespace (which is legal but dumb) embedded in your pathnames, take care to quote everything.

-------------------------------------------
#!/usr/bin/sh

typeset SRCDIR="/aaa/bbb/my folder"
typeset DESTDIR="/xxx/yyy/his folder"
typeset BACKUP_DIR="backup_machine1"

typeset REMHOST="machB"
typeset USER="mickey"
typeset PASSWD="secret"

typeset -i STAT=0

cd "${SRCDIR}"
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "Can't cd to ${SRCDIR}" >&2
exit ${STAT}
fi
ftpput.pl -h ${REMHOST} -l ${USER} -p ${PASSWD} -d "${DESTDIR}" -B *
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
mv * "${BACKUP_DIR}/"
STAT=${?}
echo "mv failed; status ${STAT}." >&2
else
echo "FTP failed; status ${STAT}." >&2
fi
exit ${STAT}
-------------------------------------------

If you create a .netrc file you will not need to pass the password parameter.

The perl script ftpput.pl is attached; invoke as "ftpput.pl -u" for full usage. The Perl script does full error checking.
If it ain't broke, I can fix that.
Dodo_5
Frequent Advisor

Re: Perl script for FTP file transfer...

actually in the machine i am using windows and through putty trying to connect other unix box..
i want to transfer files from a folder ,ie,"c:/my folder/my files" in my machine to another one through ftp and after transferring i want to take a backup of the file in "c:/backup/my backup".....like this only...
gstonian
Trusted Contributor

Re: Perl script for FTP file transfer...


use Net::FTP;

$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $@";

$ftp->login("anonymous",'-anonymous@')
or die "Cannot login ", $ftp->message;

$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;

$ftp->put("that.file")
or die "get failed ", $ftp->message;

$ftp->quit;

You should then be able to move files about with

$curlocation = "file1.txt";
$newlocation = "saved/filebackup.txt";
move($oldlocation, $newlocation);


Dodo_5
Frequent Advisor

Re: Perl script for FTP file transfer...

can you put comments (givin description )on your code..then i can understand it properly...where to give the remote machine address for ftp trasnsfer or password for connection??? i can't understand in your code...
Hein van den Heuvel
Honored Contributor

Re: Perl script for FTP file transfer...

That code is truly self explanatory
(and straight from the help text :-)

Read the example code carefully again.
If you can not figure out where a variable with the host name should go, then you should find help performing this particular job, or consider setting up a samba or nfs share to allow you to drag and drop the files.

Check the help page for the perl ftp module: google: ftp perl

Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl script for FTP file transfer...

I am having a hard time understanding why you would ask a Windows question in a UNIX forum. You have been given more than enough Perl code to do this. The Perl script I attached will work equally well in Windows or UNIX and it should be a trivial matter to change the tiny bit of shell to a .bat file or you can install Microsoft Services for Unix (SFU); it's free and you then have a real shell on your Windows box. In any event, after running the Perl script I gave you the only remaining task is moving the local files and any child on the streets of Starkville could do that in a batch file.
If it ain't broke, I can fix that.