#!/usr/bin/perl # Very Fast FTP-PUT, Auto-FTP BETA (Copy/Paste FTP) : raimo 20060807 -> 20070328, look *X # License, CC Public Domain http://creativecommons.org/licenses/publicdomain/ # ## !! libnet-perl required (and/or libwww-perl) # # INSTALL: # sudo apt-get install libnet-perl # wget -O ~/auto-ftp.txt http://dash.atspace.org/sec/auto-ftp.txt # set SETTINGS below, look *1: nano ~/auto-ftp.txt # sudo cp -i ~/auto-ftp.txt /usr/bin/auto-ftp # sudo chmod 755 /usr/bin/auto-ftp # # RUN FIRST THESE 3 COMMANDS (directories and a new profile will be created): # auto-ftp --help # auto-ftp --install # auto-ftp --new # # Profile files (named as PROFILENAME-X) lives in ~/.auto-ftp directory # USE: auto-ftp --new OR auto-ftp --edit TO create/edit profiles # Format of profile files are (6 lines, remove #): # $begin # ftp.server1.moon # remote/path/is/this # username # password # $end # # RUN: # auto-ftp -f FILENAME.EXT -p PROFILENAME-X # where FILENAME file (file1.txt) is in current directory (PWD) or /full/path/to/file1.txt # Fast mode: auto-ftp -f FILENAME.EXT -p PROFILENAME-X --silent --auto # run: auto-ftp --help # look example commands bottom of this file (*0) use strict; use Net::FTP; use File::Copy; sub loki; sub help; sub edit; # --- SETTINGS --- # *1 set here or in ~/.auto-ftprc PERL file our $auto = 0; # 0/1 confirm before transfer ON/OFF (-a) our $logging = 1; # 0/1 create log ($log) ON/OFF (--nolog) our $passive = 1; # 0/1 passive mode, try to keep this as 1 (--passive) our $binary = 1; # 0/1 binary transfer-mode, 1 is good (-b) our $debug = 0; # 0/1 not so silent mode, turn to 1 if needed (-s) our $timeout = 30; # timeout, default is 60 seconds (--timeout XX) our $message = 2; # terminal info level, default is 2 # -----------------# # --- DO NOT EDIT BELOW OF THIS LINE --- # my $home = $ENV{"HOME"}; my $pwd = $ENV{"PWD"}; my $edit = $ENV{"EDITOR"} if -x $ENV{"EDITOR"}; $edit = "nano" if !$edit; my $out = "$home/ftp-out"; # transfer directory my $dir = "$home/.auto-ftp"; # profile directory my $log = "$home/auto-ftp.log"; # log file my ($profile, $file, $subd, $newname); my $name = $0; $name =~ s/^.*\///; my $ver = 20070328; my $url = "http://dash.atspace.org/sec/auto-ftp.txt"; my $lic = "http://creativecommons.org/licenses/publicdomain/"; # "OUR -marked" settings can be in ~/.auto-ftprc PERL file too do "$home/.auto-ftprc" if -s "$home/.auto-ftprc" > 10; # removing gPad message file unlink "$home/.auto-ftp.msg" if -f "$home/.auto-ftp.msg"; # autoinstaller ;) mkdir $out if !-d $out; mkdir $dir if !-d $dir; exit print "\nOK\n" if -d $dir && -d $out && (grep/^--install$/, @ARGV); exit help if (grep/^-h$|^--help$/, @ARGV) || !@ARGV; # show help exit print "$name [$0] version: $ver\n" if (grep/^-v$|^--version$/, @ARGV); # show version exit print `cat $log` if grep/^-l$|^--log$/, @ARGV; # show log file exit unlink $log if grep/^--clearlog$/, @ARGV; # remove log file exit print `ls -m1 $dir` if grep/^--list$/, @ARGV; # list profiles edit if grep/^--edit$|^--new$/,@ARGV; # create/edit profile die "\n$! $out or $dir" if !-d $out || !-d $dir; die "\nDir can't be same $dir $out" if $dir eq $out; my $o; while (<@ARGV>){ # params $profile = $_ if $o eq "-p"; # -p PROFILENAME $file = $_ if $o eq "-f"; # -f [PATH/]FILENAME $subd = $_ if $o eq "-d"; # -d add remote SUB/DIR $timeout = $_ if $o eq "--timeout" && $_ =~ m/^\d{2}$/; # --timeout XX (10-99) $newname = $_ if $o eq "--newname" && length $_ > 0;# --newname NEW-FILENAME $o = $_; } $auto = !$auto if grep/^-a$|^--auto$/, @ARGV; # testing *3 $debug = !$debug if grep/^-s$|^--silent$/, @ARGV; # testing *3 $binary = 0 if grep/^-t$|^--text$/, @ARGV; # testing *3 $binary = 1 if grep/^-b$|^--binary$/, @ARGV; # testing *3 $passive = 0 if grep/^--active$/, @ARGV; # testing *3 $passive = 1 if grep/^--passive$/, @ARGV; # testing *3 $logging = 0 if grep/^-nl$|^--nolog$/, @ARGV; if (grep/^--force-auto$/, @ARGV){ # *9 $auto = 1; $debug = 0; } # building filename $file = "$pwd/$file" if !-f $file; # *4 die "\n$! $file" if !-f "$file"; # *4 my $filename = $file; $filename =~ s/^.*\///; $filename = $newname if $newname; # read profile open FILE, "< $dir/$profile" or die "Profile: $profile not found, try $name --list"; my @prof = ; close FILE; # checking profile file format somehow exit print "Wrong format in profile file: $dir/$profile\n", "run: $name --help and $name --edit\n" if (int @prof < 6) || (int @prof > 7) || $prof[0] ne "\$begin\n" || ($prof[5] ne "\$end" && $prof[5] ne "\$end\n"); chomp(my $server = $prof[1]); # server address chomp(my $path = $prof[2]); # remotepath chomp(my $user = $prof[3]); # username chomp(my $pssw = $prof[4]); # password # some kind of crypt needed here... exit print "Wrong settings in profile: $profile\n", "Do not use / end of path-string\n", "run: $name --help and $name --edit\n" if (length $server < 5 || !$path || !$user || !$pssw || ($path =~ m/^.*\/$/ && $path ne "/")); # no / in end of path, fixed 0328.2 # copy FILE to output-dir `touch $home/.auto-ftp.lock`; # *14 added mark for gPad auto-ftp etc. copy "$file", "$out/$filename"; # *4 exit print "Error in copy, $file -> $out/$filename" if -s "$file" != -s "$out/$filename"; # transfer file print "Passive = $passive (--passive --active)\n", "Binary = $binary (--binary --text)\n", "Debug = $debug (--silent)\n", "Logging = $logging (--nolog)\n\n" if $message > 1; print " Connecting -> $server\n", " Remote-path -> $path\n", " Original file -> $file\n", " Filename -> $filename\n" if $message > 0; if ($auto == 0){ # query if not in auto-mode print "\nTransfer file? (Cancel = Ctrl+C) "; my $ask = ; } # ADDED FOR SECURITY 20061231 die "Not secure path: $file\n" if grep/$dir/, $file; loki "-> $server, $path"; # connecting now.. my $ftp = Net::FTP -> new($server, Debug => $debug, Timeout => $timeout, Passive => $passive) or die "Can't connect to $server: $@"; # ok, login $ftp -> login($user, $pssw) or die "Login error ", $ftp -> message; print $ftp -> message; loki $ftp -> message; # change remote directory if ($path ne "/"){ # fix 20070328.2 $ftp -> cwd("$path$subd") or die "Remote-dir $path$subd not found ", $ftp -> message; } print $ftp -> message; loki $ftp -> message; $ftp -> binary if $binary == 1; $ftp -> binary if -B $file; # force binary-mode to binary files # transfer file $ftp -> put("$out/$filename"); # check if: or loki $ftp -> message; needed here? my $mem = $ftp -> message; # message memory print "$mem\n"; loki $mem; # disconnecting.. $ftp -> quit; print $ftp -> message; loki $ftp -> message; # message file for gPad etc. open FILE, "> $home/.auto-ftp.msg"; flock FILE, 2; print FILE "Auto-FTP: $file -> $profile\n"; print FILE "$mem"; print FILE $ftp -> message; close FILE; # removing file form $out unlink "$out/$filename" if -f "$out/$filename" && (!grep/^--keep$/, @ARGV); unlink "$home/.auto-ftp.lock" if -f "$home/.auto-ftp.lock"; # *14 mark for gPad etc exit 0; sub help{ my $msg = shift; $msg = "$msg\n" if $msg; print < 0 (def: $logging) --keep = leave file to output dir after transfer --newname X = change remote file name to new name X --timeout XX = set timeout, 10 - 99 seconds --binary = binary-mode ON (def $binary, try to keep as 1 --text = binary-mode OFF (DO NOT USE WITH IMAGES etc) --passive = passive-mode ON (def: $passive) --active = passive-mode OFF --log = show log --clearlog = remove log file\n Profile dir: $dir Output dir: $out Log file: $log\n Profile file format (6 lines): \$begin ftp.server1.moon remote/path/is/this username password \$end\n $url $lic\n HELP } sub edit{ print "Auto-FTP profiles:\n", `ls -m1 $dir`, "\n"; print "Type profilename to create/edit: "; my $resp = ; exec("$edit $dir/$resp"); exit 0; } sub loki{ return if $logging < 1; chomp(my $msg = shift); my $time = localtime(time); open FILE, ">> $log" or print "$! Can't access to $log\n"; flock FILE, 2; print FILE "$time $msg\n"; close FILE; } # dev: # sudo cp ~/d2/auto-ftp.txt /usr/bin/auto-ftp # remarks *X # 20070328, fixed rootdir (/) -case? # 20070105, added message file for clients as gPad etc # 20061231, added lock-file for clients as gPad etc # 20061230, added --force-auto for gPad etc. *9 # 20061228, file can now be anywhere, not only in PWD *4 # 20061227, added --edit option for profiles, testing *3, seems OK.. # 20061226, added $subd and -d param for a SUBPATH -> PROFILEPATH adding # my copy/paste-command to transfer THIS file to server: *0 # auto-ftp -f auto-ftp.txt -p dash-sec --silent --auto # or my original dev-file: # auto-ftp -f ~/d2/auto-ftp.pl -p dash -d /sec --newname auto-ftp.txt --force-auto # license: http://creativecommons.org/licenses/publicdomain/