create – compile perl script

1. If required install perl?

sudo apt-get install perl

This should not be required in backtrack.

2. Open gedit and copy in the below

# A simple perl program to print the user input
print (“Hello, world\n”);

3. Save as

4. Open terminal and change directory to find

chmod +x

In backtrack looks like bellow

root@bt:~# chmod +x (press enter)

5. To run the program ./ (press enter)

root@bt:~# ./
Hello, world

meaning behind each line!

# Says this line is a comment and there are no executable instructions on this line

! Says this is a perl script
usr/bin/perl give the location of the perl interpreter, many programing books mention the location as usr/local/bin/perl, but this is not correct in ubuntu. In ubuntu Perl interpreter is located at usr/bin/perl.

print “Hello, type in something\n”;
This line just prompts to user to type something, similar to printf statement in C.

The chmod command (abbreviated from change mode) is a Unix command that lets a user tell the system how much (or little) access it should permit to a file

chmod +x  =     execute     execute a file or recurse a directory tree

2 thoughts on “create – compile perl script

  1. Perl-How To Automatically Create User Accounts in Unix ( Linux )

    The simplest way to automatically add a new user to your system is to do run a Perl script like this:

    use strict;
    use warnings;
    use Fcntl ‘:flock’; # import LOCK_* constants

    # The file we are going to change (Testing on Back Track 5R2)

    my $pw_file = “/etc/passwd”;
    my $group_file = “/etc/group”;
    my $shadow_file = “/etc/shadow”;

    # Login name
    my $login;    # Login name
    print “Login: “;
    $login = ;

    if ($login !~ /[A-Z_a-z0-9]+/) {
    die(“No login specified”);

    open PW_FILE, “<$pw_file" or die("Could not read $pw_file");
    # Lock the file for the duration of the program
    flock PW_FILE, LOCK_EX;

    # Check login info.
    my $check_uid = getpwnam($login);
    if (defined($check_uid)) {
    print "$login already exists\n";
    exit (8);

    # Find the highest UID.  We'll be that +1
    my @pw_info = ;

    my $uid = 0;    # UID for the user

    # Find biggest user
    foreach my $cur_pw (@pw_info) {
    my @fields = split /:/, $cur_pw;
    if ($fields[2] > 60000) {
    if ($fields[2] > $uid) {
    $uid = $fields[2];

    # Each user get his own group.
    my $gid = $uid;

    # Default home dir.
    my $home_dir = “/home/$login”;

    print “Full Name: “; # Get user full name
    my $full_name = ;

    my $shell = “”;    # Get user shell to use
    while (! -f $shell) {
    print “Shell: “;
    $shell = ;

    print “Setting up account for: $login [$full_name]\n”;

    open PW_FILE, “>>$pw_file” or
    die(“Could not append to $pw_file”);
    print PW_FILE

    open GROUP_FILE, “>>$group_file” or
    die(“Could not append to $group_file”);
    print GROUP_FILE “${login}:x:${gid}:$login\n”;
    close GROUP_FILE;

    open SHADOW, “>>$shadow_file” or
    die(“Could not append to $shadow_file”);
    print SHADOW “${login}:*:11647:0:99999:7:::\n”;
    close SHADOW;

    # Create the home directory
    chmod(0755, $home_dir);
    system(“cp -R /etc/skel/.[a-zA-Z]* $home_dir”);
    system(“find $home_dir -print “.
    “-exec chown ${login}:${login} {} \\;”);

    # Set the password for the user
    print “Setting password\n”;
    system(“passwd $login”);


    How It Work:

    Lock the /etc/passwd file
    Get the user name
    Lock the password file
    Make sure the user doesn’t exist
    Genarate a user ID for the new user
    Create an entry in /etc/passwd
    Create an entry in /etc/shadow
    Create an entry in /etc/groups
    Create the user home directory
    Set the initial password for new user
    Unlock the /etc/passwd file

    Source :

    Perl for System Administration.

    You can see here –

    I forgot added !
    This is very important.
    Before runing this script – please create your copy of this file:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s