Perl Scripts for Unix
You can use these scripts in any way you want and welcome to send questions/suggestions. I am in no way responsible for any problems you come across using these scripts.
I put some working Perl scripts for Unix administration on this page. I also put some explanation notes for easy understanding of the scripts. I tried not to use the in-built system tools where ever possible just to get a hang of the Perl scripting. So some times these scripts may not be the efficient way to deal with the problem. I hope these scripts are useful understanding the basics of Perl in day to day System's administration.
Perl Script for creating users under UNIX
This first example is a script to create users automatically on first example is a script to create users automatically on most of the Unix systems. This shows the procedure to open the /etc/passwd, /etc/shadow and /etc/group files in Write mode and add the appropriate lines to create the new user's details. I understand that much more can be done to further automate this. This script is just a beginning!
# Perl Script to Create users under Unix
# Author Sai Prasad Kesavamatham, 1998
# Tested under Solaris 2.5
print "Enter the User_Login id to be created : ";
$Login_Id=<STDIN>;
while ($Login_Id eq "\n"){
print "You have not entered a Login_Id! Please re-enter the User_Login name you wish to create: ";
$Login_Id=<STDIN>;
}
chop($Login_Id);
# ***** Checking for user-id duplication
open (PASSWD,"/etc/passwd") || die "Not able to open /etc/passwd file for reading\n";
while ($Passwd_Line=<PASSWD>){
@Passwd_Array=split(/:/,$Passwd_Line);
if (@Passwd_Array[0] eq $Login_Id){
print "A user with that id already exists!\n";
exit (0);
}
}
close (PASSWD);
# User Login_Id does not exist; OK to proceed...
# Making sure that the User_Id is unique
$User_Id=@Passwd_Array[2]+1;
again:
foreach $Passwd_Array(@Passwd_Array){
if ($User_Id eq $Passwd_Array){
$User_Id++;
goto again;
}
} # End of FOR
print "$User_Id\n";
# *******End of check for user-id duplication and group id number
# User-Id is unique; OK to proceed...
# Reading the group file
open (GROUP,"/etc/group") || die "Not able to open /etc/groups file for reading\n";
while ($Group_Line=<GROUP>){
@Group_Array=split(/:/,$Group_Line);
print "@Group_Array[0]\t\t\t@Group_Array[2]\n";
}
close (GROUP);
print "\nSelect the User_Group number for the user, from the groups above and press Enter: ";
chop ($Group_Id=<STDIN>);
# End of reading group file
print " Creating the User Home Directory under /export/home\n";
print "\n Type in User home directory name or press Enter for default [$Login_Id]: ";
$Home_Directory=<STDIN>;
if ($Home_Directory eq "\n"){
$Home_Directory=$Login_Id;
}
else {chop($Home_Directory);}
print 'Type in the shell with full path as shown or press enter for default [/bin/sh]: ';
$Shell=<STDIN>;
if ($Shell eq "\n"){
$Shell="/bin/sh";
}
else {chop($Shell);}
print "\n";
print 'Type in any description for this user (Ex. Full Name): ';
chop($Description=<STDIN>);
#***********
# Create the home directory under /export/home
if (!mkdir ("/export/home/$Login_Id",0755))
{
print <<EOM;Not able to create home directory.
Error : $!
User creation aborted! Try again!!Written by Sai Prasad, 1998
EOM
exit (1);
}
#***********
else {
# Forming the Passwd_Entry line to be appended to /etc/passwd
$Passwd_Entry="$Login_Id:x:$User_Id:$Group_Id:$Description:/export/home/$Home_Directory:$Shell\n";
# Forming the Shadow_Entry line to be appended to /etc/shadow
$Shadow_Entry="$Login_Id:"."::";
open (PASSWD,">>/etc/passwd") || die "Not able to open /etc/passwd for writing\n";
print PASSWD "$Passwd_Entry";
close (PASSWD);
open (SHADOW,">>/etc/shadow") || die "Not able to open /etc/shadow for writing\n";
print SHADOW "$Shadow_Entry\n";
close (SHADOW);
# Copy the .profile to the home directory
if (system ("cp /etc/profile /export/home/$Login_Id/.profile"))
{
print "Not able to copy the .profile file\n";
}
# Set the CHOWN settings on the home directory
if (system ("chown $Login_Id:$Group_Id /export/home/$Login_Id"))
{
print "Not able to set the owner for the directory\n";
}
# Set the file permissions on .profile
system ("chown 0755 /export/home/$Login_Id/.profile");
print "\n\n The User: $Login_Id has been created successfully\n\n";
}print "Written by Sai Prasad, 1998\n\n";
Process Kill
This second example is a script to kill all the running processes under the specified name. Now what is the need to kill all the processes? Frankly this is a script just to show a way to pipe to/from a process and to parse the results in a required way. This could be used when trying to fork many similar processes.
# Perl Scirpt PID.PL
# Author Sai Prasad Kesavamatham, 1998
# Use this to kill ALL the processes running on the specified name
# Command line arguments / parameters are needed
# Usage: perl pid.pl process name
# Tested under Solaris 2.5
if ($#ARGV ne 0) {
print ("Insufficient number of parameters entered; Please check the Syntax below\n\n");
print "Usage: perl pid.pl process_name\n";
print (" example: perl pid.pl telnet\n\n");
exit (1);
}# $Input is the process name taken from the first argument; $i is the counter to show the number of process running
$Input=$ARGV[0];
$i=0;# Open a pipe for ps -e process and with TMP handler , capture the output, parse the output and close the handler
open (TMP, "/usr/bin/ps -e |");
while ($line = <TMP>){
@Process_Array=split(/\s+/,$line);
# chop(@Process_Array);# From each line, compare the process name with the specified input process name; If found kill the process
if (@Process_Array[4] eq $Input) {
print "Found the Process PID: @Process_Array[0] for $Input\n";
system ("kill -9 @Process_Array[1]");
print "\nKilled the process $Input\n";
$i++;
}
}close (TMP);
if ($i==0) {
print "\nNo '$Input' process have been found\n";
}print "Written by Sai Prasad, 1998\n\n";
FTP
This third example is a script to do auto ftp. You could use a sub-routine to test the availability of the host before proceeding with the dump command. I used the Net::FTP module. Click here for other ftp command syntax details.
#!/usr/local/bin/perl ############################################ ## Program: ftp.pl ## Author: Sai Prasad Kesavamatham ## To automate the ftp process ############################################# unshift (@INC,"/export/home/user"); use Net::FTP; &Ping_Test (spidey); $ftp = Net::FTP->new("spidey", Debug => 0); $ftp->login("user_id_here",'password_here'); $ftp->cwd("/export/home/user"); $ftp->put("/apps/application/etc/dns.stats","/apps/bind/dns.stats"); $ftp->quit; ### End of main sub Ping_Test{ print "\n\n\n Testing the network connectivity ...\n"; open (TMP, "/usr/sbin/ping $_[0] |"); chop($line = <TMP>); close (TMP); @array=split(/\s/,$line); if (@array[2] ne 'alive') { print ("I am not able to ping the Host: $_[0], Please check the connectivity\n\n");exit(1); } print " \nFound the Host $_[0] !!\n\n"; }