Saturday, 2 September 2017

Clean %temp% in windows by batch file


Create a batch file with below content and double click it to clean temp folder in windows machine.

@echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

Sunday, 27 August 2017

How to generate random port number

Below is the code to generate the random port number based on the port range configuration in machine level . It will generate a port by calculating ranges define and check if this port is being used in that machine. If port is not in use then it will return that number. This is useful for creating docker containers where you want to generate some random port to expose.

#!/bin/ksh

read LOWERPORT UPPERPORT < /proc/sys/net/ipv4/ip_local_port_range
while :
do
       PORT="`shuf -i $LOWERPORT-$UPPERPORT -n 1`"
        ss -lpn | grep -q ":$PORT " || break
done
echo "Your port is : $PORT"

Sunday, 20 August 2017

Get certificate of any website and add it to keystore


Below is the command to export certificate of any website and import it to local.
Default password for importing is "changeit". To import certificate in Unix it may need root permission as generally java installed with root.

Export :
----------
openssl s_client -connect <host>:443 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > certName.crt


Import :
---------
$JAVA_HOME/bin/keytool -import -alias <alias> -keystore $JAVA_HOME/jre/lib/security/cacerts -file <pathToCert>/certName.crt

How to find all hyperlinks present in a webpage by Unix script



For e.g. If you want to get all the group id present in maven central repository.

https://repo1.maven.org/maven2/log4j/


#!/usr/bin/bash
URL="https://repo1.maven.org/maven2/log4j/"

wget -O - $URL | \
  grep -o '<a href=['"'"'"][^"'"'"']*['"'"'"]' | \
  sed -e 's/^<a href=["'"'"']//' -e 's/["'"'"']$//' >> hyperlink.lst


It will print all the hyperlink present in the webpage .

Output:

https://repo1.maven.org/maven2/log4j/apache-log4j-extras/
https://repo1.maven.org/maven2/log4j/log4j/

Friday, 28 July 2017

Download a directory by wget in UNIX

Download entire directory from Nexus by wget command:

 wget -r -nH --cut-dirs=6 --no-parent --reject="index.html*"  --user=<USER> --ask-password  http://<host>:<port>/nexus/content/repositories/Test-Releases/com/edu/test/

For e.g.  In Nexus there is a folder test and inside that there are different folders with version number.

/test/1.0
/test/1.1

Below are arguments of wget commands.

-r  : Recursive
-nH : Ignore the hostname. If this parameter is not passed then it will create the folder with name of nexus host and port

--cut-dirs : Ignore the number of directories post Nexus host and port. If 5 is passed in this case then in local directories will be like edu/test/*

--reject="index.html*" : Download the actual content but not the index.html

--ask-password  : It will ask for password after running command

Friday, 14 July 2017

Call shell script from Java code

Below is the code to call the shell script from Java code. This will call the shell script and wait for it's execution and print the output.

public static void main(String[] args) throws Exception {
        try {
                String param1=args[0];
               
                String scriptName = "/root/script.ksh";
                String commands[] = new String[]{scriptName,param1};

                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec(commands);
                proc.waitFor();
                StringBuffer output = new StringBuffer();
                BufferedReader reader = new BufferedReader(new       InputStreamReader(proc.getInputStream()));
                String line = "";                      
                while ((line = reader.readLine())!= null) {
                        output.append(line + "\n");
                }
                System.out.println("===> " + output);
        } catch (Exception e) {
                e.printStackTrace();
        }

Clone/Checkout specific file/folder from GIT

By default git checkouts the entire repository to local. As it used to compress the data during storage and checkout so it takes very less time. But if you want to checkout only one file then use below command.

git archive --remote=ssh://git@host:port/project_key/repos/repoName.git HEAD <fileName/FolderName> | tar -x

Maven command to download and deploy to Nexus from command line


This will deploy any artifacts from command line without pom.xml. Here repositoryId will be the id defined in settings.xml.

mvn deploy:deploy-file -Dfile=file_FullPath
-DgroupId=*** -DartifactId=*** -Dversion=***
-Dpackaging=jar   -Durl={repo_url} -DrepositoryId=releases


This command will download the artifacts from Nexus via command line.

mvn -C org.apache.maven.plugins:maven-dependency-plugin:2.4:get
-DremoteRepositories=http://<host:port>/nexus/content/repositories/Test/
-DgroupId=com.edu.test -DartifactId=testArtifact-Dversion=1.0 -Dpackaging=jar

rsync with compress option

This is the fastest way to copy data from one server to another.It will compress the data and transfer .

1) rsync -avxH ~/source_dir/  username@remote_host:destination_directory

This will sync data under source_dir to destination_directory

2) rsync -avxH ~/source_dir  username@remote_host:destination_directory

This will create a directory source_dir under destination_directory and copy data under source_dir


In case of local server

rsync -avxH /source /destination

Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...