Thursday, April 10, 2014

Wednesday, December 18, 2013

Shell Scripting - referrence

-nt : Newer than
-ot : Older than
-gt : Greater than
-lt : Less than
-ge : Greater than or equal to
-le : less than or equal to 
-eq : equal to 
-ne : not equal to 

check if the name "abc" is file or not
test -f abc; echo $? - if o/p is 0 then its a file else its a directory

also, 
[-f abc]; echo $?

to check socket files eg mysql.sock
test -S /var/lib/mysql/mysql.sock; echo $? - if o/p is 0 then its socket file else not

To check null (zero size) files
test -s log1.log; echo $? - if o/p is 0 then its not a zero byte file.

man test - for all details 

NTP Server restart

/etc/init.d/ntp start
/etc/init.d/ntp stop
/etc/init.d/ntp restart 

Different ways of identifying whoe is logged on linux server

$ w
23:04:27 up 29 days,  7:51,  3 users,  load average: 0.04, 0.06, 0.02
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
ramesh   pts/0    dev-db-server        22:57    8.00s  0.05s  0.01s sshd: ramesh [priv]
jason    pts/1    dev-db-server        23:01    2:53   0.01s  0.01s -bash
john     pts/2    dev-db-server        23:04    0.00s  0.00s  0.00s w

$ w -h
ramesh   pts/0    dev-db-server        22:57   17:43   2.52s  0.01s sshd: ramesh [priv]
jason    pts/1    dev-db-server        23:01   20:28   0.01s  0.01s -bash
john     pts/2    dev-db-server        23:04    0.00s  0.03s  0.00s w -h

$ w -u 23:22:06 up 29 days,  8:08,  3 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
ramesh   pts/0    dev-db-server        22:57   17:47   2.52s  2.49s top
jason    pts/1    dev-db-server        23:01   20:32   0.01s  0.01s -bash
john     pts/2    dev-db-server        23:04    0.00s  0.03s  0.00s w -u

$ w -s
23:22:10 up 29 days,  8:08,  3 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM               IDLE WHAT
ramesh   pts/0    dev-db-server        17:51  sshd: ramesh [priv]
jason    pts/1    dev-db-server        20:36  -bash
john     pts/2    dev-db-server         1.00s w -s

2. Get the user name and process of logged in user using who and users command

who command is used to get the list of the usernames who are currently logged in. Output of the who command contains the following columns: user name, tty number, date and time, machine address.
$ who
ramesh pts/0        2009-03-28 22:57 (dev-db-server)
jason  pts/1        2009-03-28 23:01 (dev-db-server)
john   pts/2        2009-03-28 23:04 (dev-db-server)
To get a list of all usernames that are currently logged in, use the following:
$ who | cut -d’ ‘ -f1 | sort | uniq
john
jason
ramesh

Users Command

users command is used to print the user name who are all currently logged in the current host. It is one of the command don’t have any option other than help and version. If the user using, ‘n’ number of terminals, the user name will shown in ‘n’ number of time in the output.
$ users
john jason ramesh

3. Get the username you are currently logged in using whoami

whoami command is used to print the loggedin user name.
$ whoami
john
whoami command gives the same output as id -un as shown below:
$ id -un
john
who am i command will display the logged-in user name and current tty details. The output of this command contains the following columns: logged-in user name, tty name, current time with date and ip-address from where this users initiated the connection.
$ who am i
john     pts/2        2009-03-28 23:04 (dev-db-server)

$ who mom likes
john     pts/2        2009-03-28 23:04 (dev-db-server)

Warning: Don’t try “who mom hates” command.
Also, if you do su to some other user, this command will give the information about the logged in user name details.

4. Get the user login history at any time

last command will give login history for a specific username. If we don’t give any argument for this command, it will list login history for all users. By default this information will read from /var/log/wtmp file. The output of this command contains the following columns:
  • User name
  • Tty device number
  • Login date and time
  • Logout time
  • Total working time
$ last jason
jason   pts/0        dev-db-server   Fri Mar 27 22:57   still logged in
jason   pts/0        dev-db-server   Fri Mar 27 22:09 - 22:54  (00:45)
jason   pts/0        dev-db-server   Wed Mar 25 19:58 - 22:26  (02:28)
jason   pts/1        dev-db-server   Mon Mar 16 20:10 - 21:44  (01:33)
jason   pts/0        192.168.201.11  Fri Mar 13 08:35 - 16:46  (08:11)
jason   pts/1        192.168.201.12  Thu Mar 12 09:03 - 09:19  (00:15)
jason   pts/0        dev-db-server   Wed Mar 11 20:11 - 20:50  (00:39

Monday, February 11, 2013

Clone Permission


List all the files with ownership and full path :

find $PWD -type d  -ls

List all the directories with ownership and full path :

find $PWD -type f  -ls

List all the permissions with full path recursively :

find ${PWD} -exec stat -c' %a %n' {} \;

 ==================================================================
Script to Change Files

Usage : this script and file name
Script.sh filename

#! /bin/bash
i=0
t=`cat $1 | wc -l`
while [ $i -lt $t ]
        do
                i=$[$i + 1]
                read line
                user=`echo $line | awk '{print $5}'`
                group=`echo $line | awk '{print $6}'`
                file=`echo $line | awk '{print $NF}'`
                #echo $user
                #echo $group
                #echo $file
                chown $user:$group $file
                if [ $? -eq 0 ] ; then
                        echo File  $line is updated >> /tmp/updatedFiles.txt
                        else
                        echo File $line is failed >> /tmp/updatedFilesFailed.txt
                fi
        done < $1
#END

==================================================================

Script to ChangeDir

Usage : this script and file name
Script.sh filename

#! /bin/bash
i=0
t=`cat $1 | wc -l`
while [ $i -lt $t ]
        do
                i=$[$i + 1]
                read line
                user=`echo $line | awk '{print $5}'`
                group=`echo $line | awk '{print $6}'`
                file=`echo $line | awk '{print $NF}'`
#               echo $user
#               echo $group
#               echo $file
                chown $user:$group $file
                if [ $? -eq 0 ] ; then
                        echo Directory  $line is updated >> /tmp/updatedDirs.txt
                        else
                        echo Directory $line is failed >> /tmp/updatedDirsFailed.txt
                fi
        done < $1
#END

==================================================================

Changes permission for all the files :

Script to Change Files

Usage : this script and file name
Script.sh filename

#! /bin/bash
i=0
t=`cat $1 | wc -l`
while [ $i -lt $t ]
        do
                i=$[$i + 1]
                read line
                permission=`echo $line | awk '{print $1}'`
                file=`echo $line | awk '{print $NF}'`
                #echo $permission
                #echo $file
                chmod $permission $file
                if [ $? -eq 0 ] ; then
                        echo Permission  $line is updated >> /tmp/updatedPermission.txt
                        else
                        echo Permission $line is failed >> /tmp/updatedPermissionFailed.txt
                fi
        done < $1
#END


Wednesday, September 12, 2012

Adding software repository in SuSE 12.2

In YaST / Software / Software Repositories / Add URL then:
Uncheck:Download repository description Files.

For Non-Oss add URL: "http://download.opensuse.org/distribution/12.2/repo/non-oss/", Named: openSUSE-12.2-Non-Oss. 

For Oss add URL: "http://download.opensuse.org/distribution/12.2/repo/oss/" , Named: openSUSE-12.2-Oss.

Thursday, May 26, 2011

Add cronjob using shell script

have your script ...

a. crontab -l > $tmpfile
b. edit $tmpfile
c. crontab $tmpfile
d. rm $tmpfile

Saturday, February 19, 2011

Increase trial period of windows 2008 server - rearm

Sometimes we setup server for learning platform, software. Since license cost of all products are so high we cannot afford and so we use trial version.
To extend trial version Windows 2008 Server by executing below command as Administrator on server.

1. Start > Run > type "cmd"
2. Right click cmd, Run as Administrator
3. Execute on command prompt : slmgr.vbs -dli --- this will show the number of days left with trial version.
4. Execute on command prompt slmgr.vbs -rearm
5. Restart Server
6. Repeat step 1,2,3 to check new date of expiration of trial version.

Monday, January 18, 2010

Crontab for other user

crontab -l -u cruise
crontab -e -u cruise

Kill all the process by a user in Linux

kill -9 `ps -ef|grep cruise |awk '{print $2}'`

Monday, January 4, 2010

Last Restart time for Windows workstation

To find out last reboot

Open command prompt and type below command. Second line of the output will display the last restart of workstation/server.

net statistics workstation | more

eg

Workstation Statistics for \\"workstation name"
Statistics since 12/24/2009 4:27 AM

Bytes received 3192754767

Last restart for Windows

To find out last reboot

Open command prompt and type below command. Second Line of the output will display the last start time of the workstation/server.

net statistics workstation | more

Sunday, November 15, 2009

telnet in Windows 7 and Windows Vista

telnet command is by default missing in Windows 7 and Windows Vista.
To solve this, just enable it. Click Start > Control Panel > Programs, and then click Turn Windows Features on or off. Fromn the list, scroll down and select/check Telnet Client. Click OK to start the installation.

After completion of installationy, try telnet using command line. It will work :)

Monday, November 9, 2009

alt_disk_install command

By default, using the alt_disk_install command does the following:
1. Creates an /image.data file based on the current rootvg's configuration. A
customized image.data file can be used.
2. Creates an alternate rootvg (altinst_rootvg).
3. Creates logical volumes and file systems with the alt_inst prefix.
4. Generates a backup file list from the rootvg, and if an exclude.list file is given,
those files are excluded from the list.
5. Copies the final list to the altinst_rootvg's file systems.
6. If specified, the installp command installs updates, fixes, or new filesets into
the alternate file system.
7. The bosboot command creates a boot logical volume on the alternate boot
disk.
8. If a customization script is specified, it runs at this point.
9. The file systems are then unmounted, and the logical volumes and file
systems are renamed.
10.The logical volume definitions are exported from the system to avoid
confusion with identical ODM names, but the altinst_rootvg definition is left as
an ODM placeholder.
11.By default, the bootlist is set to the new cloned rootvg for the next reboot.

Tuesday, March 24, 2009

Apache restart with Sudo Permission - Issue

Unfortunately, this issue ate my 5 days to fix this issue.
and the resolution was very very small compare to time take by it.

Issue :
While restarting apache with sudo access it was throwing some exception, while same with root was working fine.

Exception :
$ sudo /etc/init.d/apache restart
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable LD_LIBRARY_PATH was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_BINDIR was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JAVA_ROOT was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JDK_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JRE_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable JRUBY_HOME was undefined
[Tue Mar 17 17:01:45 2009] [warn] PassEnv variable ORACLE_HOME was undefined
Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) [Tue Mar 17 17:01:47 2009] [warn] PassEnv variable LD_LIBRARY_PATH was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_BINDIR was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JAVA_ROOT was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JDK_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JRE_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable JRUBY_HOME was undefined
[Tue Mar 17 17:01:47 2009] [warn] PassEnv variable ORACLE_HOME was undefined
done

Resolution :

Since apache is not able to find above environment variable of user shell, its throwing the exception, so to restart the apache use option -i ( to preserver exception)

The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the user that the command is being run as. The comâmand name argument given to the shell begins with a - to tell the shell to run as a login shell. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving TERM unchanged, setting HOME, SHELL, USER, LOGNAME, and PATH, and unsetting all other environment variables. Note that because the shell to use is determined before the sudoers file is parsed, a runas_default setting in sudoers will specify the user to run the shell as but will not affect which shell is actually run.

Make sure you have to execute the below command, if you are facing issue while executing this add " (root) NOPASSWD: /bin/bash /etc/init.d/apache2 *" in /etc/sudoers file

$ sudo -i /etc/init.d/apache restart

Syntax OK
Shutting down httpd2 (waiting for all children to terminate) done
Starting httpd2 (prefork) done

WOW !!

Wednesday, March 11, 2009

Change user password expiry in linux

chage - change user password expiry information

SYNOPSIS
chage [-D binddn] [-P path] [-m mindays]
[-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] user

chage -l [user]

DESCRIPTION
chage is used to list and change the password expiry information of a user. It allows the system administrator to change the number of days
between allowed and required password changes and the date of the last password change. It allows also to define when an account will expire. The
chage command is restricted to the system administrator, except for the -l option, which may be used by an user to determine when his password or
account is due to expire.

If no option is given, chage operates in an interactive mode, prompting the user with the current values for all of the fields. Enter the new
value to change the field, or leave the line blank to use the current value. If the users exists in the local passwd file, but not in the local
shadow file, chage will create a new entry in the shadow file.

Tuesday, March 10, 2009

Find installation date for Windows Server

This seems to be challenging to find installation date for installation date for Windows server.


On Windows Server (works on Windows XP too)

On command prompt, execute : systeminfo | find /i "install date"

Tuesday, February 3, 2009

Windows SID change for 64 bit, 2003 server

On Windows Server 2003, 64 bit Server and Windows 2008 Server , it might give issue to change the server name and modify accordingly in registry. In case NEWSID command is not working on server, binaries and instructions are available on page : http://technet.microsoft.com/en-us/sysinternals/bb897418.aspx. In case this portal is unavailable click to download the installer.