Categories

Category Specific RSS

Random password generator in PERL

Usage: perl script.pl length

 

#!/usr/bin/perl
# Script: Random password generator
# Fri 03 Dec 2010 01:30:24 PM CET
#use strict;

# List of chars. Modify this if you need to add more, like simbols, etc…
my @chars = qw( a b c d e f g h i j k l m n o p q r s t
u v w x y z A B C D E F G H I J K L M N
O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7
8 9 );

# The array of the password.
my @password = (“”);

# Select a random char from the array @chars and introduced into the array @password.
sub pass_gen {
my $rand = int(rand(@chars));
my $add_char = $chars[$rand];
push(@password, $add_char);
}

# The length of the password. If none is supplied externally, given the value of 8.
my $length = shift || 8;

# Call the function pass_gen X (the value stored in $lenght) times.
for (my $x = 1; $x <= $length; $x++) {
&pass_gen();
}

# Print the password without spaces.
print “Here is your password:\n”;
print @password = grep { “$_” } @password, “\n”;

#End of file

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

list all files in directory with PERL

opendir(IMD, $dirtoget) || die(“Cannot open directory”);
@thefiles= readdir(IMD);
closedir(IMD);

print “Content-type: text/html\n\n”;
print “<html><body>”;

foreach $f (@thefiles)
{
unless ( ($f eq “.”) || ($f eq “..”) )
{
print “$f<br />”;
}
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Validate email in PERL

use Regexp::Common qw[Email::Address];
use Email::Address;

while (<>) {
my @found = /($RE{Email}{Address})/g;
my @addrs = map $_->address, Email::Address->parse(“@found”);

print “X-Addresses: “, join(“, “, @addrs), “\n”;
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Unzipping zip file in Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {

public static void main(String[] args) throws IOException {
ZipFile zf = new ZipFile(args[0]);
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
System.out.println(“Unzipping ” + ze.getName());
FileOutputStream fout = new FileOutputStream(ze.getName());
InputStream in = zf.getInputStream(ze);
for (int c = in.read(); c != -1; c = in.read()) {
fout.write(c);
}
in.close();
fout.close();
}
}
}

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare

Check a file exists in Python

import os

def file_exists(file_name):
if os.path.exists(file):
return ‘%s is found’ % file_name
else:
return ‘%s is missing’ % file_name

FacebookTwitterGoogle+PinterestTumblrStumbleUponRedditLinkedInBaiduApp.netBufferDeliciousDiggDiigoWordPressBlogger PostEmailPrintFriendlyShare