Thursday, May 19, 2011

Make sendmail to use gmail MTA

[amit@thebuggzbox ~]$ sudo yum install ssmtp
[amit@thebuggzbox ~]$ sudo bash -c 'cat > /etc/ssmtp/ssmtp.conf'
AuthUser=yourgmailid@gmail.com
AuthPass=yourgmailpassword
FromLineOverride=YES
UseSTARTTLS=YES
mailhub=smtp.gmail.com:587 (press ctrl+d here)
[amit@thebuggzbox ~]$ sudo service sendmail stop
[amit@thebuggzbox ~]$ sudo chkconfig sendmail off
[amit@thebuggzbox ~]$ sudo mv /usr/sbin/sendmail ~
[amit@thebuggzbox ~]$ sudo ln -s /usr/sbin/ssmtp /usr/sbin/sendmail
[amit@thebuggzbox ~]$ echo "this is sample test mail" | mail -s "test mail" thebuggz@gmail.com
[amit@thebuggzbox ~]$

[Hope you dont need any explanation from my side though i am open if any]

Sunday, May 15, 2011

Making GUI for bash scripts

Inspiration for this is my doctor wife, She always use to say your work is black screen as i use black background with green fonts on my terminal. Just as theme of this blog. I got something to write something on my blog, yes its ZENITY

In college time i have prepared GUI for some of my customised shell scripts to help faculties while doing lab records evaluations as they might not spend writing bas scripts for doing some silly tasks. They just have to click some buttons and script in background used to collect student programs from different working directories , compare them against each other to find how many of them have copied, and do some other tasks. Tools used was zenity. i did a grep in rpmdb of my fedora box and found i have zenity2 installed on box. using man command and Yahoo! search i got some more info and within an hour I build GUI interface having buttons for all my commonly used command lines. let me give you some tips about zenity

To start up with zenity here is basic hello world of zenity
$zenity --info --text "Hello World\!"

In case u see 'command not found' kind of thing don't abuse me ..you need to install zenity in your linux box, fedora users can install it using following command

$sudo yum install zenity

so you have got a idea how to open a information box, now try input box

$zenity --entry --text "Enter some text" 

text enter in this will be echo-ed back its upon you what you want to do with that

[apundir@fencenerve-lx ~]$ ans=$(zenity --entry --text "Enter some text"); echo $ans
whoami
[apundir@fencenerve-lx ~]$ `zenity --entry --text "Enter some text"`
apundir
[apundir@fencenerve-lx ~]$

Create error dialog box now 

zenity --error --text "error box for you! "
 

Consider them as building blocks for your GUI screen

As i believe we must do some small exercise to check how things work here i am giving you some shell script all you need to do is copy this script to a file in your system making it executable using chmod and run it. If you have time you can always make changes to it and experiment a lot


 
-----------script starts here ----------------
#!/bin/bash
#######################################################
if [ $(whoami) != "root" ]; then 
 zenity --error --text="Sorry ! you are not root user."
 exit 1
fi

zenity --info --text="This Script prepared by Amit Pundir just for blog"

menu(){
im="zenity --list --radiolist --width=800 --height=600 --title \"Examserver\""
im=$im" --column=\" \" --column \"Items\" --column \"Description\" "

im=$im"FALSE \"Task 1\" \"Sample task 1\" "
im=$im"FALSE \"Task 2\" \"Lock a User\" "
im=$im"FALSE \"Task 3\" \"Unlock a User\" "
im=$im"FALSE \"Task 4\" \"Send a message to wall\" "
im=$im"FALSE \"Task 5\" \"Shut down Computer\" "
}

option(){
choice=`echo $im | sh -`

if echo $choice | grep "Task 1" > /dev/null; 
then


# put for script here 
zenity --info --text="Task 1 executed successfully"

fi

if echo $choice | grep "Task 2" > /dev/null; 
then

# task 2 scipt comes here , 
#suppose you need have take some user input for task 2 add following line
# locking a user in the system
u=zenity --entry --text="Enter loginid of user your want to lock"
passwd -l $u;
zenity --info --text=" User locked in the system"

fi 

if echo $choice | grep "Task 3" > /dev/null; 
then

# task 3 scipt comes here , 
# unlocking a user in the system
u=zenity --entry --text="Enter loginid of user your want to unlock"
passwd -u $u;
zenity --info --text="User is unlocked in the system"

fi 

if echo $choice | grep "Task 4" > /dev/null; 
then
 msg=$(zenity --entry --text="Enter your message")
 wall $msg;
fi

if echo $choice | grep "Task 5" > /dev/null; 
then
 /sbin/shutdown -h +2 Please save your files,Server will close all connection in 2 minutes!;
fi 

}

menu
option
if test ${#choice} -gt 0; then
clear
echo "All Finished"
fi

exit 0
------------script ends here ----------

And i forgot to mention by the time i finished writing this blog it was night and i my wife got from her sleep and told me that today your laptop is making more light in dark room and distubing her sleep. i realised this extra light was just because of the GUI pop ups of white color