Thursday, December 15, 2011

Recover files deleted in linux

If any files accidentally deleted and needs to be recovered. First look in lost+found folder.
If not found follow the below steps.

Mount the filesystem in read only to avoid any rewrites on the disk block
# mount -o remount ro /dev/sdx /mntpoint.

If the partition is a root filesystem - Boot the server in single user mode
# init 1

To recover a text file you can use grep command to recover the contents. For this you need to know any uniq word from the deleted file - Preferably the start of the file.
grep -a -B2 -A1000 'uniq_word' /dev/sdx > /tmp/filerecovered.txt

It means 2lines before the uniq_word found and 1000lines after the uniq_word found from the filesystem and save the content into filerecovered file. (ensure the destination folder is read-write mode.
===================
Recover other types of files

Use package foremost There is an excellent article published in howtoforge portal.




Friday, December 09, 2011

check the disk space availablity

Script to check the disk space and mail if the disk space availablity is below the warning or critical thresholds

### Variable declaration ####
WARN=75
CRITICAL=77
ADMINI_LABS="useremailaccount"


#### Collecting Disk space information #####
df -h |grep -vE 'tmpfs|Filesystem|cdrom'|awk '{print $5 " " $6}' > /tmp/disksize

while read output; do
diskusage=$(echo $output|awk '{print $1}' |cut -d% -f1 )
filesystem=$(echo $output |awk '{print $2}' )
if [ $diskusage -ge $WARN ] && [ $diskusage -le $CRITICAL ]; then
mail -s"WARNING: $(hostname) : Running out of space $filesystem $diskusage" $ADMIN_ILABS
elif [ $diskusage -ge $CRITICAL ]; then
mail -s "CRITICAL: $(hostname) : Running out of space $filesystem $diskusage" $ADMIN_ILABS
fi
done < /tmp/disksize

## File Clean up ##
rm -f /tmp/disksize

Tuesday, November 08, 2011

Using CURL to get secure website information.

Create cookie jar for the URL login

curl -k --cookie-jar cjar --output /dev/null \
https://IPADDR/login

Passing the login credits to the site for fetching the information.
curl -k --cookie cjar --cookie-jar cjar \
--data 'username=USERNAME' \
--data 'password=PWD' \
--data 'form=login' \
--data 'action:Login=Login' \
--location \
--output /tmp/loginresutl.html \
https://IPADDR/login

Get the Secure Ironport information using the curl command using the cookie jar.
curl -l --cookie cjar \
--output /tmp/ironport_system_status.html \
https://IPADDR/monitor/reports/system_status

Tuesday, October 18, 2011

Configuring & Managing DELL PERC array controller from Linux Operating System

MegaRAID Cli
-----------------
http://www.thegeekstuff.com/2009/05/dell-tutorial-create-raid-using-perc-6i-integrated-bios-configuration-utility/
http://tools.rapidsoft.de/perc/perc-cheat-sheet.html

Download
http://www.lsi.com/downloads/Public/Obsolete/Obsolete%20Common%20Files/dr_linux_2.20.4.6_as40.zip

HP Array Controller card configuration from Linux
---------------------------------------------------
http://www.datadisk.co.uk/html_docs/redhat/hpacucli.htm

---------------------------
View Enclosure Details
MegaCli -EncInfo -aALL

View Array Controller on Server
MegaCli -AdpAllInfo -aAll

View Physical disk attached to the Array Controller
MegaCli -PDList -aAll
---------------------------

View Types of RAID Supported by Array Controller Card.
MegaCli -AdpAllInfo -aAll|grep RAID

Create RAID 1 / 5 from MegaRAID Cli
MegaCli -CfgLdAdd -r1 [8:2, 8:3] -a0
8 = Enclosure Device ID Number - Can find it out from PDList information
2 & 3 = Disk Slot Number - Can find it out from PDList information
a0 - Adapter Number

MegaCli -CfgLdAdd -r5 [8:2, 8:3, 8:4] -a0
Raid 5 Creation
--------------------------------
View Virtual Device Information.
MegaCli -LDInfo -Lall -aALL
---------------------------------

Set disk state to offline
MegaCli -PDOffline -PhysDrv [8:3] -a0

MegaCli -PDList -aAll |egrep 'Enclosure|Slot|Firmware'
Enclosure Device ID: 8
Slot Number: 3
Firmware state: Offline
---------------------------------

Set disk state to online
MegaCli -PDonline -PhysDrv [8:3] -a0

MegaCli -PDList -aAll |egrep 'Enclosure|Slot|Firmware'
Enclosure Device ID: 8
Slot Number: 3
Firmware state: Offlin
-------------------------------

Mark Disk as missing
MegaCli -PDMarkMissing -PhysDrv [8:3] -a0

Prepare disk for removal
MegaCli -PdPrpRmv -PhysDrv [8:3] -a0

Replace Missing physical disk
MegaCli -PdReplaceMissing -PhysDrv [8:3] -ArrayN -rowN -aN

Rebuild the newly added disk
MegaCli -PDRbld -Start -PhysDrv [8:3] -aN

Check rebuild /start/stop/status rebuild on the server.
MegaCli -PDRbld -Start -PhysDrv [E:S] -aN
MegaCli -PDRbld -Stop -PhysDrv [E:S] -aN
MegaCli -PDRbld -ShowProg -PhysDrv [E:S] -aN

Thursday, September 29, 2011

Friday, August 19, 2011

View smbuser on the samba-share

Samba - List the smbuser added to the samba share.

pdbedit -L = This will list all the smbuser added using smbpasswd -a command
pdbedit -Lv = This will extended view of the smbuser list.

Thursday, July 28, 2011

Configuring ISCSI targets and initiators.

ISCSI Server installation and Configuration.

ISCSI (Internet Small Computer System Interface) - (IP)-based storage networking standard for linking data storage facilities. By carrying SCSI commands over IP networks
iscsi targets should be raw disk or block file only.


ISCSI Target - Server from where the ISCSI disk space is shared.
ISCSI Initator - Clients on which the ISCSI targets are mounted.

SERVER / TARGET side Configuration
====================

Package requirments
----------------------
yum install scsi-target-utils.i386
yum install openssl-devel.i386

/etc/init.d/tgtd start

Creating iscsi lun
=============
tgtadm --lld iscsi --op new --mode target --tid 1 -T iqn.2011-04.com.mxmail:storage.disk2.mxmail.com
tgtadm --lld iscsi --op show --mode target

Attaching storage to the LUN
----------------------------
tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /iscsifile

or for physical disk
tgtadm --lld iscsi --op new --mode logicalunit --tid 1 --lun 1 -b /dev/sdb
tgtadm --lld iscsi --op show --mode target

Securing the iscsi targets
===================

IP based restriction
------------------
tgtadm --lld iscsi --op bind --mode target --tid 1 -I ALL #Allowing all IP on the network to access Target ID1
tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.2.10 #Allowing specific IP access to Target ID1
tgtadm --lld iscsi --op bind --mode target --tid 1 -I 192.168.2.0/24 #Allowing specific IP subnet to Target ID1

User based restriction
---------------------
tgtadm --lld iscsi --op new --mode account --user user1 --password password1 #Creation of User account
tgtadm --lld iscsi --op bind --mode account --tid 1 --user fujita #Adding User account to the target ID1
tgtadm --lld iscsi --op unbind --mode account --tid 1 --user fujita #Removing User account to the target ID1

-------------------------------------------------------------------------------------
To make the configuration persistent across add the above lines into /etc/rc.local file
--------------------------------------------------------------------------------------



INITIATOR side Configuration
=====================

yum install iscsi-initiator-utils-6.2.0.865-0.8.el5
yum install lsscsi-0.17-3.el5
service iscsi start
chkconfig iscsi on

Running discovery of the iscsi server.
iscsiadm --mode discovery --type sendtargets --portal 192.168.10.10 (Target Server IP)
Should list an output like this: 192.168.10.10:3260,1 iqn.2011-04.com.mxmail:storage.disk2.mxmail.com

vi /etc/iscsi/initiatorname.iscsi
add this value - InitiatorName=iqn.2011-04.com.mxmail:storage.disk2.mxmail.com

iscsiadm -m node
Will show all the iscsi devices.

#lsscsi (Command to display the mapped iscsi disks)
[0:0:0:0] disk VMware Virtual disk 1.0 /dev/sda
[9:0:0:0] storage IET Controler 0001 - #LUN Controller
[9:0:0:1] disk IET VIRTUAL-DISK 0001 /dev/sdb #Virtual Disk mapped to /dev/sdb

#fdisk -l /dev/sdb
Disk /dev/sdb: 3221 MB, 3221225472 bytes
100 heads, 62 sectors/track, 1014 cylinders
Units = cylinders of 6200 * 512 = 3174400 bytes

Disk /dev/sdb doesn't contain a valid partition table

All configuration files for iscsi is stored in /var/lib/iscsi/




Procedure to delete a LUN from ISCSI Server

Disable the connection from client ISCSI targets and Logging out of the portal session,

iscsiadm --mode discovery --type sendtargets --portal 192.168.1.1 (List the portal sessions)

iscsiadm --m node -T iqn.2013-08.com.iscsi:storage.proxmox_vm.mllab.net  --portal 192.168.1.1:3260 -u ( Will log out from the portal sessions)


iscsiadm -m node -o delete -T iqn.2013-08.com.iscsi:storage.proxmox_vm.mllab.net  --portal 192.168.1.1:3260 (Delete the portal connections)


Remove the Target from the ISCSI server side


Below listed are the steps to be followed for deleting a LUN from the ISCSI server.

Assumptions for this example,

The ISCSI server has multiple LUN configured. Each LUN is using one logical Unit(disk/slice/folder/blockdevice)


Unbind the IP Address from the Access List before deleting the logical unit.
tgtadm --lld iscsi --op unbind --mode target --tid 3 -I 192.168.1.0/24
tgtadm --lld iscsi --op unbind --mode target --tid 3 -I 172.22.1.0/24


Delete the Logincal Unit (Device/Folder/Slice) that is mapped to the LUN
tgtadm --lld iscsi --op delete --mode logicalunit --tid 3 --lun 1


Look for any active sessions connected to the LUN.
tgtadm --lld iscsi --op show --mode target
  I_T nexus information:
        I_T nexus: 10
            Initiator: iqn.1998-01.com.vmware:esxi1-4805d107
            Connection: 0
                IP Address: 192.168.1.2
        I_T nexus: 13
            Initiator: iqn.1998-01.com.vmware:esxi3-4716f412
            Connection: 0
                IP Address: 192.168.1.4
        I_T nexus: 15
            Initiator: iqn.1998-01.com.vmware:esxi5-31cd0643
            Connection: 0
                IP Address: 192.168.1.6

In this case there are 3 Sessions connected to the LUN (Connection ID 10, 13 and 15)

Disconnect each of the sessions before deleting the LUN.
tgtadm --lld iscsi --op delete --mode conn --tid 3 --sid 10
tgtadm --lld iscsi --op delete --mode conn --tid 3 --sid 13
tgtadm --lld iscsi --op delete --mode conn --tid 3 --sid 15


Delete the LUN ID 3
tgtadm --lld iscsi --op delete --mode target --tid 3


View to confirm the target LUN is deleted.

tgtadm --lld iscsi --op show --mode target

Tuesday, May 31, 2011

Script to populate mails to mail accounts with an attachment and contents in body.

Shell Script for populating mails to mail users - For testing reasons.

while read username
do
for i in {1..25}
do
mutt -s "sample mail $i" -a /root/attachment.log -- $username"@mltech.com" < mailbody.template
done
done < useraccount

useraccount file will contain mail accounts.
mailbody.template file will contain mail body content.
attachment.log file is the attachment file.

Oneliner script for user account & password creation in linux

You need to have root user privileges to run some of the commands in the script needs.

user=openuser;for i in {1..50};do useradd $user$i;pd=abc;echo $user$i$pd |passwd --stdin $user$i;done

This will create user names openuser1, 2 till 50 with password "openuser1abc, openuser2abc, etc.,"

Tuesday, March 15, 2011

DOS - Batch programming - Accept command input for batch programm

hello.bat file content
@echo welcome %1 %2 %3 %4 boy

Execution.
hello one two three four

output will be
C:\Documents and Settings\retheesh\Desktop>hello one two three four (Input parameters)
welcome one two three four boy

Friday, March 04, 2011

perl oneliners

perl -p -i -e 's/oldstring/newstring/g' filename.
This will change the oldstring to newstring in the file.



Perl oneliner to install modules. (Note should have internet access and should have CPAN configuration done)


perl -MCPAN -e 'install HTML::Template'
                 OR

cpan -i 'install HTML::Template'



SED one Liner