Quick FTP file/directory upload script
Sorry for not posting for a while, school's keeping me busy.
I wrote a small Perl script today to upload a file or a directory to FTP server by command line. Usually I use FileZilla but sometimes you just need something small uploaded fast and don't want to run those resource wasting and slow GUI applications. With my script you just have to pop-up Terminal and type "./up2ftp.pl myimage.png" for example and the file gets uploaded and link to the file gets printed to you.
I also started learning some C#. I'm not much of a Microsoft guy but .Net is getting quite popular.
So here's the FTP upload script:
#!/usr/bin/perl
# up2ftp.pl
# Tue Apr 13 15:09:02 2010
# Copyright 2010 jolttz
# jolttz{ät}gmail{dot}com
#
use strict;
use File::Basename;
use Net::FTP;
# Configuration
my $ftp_adr = "loder.pri.ee"; # FTP address
my $ftp_usr = "username"; # FTP username
my $ftp_pwd = "password"; # Password
my $http_dir = "/public_html"; # Public directory
my $up_dir = "/uploads"; # Upload directory
# Config end
# If $link doesn't get a new value it displays this
my $link = "Error generating link!";
# Connect to host
my $ftp = Net::FTP->new($ftp_adr, Debug => 1)
or die "Can't connect to hostname: $@";
# Identify
$ftp->login($ftp_usr, $ftp_pwd)
or die "Can't login ", $ftp->message;
# Change directory to upload directory
$ftp->cwd($http_dir . $up_dir)
or die "Can't change working directory ", $ftp->message;
# Set binary mode or uploaded images can't be displayed
$ftp->binary();
if ($ARGV[0] eq "-R") {
my $dir = $ARGV[1];
my @dirs = split(/\//, $dir);
my $newdir = @dirs[@dirs-1];
$ftp->mkdir($newdir); # Make new directory
$ftp->cwd($newdir);
# Open local directory to upload
opendir (DIR, $dir) || die "Error in opening dir $dir\n";
while (my $file = readdir(DIR)) {
# If exist upload every file in dir.
if (!-d $file) {
$ftp->put("$dir/$file");
}
}
closedir(DIR);
# Generate link
$link = "http://" . $ftp_adr . $up_dir . "/" . $newdir;
} else {
my $file = $ARGV[0];
my @dirs = split(/\//, $file);
my $filename = @dirs[@dirs-1];
if (!-d $file) {
$ftp->put($file);
$link = "http://" . $ftp_adr . $up_dir . "/" . $filename;
}
}
$ftp->quit();
print "\n" . $link . "\n";
Directory upload syntax:
jolttz@archbox ~ $ perl up2ftp.pl -R /dir/to\ the/dir
File upload syntax:
jolttz@archbox ~ $ perl up2ftp.pl myfile
Convert all AVI’s from a directory to MP4 using ffmpeg and Perl
I recently bought a new iPod (nano 5G) and I couldn't find a script that converts multiple AVI's from a directory to MP4 quickly so I wrote one myself. It uses ffmpeg to convert so you need to have it installed. 170MB AVI should be like 80MB as MP4.
#!/usr/bin/perl
# avi2mp4.pl
# Sun Feb 12 22:41:46 2010
# Copyright 2010 jolttz
# jolttz{ät}gmail{dot}com
#
use strict;
my $dirname = $ARGV[$0];
my ($width, $height, $size);
my $bitrate = 409600;
opendir(DIR, $dirname) or die "Can't open directory $dirname: $!";
while (defined(my $file = readdir(DIR))) {
if ($file =~ m/(.+)\.avi/i) {
# Get the aspect ratio
open(P, "ffmpeg -i '$dirname/$file' 2>&1 |") or die "$!\n";
while(
) {
if (/Video:.+ (\d+)x(\d+)/) {
$width = $1;
$height = $2;
}
}
close(P);
die "Failed finding the aspect ratio.\n" unless defined $width;
my $aspect = $width/$height;
if (abs($aspect - 16/9) < 0.02) {
$size = "320x180";
} elsif (abs($aspect - 4/3) < 0.02) {
$size = "320x240";
} else {
die "Weird aspect ratio: ${width}x${height} = $aspect\n";
}
print $size;
print "Converting '$dirname/$file'...\n";
exec("ffmpeg", "-i", $dirname. "/" .$file, "-b", $bitrate, "-s", $size, "-vcodec", "mpeg4", "-ab", 128, "-acodec", "libfaac", "-r", 25, $dirname. "/" .$1. ".mp4");
}
}
closedir(DIR);
Syntax:
b22stard@b22s-desktop ~ $ perl avi2mp4.pl /dir/to/the/avis/
Enjoy
Simple login script using MD5 encrypted password (Perl)
I was bored today so I decided to write a login script in Perl. It isn't very secure and useful yet because the file where the MD5 encrypted password will be saved can be deleted and then it'll ask for a new password. This can easily be solved by removing the "check if file exists" part and adding a file with MD5 encrypted password yourself. MD5 can be cracked so choose long passwords with numbers (6-7 numbers and letters should be fine).
#!/usr/bin/perl
# md5login.pl
# Fri Jan 22 23:08:10 2010
# Copyright 2010 jolttz
# jolttz{ät}gmail{dot}com
#
# Not very secure yet because the password file
# can be deleted and then it'll ask for a new password.
# You may need to modify a bit to run on Windows
use strict;
use Switch;
use Digest::MD5 qw(md5_hex);
if (-e ".md5.txt") { # Check if password file exists
&askpw();
} else { # If doesn't exist set a new password
&setpw();
}
sub askpw {
print "Enter password: ";
chomp(my $input = <STDIN>); # Take input
my $md5 = md5_hex($input); # Crypt input
open FILE, "<.md5.txt" or die $!; # Open password file
chomp(my $md5pw = <FILE>); # Read password file
if ($md5pw == $md5) { # Check if the md5s match
system("clear");
print "Password accepted!\n\n";
&menu();
} else {
print "Invalid password!\n";
}
}
sub setpw {
print "Choose password: ";
chomp(my $input = <STDIN>);
my $md5 = md5_hex($input);
open FILE, ">>.md5.txt" or die $!;
# Print password to file in md5
print FILE $md5;
close (FILE);
system("clear");
&askpw();
}
sub menu {
print "Choose:\n";
print " 1) to change password\n";
print " 2) to exit\n";
chomp(my $choice = <STDIN>);
switch ($choice) {
case 1 { &setpw(); }
case 2 { exit; }
}
}