FTP client in BASH, Perl, Python

To compare script language for administration tasks.
- connect to FTP server in passive mode
- delete file
- send file in binary mode

BASH

$ cat ./ftp.sh
#!/bin/bash
HOST="phn.org.ua"
USER="phn"
PASSWD="PASSWORD"
FILE="phn.log"
ftp -p -n -v $HOST << EOT
bin
user $USER $PASSWD
prompt
delete $FILE
put $FILE
quit
EOT

Perl

$ cat ./ftp.pl
#!/usr/bin/perl
use Net::FTP;
use File::Basename;
$ftp;
$host="phn.org.ua";
$user="phn";
$pw="PASSWORD";
$file="phn.log";
$ftp=Net::FTP->new($host, Passive=>1) or die "could not connect";
$ftp->login($user,$pw) or die "could not login";
$ftp->binary(); # set binary mode
$ftp->delete($file);
$ftp->put($file) or die "could not put $file";
$ftp->quit();

Python

$ cat ./ftp.py
#!/usr/bin/python
import ftplib
host="phn.org.ua";
user="phn";
pw="PASSWORD";
file="phn.log";
s = ftplib.FTP(host,user,pw) # Connect
s.set_pasv(1)
s.delete(file) # delete the file
f = open(file, "rb") # file to send
s.storbinary("STOR phn.log", f) # Send the file
f.close() # Close file and FTP
s.quit()

Leave a Reply

Your email address will not be published. Required fields are marked *