UNIX: How to Automate SSH Login

Yesterday, I was working on some shell script piece for my project and I came across a situation where I have to copy some files from one server to another using ‘scp’ command and also some time have to execute some command on other server.  One requirement was we don’t need to pass the password of functional id inside the script.

User should be able to login to other server without password using command  “ssh -l username remotehost”.

For this I used SSH public key based authentication as below:-

Continue reading “UNIX: How to Automate SSH Login”

Count lines of multiple similar files in unix

In continuation to my earlier post regarding concatenation similar files into one. I am writing this post to demonstrate counting lines of similar kind of files in a given directory and sub-directory.

Below command will find all the logs with given name pattern inside the directory including sub directories and then WC will count it’s lines and display on screen in total.

Continue reading “Count lines of multiple similar files in unix”

Concatenate/merge/join Multiple Text Files in Windows

Last night I got many log files from 8 different servers, all the servers have date wise log files. I separated out similar logs and kept in a folder, now as per requirement team needs the one type of log in consolidated manner means “USAGE.log” of all days and servers in one USAGE-Consildated.log file.

I was searching for some utility on Google but not get any. Later I thought about DOS Batch command to do the same but a simple copy command did it for me.

Continue reading “Concatenate/merge/join Multiple Text Files in Windows”

How to read Blue Dump’s (BSOD) Minidump file

From couple of weeks in my brother’s machine he was getting Blue Dump screen with message “DRIVER_IRQL_NOT_LESS_OR_EQUAL”. Today to investigate I asked him to pass me the minidump files. Minidump are file that windows write for debugging purpose at below location:

%SystemRoot%\Minidump

in the above folder you will find files with ext .dmp
if you open this file with notepad or any other editor you will not be able to read it’s content.

Continue reading “How to read Blue Dump’s (BSOD) Minidump file”

How To get ServletContext inside Filter

I was coding last night and I have to get some information from Manifest file in filters. In order to read the Manifest I need the ServletContext. Using below code you can get the ServletContext inside Filter.

public class SomeFilter implements Filter {
FilterConfig config;

public void setFilterConfig(FilterConfig config) {
this.config = config;
}

public FilterConfig getFilterConfig() {
return config;
}

public void init(FilterConfig config) throws ServletException {
setFilterConfig(config);
}

// doFilter and destroy methods…
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {

ServletContext context = getFilterConfig().getServletContext();
Manifest mManifest=new Manifest();
System.out.println(“Manifest “+mManifest.getBuildInfo(context));

}
}

Java Return Value to Unix Shell Script

Today I faced a situation where I need to verify something though Java class in database . My problem was I have to do this inside unix shell script. I created the below solution to achieve that, I am posting here sample working code:

JAVA CLASS

public class Sample
{

public static void main(String args[])
{
//Do the complete logic and print the result using system out
System.out.println(“Sample Testing”);    
}
}

Shell Script

Continue reading “Java Return Value to Unix Shell Script”

How to send an email from UNIX using mailx.


#!/bin/sh
#
# Purpose: Demonstrate how to send an email from UNIX using mailx.
############################################################

# Example 1 – Simple:
echo “This is the body.”| mailx -s “mailx Test1” sample@sample.com

# Example 2 – Using Variables:
SUBJECT=”mailx Test2″
EMAIL_ADDRESS=”sample@sample.com
BODY=”This is the body of the message.”

echo “$BODY” | mailx -s “$SUBJECT” “$EMAIL_ADDRESS”

Continue reading “How to send an email from UNIX using mailx.”

When we need HttpSessionBindingListener?

The HttpSessionBindingListener interface is implemented when an object needs to be notified if it's being bound to a session or unbound from a session.

Objects implement this interface so that they can be notified when they are being bound or unbound from a HttpSession. When a binding occurs (using HttpSession.setAttribute()) HttpSessionBindingEvent communicates the event and identifies the session into which the object is bound. Similarly, when an unbinding occurs (using HttpSession.removeAttribute()) HttpSessionBindingEvent communicates the event and identifies the session from which the object is unbound.

This interface has two methods, that are notified when the status of the object has changed:

  • public void valueBound(HttpSessionBindingEvent event) : Notifies the object that it is being bound to a session and identifies the session.
  • public void valueUnbound(HttpSessionBindingEvent event) : Notifies the object that it is being unbound from a session and identifies the session.

Note, this listener will not be declared in the deployment descriptor as the same as HttpSessionActivationListener interface. The container at runtime will introspect this object to see if it implements the HttpSessionActivationListener and/or HttpSessionBindingListener and fires appropriate events to the object.

These methods have a HttpSessionBindingEvent parameter that can be used to retrieve the session that the object was bound to and the name it was given in the session.

Class HttpSessionBindingEvent

Events of this type are either sent to an object that implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session.

The session binds the object by a call to HttpSession.setAttribute and unbinds the object by a call to HttpSession.removeAttribute.

The methods of this object that are used to get the name that's assigned to the object, the session it's bound to, and the actual object:

  • public String getName() : Returns the name with which the attribute is bound to or unbound from the session.
  • public Object getValue() : Returns the value of the attribute that has been added, removed or replaced. If the attribute was added (or bound), this is the value of the attribute. If the attrubute was removed (or unbound), this is the value of the removed attribute. If the attribute was replaced, this is the old value of the attribute.
  • public HttpSession getSession() : Return the HttpSession that the object was bound to or unbound from.

Delete .svn directories recursively

I am here showing an example of a unix command, which recursively deletes subversion .svn folders. Subversion is a widely used open-source revision control application. Every copy of source code received from subversion repository has .svn folders, which store metadata. However, if you want to use or distribute source code, these .svn folder are often not necessary.

To remove this we can follow the following steps:

We use find command to find all .svn folders beginning from current directory.

$ find . -type d -name .svn ./.svn ./sourceA/.svn ./sourceB/.svn ./sourceB/module/.svn ./sourceC/.svn

Now pass this to RM command using grave accent quotes (key (`) to left of ‘1’)

$ rm -rf `find . -type d -name .svn`

If you need to remove the .svn very frequently then simply save this command inside a script and execute that whenever is required.

Hope this helps 🙂