Wednesday, February 20, 2008

Some interesting things you can do with SED:
# more my_wc
#!/bin/sh
sed -n '$=' $1

# more my_head
#!/bin/sh
sed 10q $1

# more my_grep
#!/bin/sh
sed -n '/'$1'/p' $2

#more my_del_lines
#!/bin/sh
sed $1','$2'd' $3

#more my_tac
#!/bin/sh
sed -n '1!G;h;$p' $1

#more my_tail
#!/bin/sh
lines=`wc -l $2 awk '{print $1}' `
start=`expr $lines - $1`
sed "1,$start d" $2

#more line_insert

#!/bin/sh
sed '
/'$1'/ i\
Add this line before every line with WORD
' $2

Friday, January 11, 2008

You can change the unix user password via shell script in two ways

----------------------------------------------------------------------------
1. Using expect package.

#!/usr/bin/expect
set newpassword [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
expect "New password:"
send "$newpassword\r"
expect "Re-enter new password:"
send "$newpassword\r"
expect eof

----------------------------------------------------------------------------
2. In case your operating system do not have expect package then you can do it using the perl script.

#!/usr/bin/perl -w
#######################################################################################
# Usage : $0
# Note : This script need to be run as root.
# Author : Rishi Pahuja
# Created : 01 Oct 2007
#######################################################################################

use strict;

my ($usr, $plain) = @ARGV;
my $itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
my $salt;
for (1..2) { $salt .= substr $itoa64, rand(length($itoa64)), 1; }

my $password = crypt($plain, $salt);
#print `usermod -p $password $usr`;

# Changing the password
system("usermod -p $password $usr");
if ( $? == 0 ) {
print "\nOS password changed successfully for user $usr to $plain.\n";
}
else {
print "ERROR: Changing password for user $usr.\n";
print " Return Code: $?\n";
print "Exiting with return code of 1.\n";
exit 1;
}