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/

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...