Innovation The Key To Success
Scripts
Remote SSH Login Without Password, Execute Command Via SSH and Backup Up Data Using Rsync
Oct 21st
Remote SSH Login Without Password, Execute Command Via SSH and Backup Data Using Rsync
Secure Shell or SSH is a network protocol that allows encrypted data transfer and password between networks. SSH protocol is commonly used for remote server management. It gives us safe and secure path to work on remote machines.
Each time while we access a remote machine via ssh protocol, we need to manually enter the password. This would be a tough job during the repeated access. More server logins are easier via ssh key pair. This technology doesn’t require password for login. Private and public keys are used for this purpose. It is important to keep your keys secure, otherwise our server get hacked by others.
We use ssh key pair for remote login and execution of commands via ssh.
First check whether SSH keys are present.
# ls ~/.ssh/
If not generate new SSH keys.
# ssh-keygen -t dsa
Output
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): NewPassword
Enter same passphrase again: NewPassword
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
2d:25:2d:39:88:63:3d:97:56:4a:75:af:f9:1e:0c:29 root@jmj.talk2melbin.com
* Generate SSH pair using new password.
* Don’t use the same root password.
Secure SSH folder with permission.
# chmod 755 ~/.ssh/
Copy the generated id_dsa.pub to remote server. Using “ssh-copy-id” is secure than “scp” command.
# ssh-copy-id -i ~/.ssh/id_dsa.pub root@remoteserver
Output
ssh key pair generation
Now try logging into the machine, with “ssh ‘root@remoteserver’”, and check in:
.ssh/authorized_keys
to make sure we haven’t added extra keys that you weren’t expecting.
Login into remote server.
# ssh remote server
Prompt ask for password. Enter the password used during the generation of SSH key.
Output
Enter passphrase for key ‘/root/.ssh/id_dsa’:
Security can increase using SSH Agent
SSH Remote Login Without Password.
check the status of identities,
# ssh-add -L
output
The agent has no identities.
Add ssh identities
# ssh-add
output
Enter passphrase for /root/.ssh/id_dsa:
Identity added: /root/.ssh/id_dsa (/root/.ssh/id_dsa)
After adding check the status using,
# ssh-add -L
Output
ssh-dss AAAAB3NzaC1kc3MAAACBALrFcmm ……….= /root/.ssh/id_dsa
Now happily login remote server without password.
# ssh user@remoteserver
Execute Command On Remote Server via SSH
We are able to run commands on remote server without login. Some examples are given below,
# ssh root@remoteserver 'ps -ef | grep apache | grep -v grep | wc -l'
Output
8
ssh root@remoteserver 'top -b -n 1 | head -n 10'
Output
top – 21:29:08 up 53 days, 23:30, 3 users, load average: 0.15, 0.07, 0.01
Tasks: 107 total, 1 running, 106 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.2%us, 0.1%sy, 0.0%ni, 99.0%id, 0.4%wa, 0.1%hi, 0.2%si, 0.0%st
Mem: 4037612k total, 4009164k used, 28448k free, 236620k buffers
Swap: 4096496k total, 144k used, 4096352k free, 3297004k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 15 0 10344 684 572 S 0.0 0.0 0:04.59 init
2 root RT -5 0 0 0 S 0.0 0.0 0:00.38 migration/0
3 root 34 19 0 0 0 S 0.0 0.0 0:02.03 ksoftirqd/0
Small Backup Script Using RSYNC
Script is scheduled on the backup storage machine via crontab.
Ruining the script will help to backup MySQL folder from remote server to another. This keep a backup copy of MySQL root folder of another machine. Logs are generated to view the data transfer between the server with actual date.
#/bin/bash echo "-------------------------------------------------------------------------------------------------" >> /var/BACKUP/secure/log/mysql.log /bin/date >> /var/BACKUP/secure/log/mysql.log rsync -avz -e "ssh -i /root/.ssh/id_dsa" root@remoteserverip:/var/lib/mysql /var/BACKUP/secure/ >> /var/BACKUP/secure/log/mysql.log
root@remoteserverip:/var/lib/mysql ; is remote server from where we took MySQL folder backup and /var/BACKUP/secure/ ; is the backup location to where we store the backup.
Cheers!
Melbin Mathew
www.talk2melbin.com
Bash Script For Subversion (Svn) Repository/Project Creation And User Management
Oct 14th
Bash Script For Subversion (Svn) Repository/Project Creation And User Management
In an organization project revisions and sharing is mostly done through subversion or concurrent versions system. This repositories also act as backup solution.
Creating new projects, editing apache svn.conf, add new users and their passwords are time taking. Inorder to make it simple i have created a bash script.
Script will help to create new project, add new users and their password. The password’s are stored on a common file. Script use two switches “c” (svnmel c) to create new project and “p” (svnmel p) to add and update the users and their passwords.
If the svn project repository already exists, script skips the creation of new project. Change the svn source code directory path and the password file path values mention on the begin of the script.
vi /usr/local/bin/svnmel
#!/bin/bash -e ################################################################################################################################################################################ # SVNMEL is a script used to create new svn projects in the svn parent directory. Script will append # the project variable on apache svn.conf file and also help to create a new # user name and password for authentication. This makes the svn directory secure. Script can use to # create new svn projects (repositories), create new authentication and configure apache svn.conf. # Two switches used for the working on the script. They are "c" and "p". By using "c" new project can # create and using "p" # we can add new user and password to apache passwd file. # ---By Melbin Mathew # www.talk2melbin.com # email: support@talk2melbin.com ################################################################################################################################################################################# #Modify svn home directory and password file paths. svnhome="/SVN" apache="/etc/httpd/conf.d/svn.conf" passfile="/SVN/httpasswd" value=$1 echo "$value" if [ $value == c ] then ## echo "Enter The New Project" read project #echo "hi $project" #svnadmin create project and append the apache svn.conf if [ -d "$svnhome/$project" ] then echo "Project Already Exist" exit else echo -e "Greeting Creating New Project \nAppending to Project to Apache svn.conf" svnadmin create "$svnhome/$project" echo -e "<Location /$project> \nDAV svn \nSVNPath $svnhome \nAuthType Basic \nAuthName "repo" \nAuthUserFile $passfile \nrequire valid-user \n</Location>" >> $apache fi ##Password creation## echo "Enter User Name" read user if [ -f "$passfile" ] then echo "Appending to httpasswd file" htpasswd -m $passfile $user else echo "Creating New Httpasswd File" htpasswd -cm $passfile $user fi ## Repeat value httpasswd## #passrepeat=$1 #if [ "pass == $passrepeat" ] elif [ $value == p ] then echo "Adding New User Name & Password" echo "Enter User Name" read user #echo $passfile $user htpasswd -m $passfile $user else echo "Parameter Wrong" exit fi
Cheers!
Melbin Mathew
Xmms: Changing Play list Using Bash Script – Update And Play Our Collection
Oct 1st
Xmms: Changing Play list Using Bash Script – Update And Play Our Collection
While listening to music, i usually change the play list on my Xmms player. Some time i do download of new songs and adding them to my collection have two music collection folders. One for English and other one for Malayalam. Each day i hear songs from both the collections (English, Malayalam). Normally it took little time to do loading and changing the play list. So i thought to write a script for changing the play list without much effort. The script is also capable of updating new songs from collection folder to their corresponding panelist.
Install Xmms Player
1. Set the yum repository with rpmforge-release
# cd /tmp
# wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
*Select rpmforge-release package according to the Linux OS version.
2. Install Xmms package using the yum
# yum install xmms-skins xmms xmms-mp3
Script
# vi /usr/local/bin/xmmel
#!/bin/bash
xmms=(`which xmms`)
#echo "$xmms"
echo -e "Enter your choice,\nFor English Press (e),\nFor Malayalam Press (m),\nFor Updation Press (u)"
read language
#echo $language
if [ $language == e ]
then
killall -15 xmms
##Store location for English play list####
$xmms/media/zongs/playlist/english.m3u &
echo "Enjoy English Song"
elif [ $language == m ]
then
killall -15 xmms
##Store location for Malayalam play list####
$Xmas /media/zongs/playlist/malayalam.m3u &
echo "Enjoy Malayalam Song"
elif [ $language == u ]
then
##Updation from my music folders# > ##to there corresponding play list#
find /media/songs/English/ -name *.mp3 > /media/zongs/playlist/english.m3u
find /media/zongs/Malayalam/ -name *.mp3 > /media/zongs/playlist/malayalam.m3u
echo "Updation Completed"
else
echo "Wrong Choice"
exit
fi
# chomd a+x /usr/local/bin/xmmel
Enjoy the Script
# xmmel
Options:
Song selection press “e” for English, “m” for Malayalam and “u” for Updating the collection.
Cheers!
Melbin Mathew
www.talk2melbin.com
Simple monitoring script using pgrep command
Sep 21st
This is a usefull script to monitor the service status.
The script checks the service status using pgrep command. If the command return zero script identifies the service down and send us an alert email.
#!/bin/bash apache=`pgrep httpd` mysql=`pgrep mysqld` tmp="/tmp/monitoring.tmp" touch /tmp/monitoring.tmp if [ -n "$apache" ] then echo "Apache Is Working" else echo "Apache Service Down" >> $tmp fi if [ -n "$mysql" ] then echo "Mysqld Is Working" else echo "Mysqld Service Down" >> $tmp fi EMAIL="****@talk2melbin.com" SUBJECT="Alert Service Down" MESSAGE="$tmp" COUNT=`wc -m $tmp | awk '{print $1}'` echo "$COUNT" if [ $COUNT -ne 0 ]; then /bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE fi rm -rf /tmp/monitoring
Cheers!
Melbin Mathew
www.talk2melbin.com
Simple Bash Script To Send Email Message
Sep 21st
Here is a small script that used to send email from bash,
#!/bin/bash #Enter the email address to which we need to send the message EMAIL="**@talk2melbin.com" #Enter the subject of the email SUBJECT="Alert Service Down" #Enter the body of the message, read it from a file MESSAGE="/tmp/monitoring.tmp" #Mail command to send the email /bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE
Cheers!
Melbin Mathew
www.talk2melbin.com