Thursday, September 19, 2013

Ubuntu Recovery Mode documentation

https://wiki.ubuntu.com/RecoveryMode

Wednesday, July 3, 2013

Linux command line too to find public IP

Simply run : wget -qO - icanhazip.com

Friday, March 8, 2013

Delete files older than ‘N’ no of days.

Here I try to delete “.gz” files that are older than ‘n’ days.
However I make sure at least one file retain in the system after delete else no delete .


#!/bin/sh
# Author : SanjeewaF(21-02-2013)
#This file is to delete gzip files afterX no of days.
#Please only run them needed .


export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
CFG_FILE_PATH=/share/backups/reports/
FILE_TYPE=*.gz

NOW=$(date +"%Y-%m-%dT%H%M")
CFG_CUTTOFF_NO_DATES=45

if [ -w "$CFG_FILE_PATH" ]
then
noDeL=`find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn -mtime +$CFG_CUTTOFF_NO_DATES 2>/dev/null | wc -l`
noExt=`find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn 2>/dev/null | wc -l`

#echo "find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn -mtime +$CFG_CUTTOFF_NO_DATES 2>/dev/null | wc -l"
#echo "find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn 2>/dev/null | wc -l"

if [ $noDeL -ne 0 ] && [ $noExt -gt $noDeL ]
then

  echo "Deleting files older than $CFG_CUTTOFF_NO_DATES"
  find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn -mtime +$CFG_CUTTOFF_NO_DATES -print


find $CFG_FILE_PATH$FILE_TYPE -maxdepth 1 -nowarn -mtime +$CFG_CUTTOFF_NO_DATES -print | xargs rm


else
  echo "unable delete all or no files del=$noDeL all=$noExt"
exit 1
fi
else
  echo "unable to find the writable directory $CFG_FILE_PATH"
fi

Tuesday, February 26, 2013

Check who's pinging my server and accessing other ports

sudo tcpdump -nnvXSs 0 -c2 icmp -- ping check
sudo tcpdump port 8080

Friday, February 8, 2013

Redirect ports in Ubuntu using iptables command



 Following code redirect all incoming traffic to port 7001 to 80


 sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 7001 -j REDIRECT --to-port 80

Following code redirect all incoming traffic to port 7001 to 8080

 sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 7002 -j REDIRECT --to-port 8080

Thursday, October 18, 2012

Change default Java version in Linux (Ubuntu)


I have decide to install jdk1.6.0_24 to my Ubuntu server 10.04 LTS and I want to make this as default java in my servere.

Note: if you laready have version you want to make default simple run the step 4 .

  1. Installed above jdk using jdk-6u24-linux-x64.bin in preferred directory
    in my case  : ‘/usr/lib/jvm’
  2. Once you installed I got following directory installed  ‘/usr/lib/jvm/jdk1.6.0_24’
  3. Now run following command :
    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.6.0_24/jre/bin/java" 1
  4. Now time to select which java version you prefer , simple run and select the version
    sudo update-alternatives --config java
      eg :
       Selection    Path                                      Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/jdk1.6.0_24/jre/bin/java      1         manual mode

Press enter to keep the current choice[*], or type selection number: 2
That’s it your default java now set to target version.

  5. Same time you can change $JAVA_HOME and path variable to reflect changes . I prefer global change  
       so I edit /etc/profile and append
       export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_24
       export PATH=$PATH:$JAVA_HOME/bin


That’s it your server now refer to preferred java version .

Wednesday, October 17, 2012

Open Port scaning for linux(Ubuntu)



If you want ever want to find the ports that are open in a target server or network you may use
utility call `nmap` .
eg :
 in Ubuntu       1. Install the nmpa using -- > sudo apt-get install nmap
                       2.port scan for target ip  -- >  nmap -v -sT 192.168.01.98

you may get an output like below . Please use `nmap `Manual page to find more options

Starting Nmap 5.21 ( http://nmap.org ) at 2012-10-17 18:45 PGT
Initiating Ping Scan at 18:45
Scanning 192.168.01.98 [2 ports]
Completed Ping Scan at 18:45, 0.01s elapsed (1 total hosts)
Initiating Parallel DNS resolution of 1 host. at 18:45
Completed Parallel DNS resolution of 1 host. at 18:45, 0.06s elapsed
Initiating Connect Scan at 18:45
Scanning 192.168.01.98 [1000 ports]
Discovered open port 80/tcp on 192.168.01.98
Discovered open port 8080/tcp on 192.168.01.98
Discovered open port 3306/tcp on 192.168.01.98
Discovered open port 445/tcp on 192.168.01.98
Discovered open port 22/tcp on 192.168.01.98
Discovered open port 21/tcp on 192.168.01.98
Discovered open port 139/tcp on 192.168.01.98
Discovered open port 8081/tcp on 192.168.01.98
Discovered open port 8090/tcp on 192.168.01.98
Completed Connect Scan at 18:45, 0.02s elapsed (1000 total ports)
Nmap scan report for 192.168.01.98
Host is up (0.00094s latency).
Not shown: 991 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
22/tcp   open  ssh
80/tcp   open  http
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
3306/tcp open  mysql
8080/tcp open  http-proxy
8081/tcp open  blackice-icecap
8090/tcp open  unknown

Read data files from: /usr/share/nmap
Nmap done: 1 IP address (1 host up) scanned in 0.13 seconds


Wednesday, July 4, 2012

Find large files in the system -Linux

Find large files in the system -Linux:

Navigate to ‘/’ then issue the following command

sudo  du -h  | egrep '([0-9]+(\.)*[0-9]*)G(.*)'

    ·         Command will be in effect from the path you execute . ‘sudo’ is optional
    ·         'G' represent gigabyte  . change it depends on scale you want .

Tuesday, March 20, 2012

One line for loop Linux command

Format : for i in {1..N} ; do  <command>;done;
Eg :
for i in {1..100} ;
do  sleep 2;echo `date`;done;

Installing custom init-scripts

Let’s say you want to start a script every time you start
your linux machine (Ubuntu) and stop 
when shutdown sequence initiate.

Create your script eg : ‘myScript’

1. sudo vim /etc/init.d/myScript              or ‘vi’ to write the script.
2. sudo chmod +x /etc/init.d/myScript                    [to adjust the permission]
3. sudo update-rc.d myScript defaults 98 02  [98 and 02 are the start 
                                 and stop sequence numbers respectively]
 
myScript above accept start and stop as arguments .
Refer your ‘tomcat’ script in /etc/init.d/tomcat if you want to 
know how to accept above arguments (if you dont have stop ,
then it will retry same thing at shutdown). 
my above script eg is for start/stop vpn.
 
Source reference from: https://help.ubuntu.com/community/UbuntuBootupHowto

Wednesday, February 15, 2012

Disable root login in Ubuntu – Avoid unexpected ‘OMG’ problems.


Recently I faced a huge issues in my production environment ,where ppl try to login as root to system which made system more unstable(Yes I know its in mess- super democracy server). As you know Ubuntu came with ‘Sudo’ concept which gives all the privilege’s that you want in systems. There for don not allow root user in your system especially in production. More details can be found in : https://help.ubuntu.com/community/RootSudo

 Two steps to block any root logins :
1.      Disable Root user : 
sudo passwd -dl root

2.  Disable root login in ssh config:
vim /etc/ssh/sshd_config
PermitRootLogin no
And restart sshd -- > /etc/init.d/sshd restart


BTWAY: I was able to recover lost root password from a linux machine. Will update on that in future release .

Monday, February 6, 2012

Detect USB Flash Drivers in linux

Following Script Display possible USB devices attached to the system : Tested on Ubantu 10.04 LTS

#!/bin/sh
#author sanjeewaF

for udi in $(/usr/bin/hal-find-by-capability --capability storage)
do
    device=$(hal-get-property --udi $udi --key block.device)
    vendor=$(hal-get-property --udi $udi --key storage.vendor)
    model=$(hal-get-property --udi $udi --key storage.model)

   if  [ `hal-get-property --udi $udi --key linux.sysfs_path | grep -i usb | wc -l` -gt 0 ]
   then
           echo "Possible USB:"
           parent_udi=$(hal-find-by-property --key block.storage_device --string $udi)
           mount=$(hal-get-property --udi $parent_udi --key volume.mount_point)
           label=$(hal-get-property --udi $parent_udi --key volume.label)
            media_size=$(hal-get-property --udi $udi --key storage.removable.media_size)
           size=$(( media_size/(1000*1000*1000) ))
            printf "$vendor  $model  $device  $mount  $label "${size}GB" \n"
    fi
done

Wednesday, October 26, 2011

Set Date In Ubantu

1 . Set Timezone 2. sudo date --set="26 OCT 2011 19:51:00" 3 You can sync the Hw clock aswell : hwclock -w

Tuesday, August 30, 2011

Tomcat Remote Debug in linux

add folowing inside /etc/default/tomcat6

JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

and restart . tomcat debug is now in port 8787

Monday, August 22, 2011

linux + convert file to Unix format

1. vi filename
2. Esc --> : set fileformat=unix
3.wq!

Wednesday, August 17, 2011

Find system profile in a linux machine - ubantu

$sudo lshw -html > mypc-name.html

Thursday, July 7, 2011

Archive logs and Restart tomcat script .

#!/bin/bash
#author sanjeewa
#Important!! Script Must be run as root(u can ignore this if your tomcat does not need root
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
export PATH
NOW=$(date +"%Y%m-%dT%H%M")
LOG_PATH=/home/abc/logs

log1=/var/log/tomcat6/catalina.out
log2=/var/log/tomcat6/application/error.log
log3=/var/log/tomcat6/application/application.log
netstatlog=/home/abc/logs/netstat.log

if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi


echo `date` ":Tomcat restart script initiated!!"

cd $LOG_PATH
touch $netstatlog
echo "" > $netstatlog

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
echo "tomcat found running"

netstat --tcp > $netstatlog
kill -3 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
/etc/init.d/tomcat6 stop
/etc/init.d/tomcat6 stop

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -gt 0 ];
then
kill -9 `ps ax | grep tomcat6 | grep -v grep | awk '{print $1}'`
fi
tar -cvf $NOW-abc.tomcat.tar $log1 $log2 $log3 $netstatlog
rm $log1
rm $log2
rm $log3

echo "Restarting tomcat !!!!!"
/etc/init.d/tomcat6 start

else
echo "Tomcat not found running"
echo "Restarting tomcat !!!!!"
/etc/init.d/tomcat6 start
fi

if [ `ps -ef | egrep 'tomcat6' | egrep -v egrep | wc -l` -eq 0 ];
then
/etc/init.d/tomcat6 start
fi

#you may use same script for search a exception in logs , if found restart
#[ `grep -R "ABC exception try restarting transaction" $log1 | egrep -v egrep | wc #-l` -gt 0 ];

Thursday, June 2, 2011

Script to restart a remote machine, if certain processers found running.

Eg : Usage can be change to any command (eg : shutdon, sending mails, backup ..)


#!/bin/bash
# author SanjeewaF.
# Script will reastrt the remote server if any matching processors found.
# Process 1 : [abc]

if [ ! $1 ] ; then
echo "Please specify the remote IP";
exit
fi
echo "Checking for xyz Processers ========================== ";
ssh root@$1 "if [ `ps -ef | egrep '(\[\babc\b] )' | egrep -v egrep | wc -l` -gt 0 ]; then echo 'xyz processes were found!! System will Restart!!!!'; shutdown -r now; else echo 'xyz processes were Not found...............'; fi"

Tuesday, March 15, 2011

Shell script to travel through Mysql database tables

TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )

for t in $TABLES
do
echo "checking $t table from $MDB database..."
$MYSQL -u $MUSER -p$MPASS $MDB -e "check table $t"
done

I found it in Net .. might be useful ..

Wednesday, December 1, 2010

easy way of working in SVN brnaches(svn switch)

hmmm ... after while .
You may already know this ..
let say if you need to work on svn branches frequently while working in HEAD revision .

you may do follwoing .

keep separate checkout to branch and you can switch same src to new branch .....

eg :
switch https://abc.jira.com/svn/BTY/branches/BTY_1_1_1 /home/cena/projects/npn/workspace/branch/BTY -r HEAD --force

here im switching src BTY(earlier branch checkout 1_1_0) in .../workspace/branch/BTY to
branch BTY_1_1_1

lem me know if any issues ...
got from one of my friend ..