Archiwum

Archive for the ‘Perl’ Category

How To Completely Remove User Account In Unix (Linux)

Czerwiec 28, 2012 Dodaj komentarz

How do I remove a user’s access from my server? How do I delete a user account under Linux operating systems?

You need to use the userdel command to delete a user account and related files from user account or use this perl script:

/#!/usr/bin/perl
use strict;
use warnings;
use Fcntl ':flock'; # import LOCK_* constants

if ($#ARGV != 0) {
print STDERR "Usage is $0 <user>\n";
exit (8);
}

my $user = $ARGV[0];

sub edit_file($)
{
my $file = shift;

open IN_FILE, "<$file" or
die("Could not open $file for input");

open OUT_FILE, ">$file.new" or
die("Could not open $file.new for output");

while (1) {
my $line = <IN_FILE>;
if (not defined($line)) {
last;
}
if ($line =~ /^$user/) {
next;
}
print OUT_FILE $line;
}
close (IN_FILE);
close (OUT_FILE);
unlink("$file.bak");
rename("$file", "$file.bak");
rename("$file.new", $file);
}

my @info = getpwnam($user);
if (@info == -1) {
die("No such user $user");
}

open PW_FILE, "</etc/passwd" or
die("Could not read /etc/passwd");

# Lock the file for the duration of the program
flock PW_FILE, LOCK_EX;

edit_file("/etc/group");
edit_file("/etc/shadow");

if ($info[7] eq "/home/$user") {
system("rm -rf /home/$user");
} else {
print "User has a non-standard home directory.\n";
print "Please remove manually.\n";
print "Directory = $info[7]\n";
}
print "User $user -- Deleted\n";

edit_file("/etc/passwd");

flock(PW_FILE,LOCK_UN);
close(PW_FILE);</p>

This script  must be run as root user.

Running the script


root@bt:~/src/perl# ./del_user_a.pl
Usage is ./del_user_a.pl <user>
root@bt:~/src/perl# ./del_user_a.pl wysocand

Running On BackTrack 5R2
perl_del_user

 

Source:

Kategorie:Back Track 5, Perl Tagi: ,

How To Automatically Create User Accounts in Unix ( Linux )

Czerwiec 27, 2012 1 komentarz

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


#!/usr/bin/perl
 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 = <STDIN>;
 chomp($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 = <PW_FILE>;

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) {
 next;
 }
 if ($fields[2] > $uid) {
 $uid = $fields[2];
 }
 }
 $uid++;

# 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 = <STDIN>;
 chomp($full_name);

my $shell = "";    # Get user shell to use
 while (! -f $shell) {
 print "Shell: ";
 $shell = <STDIN>;
 chomp($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
 "${login}:x:${uid}:${gid}:${full_name}:${home_dir}:$shell\n";

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
 mkdir($home_dir);
 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");

flock(PW_FILE,LOCK_UN);
 close(PW_FILE);

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

Example on BackTrack Linux:

add user

Add New User

And:

Add New User

Add New User

 

Source:

1.Perl for System Administration.http://docstore.mik.ua/orelly/perl/sysadmin/index.htm

 

Kategorie:Back Track 5, Perl

Perl – OnLine Library

Czerwiec 27, 2012 Dodaj komentarz
  1. HTMLified Perl 5 Reference Guide – http://www.oopweb.com/Perl/Documents/Perl5Ref/VolumeFrames.html
  2. Perl 5 Documentation – http://www.oopweb.com/Perl/Documents/PerlDoc/VolumeFrames.html
  3. Perl for Perl Newbies – http://www.oopweb.com/Perl/Documents/P4PNewbies/VolumeFrames.html
  4. Perl for Win32 FAQ – http://www.oopweb.com/Perl/Documents/PerlWin32/VolumeFrames.html
  5. Beginning Perl – http://www.perl.org/books/beginning-perl/
  6. Impatient Perl – http://www.perl.org/books/impatient-perl/
  7. Extreme Perl – http://www.extremeperl.org/bk/home
  8. MacPerl: Power & Ease – http://macperl.com/ptf_book/r/MP/i2.html
  9. Embedding Perl in HTML with Mason – http://www.masonbook.com
  10. Perl for the Web – http://www.globalspin.com/thebook/
  11. Web Client Programming with Perl – http://www.oreilly.com/openbook/webclient/
  12. Perl 5 By Example – http://www.computer-books.us/perl_0010.php
  13. An Introduction to Perl – http://www.linuxtopia.org/Perl_Tutorial/index.html
  14. Beginning CGI Programming with Perl – http://www.learnthat.com/internet/learn-160-cgi_programming_perl.htm
  15. Perl Tutorial: Start – http://www.comp.leeds.ac.uk/Perl/start.html
  16. A Perl Tutorial – http://www.civeng.carleton.ca/Courses/Grad/1995-96/82.562/perl/
  17. Robert’s Perl Tutorial – http://www.sthomas.net/oldpages/roberts-perl-tutorial.htm
  18. Beginning Perl Tutorials – http://www.pageresource.com/cgirec/index2.htm
  19. Beginner’s Guide to CGI Scripting with Perl – http://www.lies.com/begperl/
  20. Practical Perl Programming – http://www.cs.cf.ac.uk/Dave/PERL/
  21. Perl 5 Unleashed – http://octopus.cdut.edu.cn/~yf17/perl5/
  22. Perl for System Administration – http://www.unix.org.ua/orelly/perl/sysadmin/index.htm
  23. PERL — Practical Extraction and Report Language – http://www-cgi.cs.cmu.edu/cgi-bin/perl-man
  24. Programming Perl – http://www.unix.org.ua/orelly/perl/prog3/
  25. Steve Litt’s Perls of Wisdom – http://www.troubleshooters.com/codecorn/littperl/index.htm
  26. Perl Regular Expression Tutorial – http://virtual.park.uga.edu/humcomp/perl/regex2a.html
  27. Perl Documentation – http://www.perl.com/pub/q/documentation
  28. Programming Perl 5 – http://www.squirrel.nl/pub/perlref-5.004.1.pdf
  29. Beginner’s Introduction to Perl – http://www.perl.com/pub/a/2000/10/begperl1.html
  30. Perl in a Nutshell – http://www.unix.org.ua/orelly/perl/perlnut/index.htm
  31. Programming Perl, 3rd Edition – http://www.unix.org.ua/orelly/perl/prog3/index.htm
  32. Advanced Perl Programming – http://www.unix.org.ua/orelly/perl/advprog/index.htm
  33. Perl Cookbook – http://www.unix.org.ua/orelly/perl/cookbook/index.htm
  34. XML processing with Perl – http://www.xmltwig.com/tutorial/perl_xml/mtb04_01.html
Kategorie:Back Track 5, Perl