Archive

Archive for the ‘Tools’ Category

Bash Epoch Converter

July 10, 2014 Leave a comment

I use the online epoch converter on an almost daily basis with an almost single usage – converting a value of seconds/milliseconds since the epoch (1/1/1970 00:00:00) to a human readable date.
I wanted something quicker which I can use from my Terminal window without – so I created a Bash function that performs that:

epoc usage

epoc usage


The function

epoc ()
{
    [[ $# == 0 ]] && SECONDS_SINCE_EPOCH=$(date +%s);
    [[ "$1" == "-h" ]] && printf "$(tput bold)$(tput setaf 1)Usage:$(tput sgr0) $(tput bold) epoc [<seconds-since-epoch|milli-seconds-since-epoch>]$(tput sgr0)\n$(tput bold)$(tput setaf 2)Examples:$(tput sgr0)\n\t$(tput bold)epoc 1404382131$(tput sgr0)\n\t$(tput bold)epoc 1404305405000\n" && return 1;
    [[ $# == 1 ]] && SECONDS_SINCE_EPOCH=$1;
    [[ $(printf ${SECONDS_SINCE_EPOCH} | wc -c) -gt 10 ]] && let SECONDS_SINCE_EPOCH=${SECONDS_SINCE_EPOCH}/1000;
    printf "\n$(tput bold)$(tput bold)$(tput setaf 2)%-20s\t| %-35s\t| %-35s$(tput sgr0)\n" "Seconds Since Epoch" "Time (Local)" "Time (GMT)";
    printf "%-20s\t| %-35s\t| %-35s\n\n" ${SECONDS_SINCE_EPOCH} "$(date -r ${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%z)' | sed 's/(+\([0-9][0-9]\)/(+\1:/')" "$(export TZ=GMT; date -r ${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%z)' | sed 's/(+\([0-9][0-9]\)/(+\1:/')"
}
epoc ()
{
    [[ $# == 0 ]] && SECONDS_SINCE_EPOCH=$(date +%s);
    [[ "$1" == "-h" ]] && printf "$(tput bold)$(tput setaf 1)Usage:$(tput sgr0) $(tput bold) epoc [<seconds-since-epoch|milli-seconds-since-epoch>]$(tput sgr0)\n$(tput bold)$(tput setaf 2)Examples:$(tput sgr0)\n\t$(tput bold)epoc 1404382131$(tput sgr0)\n\t$(tput bold) epoc 1404305405000\n" && return 1;
    [[ $# == 1 ]] && SECONDS_SINCE_EPOCH=$1;
    [[ $(printf ${SECONDS_SINCE_EPOCH} | wc -c) -gt 10 ]] && let SECONDS_SINCE_EPOCH=${SECONDS_SINCE_EPOCH}/1000;
    printf "\n$(tput bold)$(tput bold)$(tput setaf 2)%-20s\t| %-35s\t| %-35s$(tput sgr0)\n" "Seconds Since Epoch" "Time (Local)" "Time (GMT)";
    printf "%-20s\t| %-35s\t| %-35s\n\n" ${SECONDS_SINCE_EPOCH} "$(date -d @${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%:z)')" "$(export TZ=GMT; date -d @${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%:z)')"
}

One liners:

# UNIX, Mac OSx and FreeBSD
function epoc() { [[ $# == 0 ]] && SECONDS_SINCE_EPOCH=$(date +%s); [[ "$1" == "-h" ]] && printf "$(tput bold)$(tput setaf 1)Usage:$(tput sgr0) $(tput bold) epoc [<seconds-since-epoch|milli-seconds-since-epoch>]$(tput sgr0)\n$(tput bold)$(tput setaf 2)Examples:$(tput sgr0)\n\t$(tput bold)epoc 1404382131$(tput sgr0)\n\t$(tput bold)epoc 1404305405000\n" && return 1; [[ $# == 1 ]] && SECONDS_SINCE_EPOCH=$1; [[ $(printf ${SECONDS_SINCE_EPOCH} | wc -c) -gt 10 ]] && let SECONDS_SINCE_EPOCH=${SECONDS_SINCE_EPOCH}/1000; printf "\n$(tput bold)$(tput bold)$(tput setaf 2)%-20s\t| %-35s\t| %-35s$(tput sgr0)\n" "Seconds Since Epoch" "Time (Local)" "Time (GMT)"; printf "%-20s\t| %-35s\t| %-35s\n\n" ${SECONDS_SINCE_EPOCH} "$(date -r ${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%z)' | sed 's/(+\([0-9][0-9]\)/(+\1:/')" "$(export TZ=GMT; date -r ${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%z)' | sed 's/(+\([0-9][0-9]\)/(+\1:/')" ; }
# Linux
function epoc() { [[ $# == 0 ]] && SECONDS_SINCE_EPOCH=$(date +%s); [[ "$1" == "-h" ]] && printf "$(tput bold)$(tput setaf 1)Usage:$(tput sgr0) $(tput bold) epoc [<seconds-since-epoch|milli-seconds-since-epoch>]$(tput sgr0)\n$(tput bold)$(tput setaf 2)Examples:$(tput sgr0)\n\t$(tput bold)epoc 1404382131$(tput sgr0)\n\t$(tput bold) epoc 1404305405000\n" && return 1; [[ $# == 1 ]] && SECONDS_SINCE_EPOCH=$1; [[ $(printf ${SECONDS_SINCE_EPOCH} | wc -c) -gt 10 ]] && let SECONDS_SINCE_EPOCH=${SECONDS_SINCE_EPOCH}/1000; printf "\n$(tput bold)$(tput bold)$(tput setaf 2)%-20s\t| %-35s\t| %-35s$(tput sgr0)\n" "Seconds Since Epoch" "Time (Local)" "Time (GMT)"; printf "%-20s\t| %-35s\t| %-35s\n\n" ${SECONDS_SINCE_EPOCH} "$(date -d @${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%:z)')" "$(export TZ=GMT; date -d @${SECONDS_SINCE_EPOCH} '+%d-%h-%Y %H:%M:%S %Z (%:z)')" ; }

Personalizing your connection to QA/Production environments with SecureCRT

January 6, 2014 Leave a comment

The trouble with production/QA environments is that you can rarely change them to fit you personal preferences, and usually you use a common user to access them.
If you are working on Windows and using SecureCRT to connect to Linux/Unix machines (and if not – ask yourself why …) you can personalize any connection to QA/Production environments by telling SecureCRT to perform automatic login activities – this without effecting how the machine behaves for other users.
By personalization I am referring to the definition of your personal aliases etc.

How to do it:

  1. Download this VBScript to your computer, and place it somewhere (e.g. C:\Utils\SecureCRT\Scripts)
  2. Find a connection you want to effect in SecureCRT and open it’s properties
  3. Navigate to Connection -> Logon Actions
  4. Check the Logon script checkbox and type the path to the file defined at the beginning (e.g. C:\Utils\SecureCRT\Scripts\EnvironmentPersonalizer.vbs)

Every time you login – SecureCRT will run that script.

What the script does:
The way this scripts works is that it waits for something to happen on the screen and then sends commands.
Example:

objTab.Screen.WaitForString("$")   // Wait for the prompt
objTab.Screen.Send "bash" & vbcr   // Switch to a BASH shell
objTab.Screen.Send "clear" & vbcr   // clear the screen
objTab.Screen.Send "alias ll='ls -lart'" & vbcr   // Alias 'll' to be 'ls –lart' 

You can find a few more utility scripts on my Github Repository – https://github.com/ronkitay/SecureCRT-Environment-Personalization

Note: Take great care when effecting anything on production environments – don’t do anything that will effect how the environment behaves for other people (like changing the .bash_profile for example)

Categories: Shell Scripting, Tools, UNIX Tags:

Automatically setting permanent Environment Variables on Windows

August 26, 2013 Leave a comment

More than once I needed to define a permanent Environment Variable on a Windows machine – mostly as part of some automatic installation process. E.g. – like the Java installer sets the JAVA_HOME during installation.

Windows has a very simple utility to do so – SETX.

# Set the variable HELLO with the value "WORLD" for the current user
SETX HELLO WORLD
# Set the variable HELLO with the value "WORLD" for all users (System)
SETX HELLO WORLD /m

Note that SETX is different from SET – invoking SETX as part of a shell script will not make the variable available for that shell script.

Reading material
Official Documentation – http://technet.microsoft.com/en-us/library/cc755104.aspx
Non-Official Documentation – http://ss64.com/nt/setx.html

Chrome’s Search Engines

June 10, 2013 Leave a comment

A good way to increase effectivity when browsing is to use Google Chrome’s “Search Engines” functionality.
This gives you a way to quickly search within sites that have search options, or to navigate to to sites where you know their structure.

How to do it? three steps:

  1. Click on the ‘Settings’ button. Chrome_Search_Engine_1
  2. Choose ‘Manage Search Engines’ Chrome_Search_Engine_2
  3. Scroll down until you see editable text boxes and give 3 values for your search engine:Chrome_Search_Engine_3

Example from life: Let’s say you work a lot on a Unix/Linux environment and need to man a lot of commands. You can view these man pages online with much better detail and examples on this site:
http://ss64.com/bash/.
Every page on this site has the following pattern: http://ss64.com/bash/command.html
All you need to do is define a new search engine like this:

  1. Name: UNIX Man Pages
  2. Alias: man
  3. URL: http://ss64.com/bash/%s.html

That way when you type “man less” – you will see this: Chrome_Search_Engine_4
Pressing enter will lead you to the requested page:
Chrome_Search_Engine_5

Teaching Ditto to paste into SecureCRT

May 13, 2013 Leave a comment

I use Ditto and SecureCRT constantly on my day to day work, but by default – they don’t work well together.

Ditto supports being configured for specific applications to work in a different manner – all you need to do is to update the registry in the correct way.

How to do it?

Automatically

  1. Save the below into a file named Ditto_SecureCRT.reg
  2. Execute the file and choose Yes when prompted
Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Ditto\PasteStrings]
"SecureCRT.exe"="+{INS}"

Manually

  1. Open the registry editor (Start -> Run -> regedit)
  2. Navigate to HKEY_CURRENT_USER\Software\Ditto
  3. If the Key PasteStrings does not exist, create it: Right-Click on Ditto -> New -> Key -> Name it as ‘PasteStrings’
  4. Add a new value for SecureCRT:
    1. Right-Click on PasteStrings -> New -> String Value -> Name it as ‘SecureCRT.exe’
    2. Double click on the new value, and give Value Data the value +{INS}

Either way you do it – the change is immediate, no need for a restart.

Categories: Tools Tags: , ,

Quick decompile of Java code

April 29, 2013 2 comments

If you need to quickly decompile a java .class file and you only have access to a machine with the JDK installed on it (like production servers) – you can use a tool which is built-in with the JDK.

It is called javap and it can disassemble any .class file. All you need to do is run it from the directory where all of your the .class files exist (the “root”).
It will also work if the class can be found within the CLASSPATH (i.e. the environment variable CLASSPATH) and it can be given a specific classpath via a CLI argument. It can also find the .class file within a JAR.

javap <FQCN> Usage Gets the list of all the public members (variables + methods).
Only gives you method declerations.
Example javap java.lang.String
javap -private <FQCN> Usage Gets the list of all the members (variables + methods).
Only gives you method declerations.
Example javap -private java.lang.String
javap -c <FQCN> Usage Same as javap <FQCN>, but also disassembles the method body.
This does not “decompile” – but you can understand what will happen in simple methods.
Example javap -c java.lang.String
javap -c -private <FQCN> Usage Same as above but does it for all private members as well.
Example javap -c -private java.lang.String

FQCN: Fully Qualified Class Name.

How does it look? Take for example this piece of code:

package com.ron;

import java.lang.*;

public class JavapSample
{
        private String someMember;

        public JavapSample(int someArgument)
        {
                someMember = String.valueOf(someArgument);
        }

        public void doSomething()
        {
                System.out.println(someMember);
        }

        private void doSomethingSecret()
        {
                System.out.println("Secret ...");
        }
}

Here is the output:

public class com.ron.JavapSample extends java.lang.Object{
    public com.ron.JavapSample(int);
    public void doSomething();
}
public class com.ron.JavapSample extends java.lang.Object{
    private java.lang.String someMember;
    public com.ron.JavapSample(int);
    public void doSomething();
    private void doSomethingSecret();
}
public class com.ron.JavapSample extends java.lang.Object{
private java.lang.String someMember;

public com.ron.JavapSample(int);
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   iload_1
   6:   invokestatic    #2; //Method java/lang/String.valueOf:(I)Ljava/lang/String;
   9:   putfield        #3; //Field someMember:Ljava/lang/String;
   12:  return

public void doSomething();
  Code:
   0:   getstatic       #4; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   aload_0
   4:   getfield        #3; //Field someMember:Ljava/lang/String;
   7:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   10:  return

private void doSomethingSecret();
  Code:
   0:   getstatic       #4; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #6; //String Secret ...
   5:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return
}

Where/When to use it – I use it when I need to ensure a certain fix is available within a JAR on a QA/Prod environment. It is not intrusive – and it is rather easy to understand if the fix is there or not.

Note for Java7 users: In java 7 it looks like javap can receive a .class file as an argument instead of the FQCN.

Categories: Java, Tools Tags: , ,

Jar scanning utility – find a class within all jars (Unix/Linux)

March 28, 2013 Leave a comment

Every so often I need to figure out which jar(s) contain a certain class so I can figure out why an application acts the way it does – either since a class is missing in some environment or since the wrong class was loaded into the classpath.

When that happens, I use the following script. I saw it once in a company I worked at, and then again in another company.
I modified it slightly, but the credits do not go to me, they go to the anonymous person who wrote the initial code.

The script is available for download on GitHub.

Or you can simply copy it from here:

#!/bin/tcsh -f

if ( $#argv < 2 ) then
        printf "Scans all the JARs in the specified directory for classes matching the specified string\n"
        printf "Usage: $0 <dir> <class-name-pattern>\n"
        printf "Example: $0 . Exception\n"
        printf 'Example: '$0' $JAVA_HOME/lib "com\/sun\/.*action"\n'
        exit 1
endif

set DIR = "$1"
set CLASS = "$2"

foreach J ( `find $DIR -name "*.jar"` )
        printf "."
        set res=`jar tvf $J | grep "$CLASS" | sed 's/$/;/'`
        set outputsize = `echo $res|wc -c`
        if ( $outputsize > 1 )  then
                printf "\nMatch found in [$J]\n"
                printf  "$res\n" | tr ';' '\n'
        endif
end

printf "\n"
Categories: Java, Shell Scripting, Tools, UNIX Tags: , , , ,
%d bloggers like this: