Sunday, 19 May 2013

Sendmail with To, Cc , Bcc fields and subject ,message body etc




Sendmail is a pretty old mail interface and hence sending mail with all fields like Subject, Body and Content using sendmail is a bit tedious. I have seen users sometime getting frustrated with sendmail because they do not know how to use it properly and utilize its full features. They come to unix admin for help in sorting out their issues. In this post i will show how to add To, Cc , Bcc fields and subject / message body in the sendmail message. I will share a trick to get sendmail working as desired.


First create a sendmail.txt file with below contents :-

*********************************************************************************

From: root@pnc.com
To: abhi@xyz.com
Cc: nlm@xyz.com
Date: echo "`date`"
Subject: HELLO
Mime-Version: 1.0
Content-Type: text

Write the message body here , this is the mail content.  


*********************************************************************************

The above plain text file contains all the desired fields. Most important part is the Content-Type declaration. 

Then run the below command from unix prompt

# cat sendmail.txt | /usr/lib/sendmail -t 

The -t option of sendmail as per manpages


     -t          Read message for recipients.  To:, Cc:, and Bcc: lines will
                 be scanned for recipient addresses.  The Bcc: line will be
                 deleted before transmission.
 
The above command simply takes input for sendmail command from the text file we just created. Once you send the mail you can see all fields are visible and sendmail works as per your requirement. Do let me know if you face any issue in running sendmail with these options.
 
 

   

Friday, 17 May 2013

How to delete all files in a directory except the last 3 files ?

The trick here is as follows
1) Do the listing sorted by file modification time.
2) Use tail or head to cut out the 3 latest files and
3) Then do ’ rm’ on all files that remain.

Suppose you have to delete all files except the latest 3 from the folder /var/path_to_folder.
So the below command will accomplish your task.

# cd /var/path_to_folder && /usr/xpg4/bin/ls -t /var/path_to_folder | tail -n +3 | xargs rm –r


The above command lists all files in the directory sorted as per last modification time ( you may use –u switch to ls command to sort according to last access time instead of last modification time, see man page for ls for details), then tail command takes the listing as its input and cuts out the 3 files from top. Pay attention to the + sign before 3 , if i had used -3 in place +3 the result would have been something entirely different. The –n +3 operates on top of the listed output and removes the top 3 lines from the output. On the other hand , -n -3 would have operated on last 3 lines of the output and instead of removing those lines it would have printed out those lines to the terminal. The meanings and usage of + and - are very different. The usage also changes dramatically if i had used head instead of tail to cut out the 3 lines. Take care of this and before going on to our next step with "xargs and rm" command, be sure of the output you are getting here , as you would end up deleting last 3 files accidently if you misinterpreted the above.

Why i used xargs ? Because xargs constructs argument list and invokes the desired utility ( in our case ‘rm’ ) sequentially on each argument from the list. Our output after doing the tail is a list of files, one per line , as is usual when doing listing. The "rm" command wont act on these one per line arguments if supplied as it is to a single rm invocation. So xargs helps us here by invoking rm once for each filename.

svcadm enhancements in Solaris 10 - Bind to localhost

Some services in solaris 10 operate based on local and global properties.
For example, in rpcbind configuration if the value for local_only is set to true, all rpc services are accessible from inside the machine but an outside machine cannot access these services.

bash-3.00# svccfg -s rpc/bind listprop config/local_only
config/local_only  boolean  false
bash-3.00#
bash-3.00# svccfg -s rpc/bind setprop config/local_only=true
bash-3.00#
bash-3.00# svcadm refresh rpc/bind
bash-3.00#
bash-3.00# svccfg -s rpc/bind listprop config/local_only
config/local_only  boolean  true
bash-3.00#
bash-3.00# svccfg -s rpc/bind setprop config/local_only=false
bash-3.00#
bash-3.00# svcadm refresh rpc/bind
bash-3.00# svcadm refresh rpc/bind
bash-3.00#
bash-3.00# svccfg -s rpc/bind listprop config/local_only
config/local_only  boolean  false

This in my opinion is a significant security enhancement, especially in some cases where you want a particular service to be accessible from localhost but disabled for outside machines. Many solaris services have this kind of configurability.



Wednesday, 15 May 2013

Solaris 10, project files


On Solaris 10, system V  IPC paramters( e.g shmmax,shmseg) are set under user specific projects and these values take effect on a per-project basis only and are not system-wide values. We do not need to set them in /etc/system and even if set, the values are ignored.

All the processes started by users who are member of a project inherit the parameter values from /etc/project file.

hostA: /etc\> projects -l

user.oracle
        projid : 1001
        comment: "Oracle Project"
        users  : oracle
        groups : dba
                 oinstall
        attribs: process.max-sem-nsems=(priv,256,deny)
                 project.max-sem-ids=(priv,100,deny)
                 project.max-shm-ids=(priv,128,deny)
                 project.max-shm-memory=(priv,4294967296,deny)


hostA: /etc\> more /etc/project
system:0::::
user.root:1::::
noproject:2::::
default:3::::
group.staff:10::::
user.oracle:100:Oracle Project:oracle:dba,oinstall:process.max-sem-nsems=(priv,256,deny);project.max-sem-ids=(priv,100,deny);project.max-shm-ids=(priv,128,den
y);project.max-shm-memory=(priv,4294967296,deny)


      After editing the /etc/project file, we also need to give privilege to oracle user to be able to use projects, otherwise even if projects are created properly , it will not take effect.

ggnqccita2: /etc\> more /etc/user_attr
oracle::::project=user.oracle

The values set in /etc/project file are dynamic and do not need reboot to take effect. In previous versions of solaris, the values set in /etc/system did not take effect until reboot.


Tuesday, 14 May 2013

Can we recover a file from [lost + found] folder


Yes we can , but the recovered files after running fsck on the filesystem  will have absurd names like #p89t3590, hence containing correct owner, group and inode info but will not have correct names.
Files that appear in lost+found are typically files that were already unlinked (i.e. their name had been erased) but still opened by some process (so the data wasn't erased yet) when the system halted suddenly (kernel panic or power failure)
Files can also appear in lost+found because the filesystem was in an inconsistent state due to a software or hardware bug.

Two files with same name in a directory !!!


Have you ever seen two files with same name inside a directory ? Is it possible at all ? What if a user shows you this, right in front of your eyes ?

Actually, it is impossible to have two files of the same name in a directory,unix does not permit this. If at all such behaviour is observed, then one of the files must have control characters in its name!! and it simply means these are 2 different files and so must have different inode numbers.

Check the different inode numbers of the files with below command.

# ls -lib           -----> this command will show inode number and any control characters in the filename.

The man page for ls says that –b will ……
List nonprinting characters in the octal \ddd notation


Once we have the inode number ( in the example below i am using 2460 as inode number), we can easily use find command to move the dubious file to a safe location and delete it there.

# find / -xdev -inum 2460 -exec mv {} /tmp/wastebin/ \;
# rm –rf /tmp/wastebin