Monday, August 24, 2020

Shell Scripting for Beginners

 A shell script is a file containing series of commands .The shell reads this file and run each command as if like it entered on the command line.On a Linux Operating System there are multiple Interpreters which will execute the commands that we pass to them . Default is Bash and this is widely available on various operating systems . Other 's are KSH,SH . Some commands may different on there interpreters but majority will support by all.

1.What is shell Scripting
2.Variables
3.File Manipulations
4.Common Iterations/Loops
5.Useful System Variables
6.Test Operators
7.Numeric Tests
8.String tests
9.Logical Tests
10.Argument Variables
11.Some Advance commands ,utilities ,network and file handling commands

1.What is shell Scripting

  In short and simple a shell script is a file containing series of commands .The shell reads this file and run each command as if like it entered on the command line.
  The benefit of this is to simplify the day to day mundane activities . Shell is a scripting language interpreter.
  Scripts unlock the power of our Linux machine. imagine you have 2 files on with 100 words and another with 100000 words and you need to check first file words on second file and print the word existence and no of times present .
  This is where the power of scripting comes .Once you write a simple script then start using it and enhance further to fit into day to day activities .
#!/bin/bash
echo "Hello World"

First line #!/bin/bash is the interpreter which will execute the commands . Next to that are the commands . echo will print the values in the screen .

2.Variables

 Variable is a keyword to store some values . We have some System predefined variable and user can also define variables in the scripts to store the results of commands .
 Example :
 PATH is the OS define variable .run echo $PATH to print the values 
 VALUE=`date` && echo $VALUE --> here VALUE is user defined variable and it exists till the script finishes .Once finished VALUE variable no more exist.

3.File Manipulations

Below table gives the basic file manipulation operation help full while handling the files.

File CommandsExplaination
> fileCreate/Overwrite file
>> fileAppend to file
>file 2>&1redirect both output and error to file
< fileread from file
file1 | file1pipe output of file1 as input to file2

4.Common Iterations/Loops

Below given blocks are very important because in the scrips for each command that we use we need to check something before executing and store the results some where and iterate through some repetitive steps .

read text file line by line . in many scenarios we need to read file line by line and apply the logic .In the below we are reading and printing with echo.

while read line 
do 
  echo "Line is $line
done < file 

Find matching lines

grep foo file --> print the lines that has foo matching word
egrep 'foo|bar' file -->find multiple word and print the matching lines
grep -i FOO file --> same as above but i for ignore case
grep -v foo file --> -v used to print not matching lines 

Get the output of command to a variable

FILELIST=`ls`
COUNTOFFILES=`ls -lrt |wc -l`

Case is a good way to Iterate avoiding multiple if/elif blocks

case.sh
#!/bin/bash
# case example
 $1 means first argument in the script
# Usage ./case.sh argument
case $1 in
	start)
		echo starting
	;;
	stop)
		echo stoping
	;;
	restart)
		echo restarting
	;;
	*)
		echo don\'t know
	;;
esac

Function declaration and calling ...

multiply() {
	expr $1 \*2
	}
multiply 3 

For loop iterates the in the list of give values and run the logic

for i in 1 2 3 4 5 
do
	echo "In $i loop"
	echo "Do some logic here "
done	

5.Useful System Variables

$? --> what the shell returned or previous command return status
0 means success
other value means failure
This $? verification is very useful to check the status of previous command to take next decession
$* --> all arguements
./abc.sh 1 2 --> in this $* is 3
$# --> No of the variable values . where $0 is name of script , $1 is 1st arguement ,$2 is 2nd arguement

6.Test Operators

# Compare 2 variable and do something 
if [ "$x" -lt "$y" ] ; then
 # Do something
 fi

7.Numeric Tests

Numeric Test operationsExplaination
ltless than
gtgreate than
eqequal
nenot equal
gegreate or equal
leless than or equal

8.String tests

File Test operationsExplaination
ntnewer than
dis a directory
fis a file
rreadable
wwritable
xexecutable

9.Logical & String Tests

Logical & String TestsExplaination
=equal to
zzero lenth
nnot zero length
&&Logical AND
||logical OR
!logical Not

10.Argument Variables

Argument VariablesExplaination
$0Program name or script name
$11st argument
$22nd arguments
$99th argument
$*all aguements
$#No of Arguments

11.Some Advance commands ,utilities ,network and file handling commands

below are some of the commands that we daily use for the day-to-day activities.

Linux CommandsUsage
command1 ||Command2run command1; if it fails, run Command2
command1 && Command2run command1; if it works, run Command2
command1 ; Command2keeping 2 commands on the same line
ls -lStlist files biggest last
ls -lrtlist files newest last
ls -alshow all fils including hidden
sort -nsort numarically
wget URLdownload url
read xread some value from user /keyboard
touch filecreate empty file
cmd |tee file.txtcommand output to stdout also to file.txt
ifconfig -alist all network interfaces .can see ip here
netstat -rshow routers
netstat -tnpl |grep -I listenshows all listening ports on the server
ssh u@hostlogin to host as user u
scp file.txt u@host:\tmpcopy file.txt to host /tmp/ wit u user
alias l='ls -lrt'creating alias to a command
df -hshow dis mount points
find . -type f -name a.txtfind a.txt in current directory
find . -type f -name *.txt -printfind all the txt files
find /foo -type d -lslist all directories under foo
awk -F":" '{ print $1 " " $NF}'print file value in each line of the file delimted with : and NF for last value
tar -cvf abc.tar a.txt b.txtcreate acrchive file
tar -tvf abc.tarcheck the list of file in abc.tar
tar -rvf abc.tar c.txtadd c.txt to existing abc.tar
tar -xvf abc.tarextract abc.tar file
tar -zcvf my_archive.tar.gz *create zip and then tar
tar -zxvf my_archive.tar.gz *extract zip tar
zcatview the file without decompressing it like cat
ps -ef |grep keywordgrep some process running
kill -9 pidkill process with pid
pwdpresent working dir
cdGo to user home
cd ..Go to previous directory
hosnamehostname of the server

Hope the blog gives some useful information for starting writing scripts ...

Monday, July 6, 2020

Linux find and replace

 

There is a requirement to find and replace a string from file's in the in the directory name across all the files and directories in the given directory.

We already know some basic commands like mv,sed/perl & find commands . We will use these basic commands to write a script and get the expected result .

basic commands 

mv
mv ABC DEF  -> to rename a directory
find
find /dir -type f -exec grep -l "ABC" {} \; | xargs perl -pi -e 's/ABC/DEF/g' 
Above command is used find and replace ABC in all the files with DEF on /dir 
sed
sed 's/ABC/DEF/g' -->To replace a string in a word 
perl
perl -pi -e 's#ABC#DEF#g' file --> this searches and replaces ABC to DEF .

We combine this in to 2 scenarison .

  1. To find and replace the string in given file .
  2. To find and replace the string in the directy recursivley along with matched sub directory string .

On the below we have ABC directory with abc.txt and ACBC directory . Test results for both scenarios 1 and 2 given below .

Scenario 1 :

Scenario2 :

1.sh

#!/bin/bash
echo "Enter file Name"
read fileName
echo "Enter findstring "
read findstring
echo "Enter replacestring "
read replacestring
if [ -f $fileName ] ; then
 perl -pi -e 's#'$findstring'#'$replacestring'#g' $fileName
 echo "$findstring replace with $replacestring "
 else
 echo "Given file is not available "
fi

2.sh

#!/bin/bash
echo "Enter Dir Name"
read dir 
echo "Enter findstring "
read findstring
echo "Enter replacestring "
read replacestring
if [ -d $dir ] ; then
 find $dir -type f -exec grep -l "$findstring" {} \; | xargs perl -pi -e 's/'$findstring'/'$replacestring'/g' 
 echo " Word $findstring replaced with $replacestring successfully ..."
 for directory in `find $dir -type d -name "$findstring"` 
  do
    newDirName=`echo $directory |sed 's/'$findstring'/'$replacestring'/g'`
	mv $directory $newDirName
	echo " $directory name changed to $newDirName "
  done
 else
  echo "Directory not found : $dir "
fi

set -x and set +x are for debugging . Hope this article helps in detail for find and replace . ..

Wednesday, June 24, 2020

SSL Setup in JBOSS Using Elythron

Follow below steps to setup SSL using Elythron on JBOSS EAP 7.X 

1.Create JKS file and generate self signed certificate

keytool -genkey -alias jboss -keysize 2048 -validity 365 -keyalg RSA -sigalg SHA256withRSA -keystore jboss.jks -storepass jboss@123 -keypass jboss@123 -dname "CN=example.com, OU=blog, O=AskMiddlewareExpert.com, C=IN"

Configure a keystore

/host=master/subsystem=elytron/key-store=httpsKS:add(path="${jboss.home.dir}/ssl/jboss.jks", credential-reference={clear-text=jboss@123}, type=JKS)
2.Connect JBOSS Cli mode to configure keystore, key-manager and ssl-context in Elytron

Create a new key-manager

/host=master/subsystem=elytron/key-manager=httpsKM:add(key-store=httpsKS,algorithm="SunX509",credential-reference={clear-text=jboss@123})

Configure new server-ssl-context reference with protocol and ciphers

/host=master/subsystem=elytron/server-ssl-context=httpsSSC:add(key-manager=httpsKM,protocols=["TLSv1.2"], cipher-suite-filter="TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA")

Run the above 3 CLI commands to make changes within profile eg. full-ha in domain.xml as below:

/profile=full-ha/subsystem=elytron/key-store=httpsKS:add(path="/home/sshriram/EAP7.1/7.1/LDAP/jboss-eap-7.1/domain/configuration/jboss.jks", credential-reference={clear-text=jboss@123}, type=JKS)

/profile=full-ha/subsystem=elytron/key-manager=httpsKM:add(key-store=httpsKS,algorithm="SunX509",credential-reference={clear-text=jboss@123})

/profile=full-ha/subsystem=elytron/server-ssl-context=httpsSSC:add(key-manager=httpsKM,protocols=["TLSv1.2"], cipher-suite-filter="TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA")

3.Configure undertow to map ssl-context of Elytron

[domain@localhost:9990 /] batch
[domain@localhost:9990 / #] /profile=full-ha/subsystem=undertow/server=default-server/https-listener=https:undefine-attribute(name=security-realm)
[domain@localhost:9990 / #] /profile=full-ha/subsystem=undertow/server=default-server/https-listener=https:write-attribute(name=ssl-context,value=httpsSSC)
[domain@localhost:9990 / #] run-batch

4) If you want management-interface to use the same ssl-context, execute the following command which will enable SSL in management-interface

[domain@localhost:9990 /] /host=master/core-service=management/management-interface=http-interface:write-attribute(name=ssl-context, value=httpsSSC)
[domain@localhost:9990 /] /host=master/core-service=management/management-interface=http-interface:write-attribute(name=secure-port,value=8443)

Reload the servers to make the change effective.

reload --host=master

5.Restart Jboss and verify https url's reflected with the self-signed certificate that we generated .

We can also enable SSL in the traditional way . And the content of xml file loos like below .standalone.xml (or host.xml for domain) 

<security-realms>
    <security-realm name="CertificateRealm">
        <server-identities>
            <ssl>
                <keystore path="/path/to/keystore.jks" keystore-password="secret" alias="servercert"/>
            </ssl>
        </server-identities>
        <authentication>
            <truststore path="/path/to/truststore.jks" keystore-password="secret"/>
        </authentication>
    </security-realm>
</security-realms>
<subsystem xmlns="urn:jboss:domain:undertow:3.1">
    <buffer-cache name="default"/>
    <server name="default-server">
        <http-listener name="default" socket-binding="http" redirect-socket="https"/>
        <https-listener name="https" secure="true" enabled-protocols="TLSv1.1,TLSv1.2" security-realm="CertificateRealm" socket-binding="https"/>
...

Under profile undertow CertificateRealm will be mappted to https https-listener.

https name is refered in the port interface .

Monday, May 11, 2020

How to clear WebSphere tmp files

 It is better to always remove temp and wstemp when there is any update with application or re-deploy the application . But safer to take backup of the complete profile before making any deletion .

profile_root//temp and profile_root//wstemp these are the location of the temporary files in Websphere Application server .

WebSphere Application Server uses multiple temporary locations for many reasons. This post explains the most commonly used temporary files, why they are used and when they can be removed. This blog will also explain the files and directories that can be removed under the profile direction with caution.

Important:

  • Be careful in deleting any temporary, cache and log files in WebSphere Application Server!
     
  • Before making any changes to the environment , take a backup of the profile. It can be a tape backup, using the backupconfig tool, or using the manageprofile -backupProfile option.

    profile_root example: C:/WebSphere/AppServer/profiles/profile_name
    install_root example: C:/WebSphere/Appserver

Let's describe the different files and their locations:

  • profile_root/wstemp
    Usage: wstemp is a workspace temporary directory. Any changes that you make to the configuration are stored in the wstemp directory temporarily. For example, if you are changing the heap size for an application server, the change is stored in the wstemp location until you save the changes. The concept is same for any administrative client, such as the Integrated solution console, wsadmin or JMX, that you use to make the changes.

    Caution: The WebSphere Application Server administrative console stores a preferences.xml file in install_root/wstemp/<workspace_id>. This file contains user preferences on administrative console layout and actions. It is created when you log onto the administrative console. If you remove this file, you lose the user preferences; however,  the preferences can be created again the next time you log onto the administrative console.

    Do not delete the wstemp files when the server is running (especially deployment manager or node agent servers). This approach can cause unexpected results. Also, do not delete the files when you are unsure about the changes that you made to the configuration. Save any pending changes, stop the deployment manager or node agent, which depends on whether you are removing the dmgr wstemp or node wstemp, and then delete the wstemp files.

    Why remove these files?: Files in the profile_root/wstemp directory can be removed. Restart the server process after removal. Because the directory is  used by multiple clients, some times you might see multiple files and subdirectories left behind in this directory. For example, when you use the ConfigService MBean to make changes to configuration and you do not discard the session in the code, this directory will never get deleted. Another reason is corruption in the workspace. Corruption can happen when multiple users make changes to the same configuration at the same time.
     
  • profile_root/temp
    Usage: The temp directory is used by multiple WebSphere components. Two good examples are compiled Java ServerPages (JSP) files and web service cache files. Compiled JSP class files (servlets) are stored in this location. The directory might get regenerated when you invoke the JSP again. However, you might experience a performance issue when you invoke the JSP for the very first time after the JSP compiled files have been removed.

    Caution:  Be cautious if you have a web services application deployed on the node. The wscache.xml is generated during the deployment process and stored under the temp directory. You have to redeploy the web service application to generate the wscache.xml again. You may experience some performance issue with large and complicate webservices application

    Why remove these files?: Corrupted JSP files or any non-root permission issues might cause the server start up issue.

Never delete any other files or directories for WebSphere Application Server unless otherwise directed by the IBM Support team.

Clear Weblogic temp/cache files

It is better to always remove tmp and cache when there is any update with application or re-deploy the application . But safer to take backup of the complete directory before making any deletion .

Recently when working with new application update I found that just bouncing/ restarting server is not enough, we will also need to clean up the cache so that new changes take effect. We will see how to clean temporary directories in Weblogic .

Find out the Domain directory under which our Managed instance folder's exist . Some customised Weblogic domain existing on a different location .If you dont know the location then on the Weblogic installation location find domain_registry.xml file . In this file you can see the list of Domain's created and it's paths .

Each managed server have its own tmp and cache directory .

--> Shut down Server.

--> Delete the contents of the folder ORACLE_HOME/user_projects/domains/your_domain/servers/your_server/tmp…

You can also delete ORACLE_HOME/user_projects/domains/your_domain/servers/your_server/cache

-->Restart Server.

You can also do something like this . In your startup script you can add delete statements so that every time the server restarted new tmp and cache will be re-created .

Note: Do not delete any other directories under the server . If you deleted then restoration is difficult and lead to startup issues .

Thursday, May 7, 2020

Add Ciphers and Protocols in Apache httpd server

 

Add listed Ciphers using SSLCipherSuite directive in Apache HTTPD inside SSL virtual host as below :

~~~~~~~

SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1<br>

SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256

~~~~~~~

- SSL 3.0 and TLS 1.0 are susceptible to known attacks on the protocol; they are disabled entirely.

- Disabling TLS 1.1 is (as of August 2016) mostly optional; TLS 1.2 provides stronger encryption options, but 1.1 is not yet known to be broken. Disabling 1.1 may mitigate attacks against some broken TLS implementations.


In addition, you can use SSL server which accepts strong encryption only by adding following directives too 

~~~~~~~

SSLHonorCipherOrder on

SSLCompression      off

SSLSessionTickets   off

~~~~~~~

- Enabling SSLHonorCipherOrder ensures that the server's cipher preferences are followed instead of the client's.

- Disabling SSLCompression prevents TLS compression oracle attacks (e.g. CRIME).

- Disabling SSLSessionTickets ensures Perfect Forward Secrecy is not compromised if the server is not restarted regularly.


More details about the SSL directives can be found in <a href="https://httpd.apache.org/docs/2.4/mod/mod_ssl.html">ApacheModSSL</a>

Sunday, May 3, 2020

How to reset Weblogic Security

 There will a situation that the admin account is locked or no back up id to unlock or reset the admin id or completely forget the password then we may need to reset Weblogic Admin security.

WindowsLinux/AIX/Solaris
Assuming Domain path : cd D:\ABC\ABCDomain\Assuming Domain path : /u01/ABC/ABCDomain/bin
cd D:\ABC\ABCDomain\bin and run below commandcd /u01/ABC/ABCDomain/bin and run below command
setDomainEnv.cmd. ./setDomainEnv.cmd [ dot then space ]
cd D:\ABC\ABCDomain\security\cd /u01/ABC/ABCDomain/security
move DefaultAuthenticatorInit.ldift [ Rename the file ]mv DefaultAuthenticatorInit.ldift DefaultAuthenticatorInit.ldift_old
java weblogic.security.utils.AdminAccount weblogic welcome1 .java weblogic.security.utils.AdminAccount weblogic welcome1 .
cd D:\ABC\ABCDomain\servers\AdminServer\security\cd /u01/ABC/ABCDomain/servers/AdminServer/security
Move boot.properties and create new boot.properties with below username and passwordMove boot.properties and create new boot.properties with below username and password
username=weblogic
password=welcome1
username=weblogic
password=welcome1
got to D:\ABC\ABCDomain
move servers\AdminServer\data\ldap servers\AdminServer\data\ldap_bkp
go to /u01/ABC/ABCDomain/
mv servers/AdminServer/data/ldap servers/AdminServer/data/ldap_old
Now restart the Admin server and verify logs and Admin login with new credentials.. it will create fresh ldap directory .Now restart the Admin server and verify logs and Admin login with new credentials.. it will create fresh ldap directory .
Once AdminServer is started successfully you can see the boot.properties file userid and password is encryptedOnce AdminServer is started successfully you can see the boot.properties file userid and password is encrypted
copy boot.protperites file to all the other managed server's under this domain and restartcopy boot.protperites file to all the other managed server's under this domain and restart
Good Luck!!!Good Luck!!!

If there are any local additional id's created before just recreate them all the local id's are lost due to resetting of admin security.

Saturday, April 11, 2020

Password less Authentication SSH/SCP

 As a part of OS user authentication there is a possibility to exchange the keys between ID's on same or different OS so that they can communicate without password . it is call password less authentication . Using this one can login to the server with our any "Entering password" / do file transfer from the script where we don't require to Enter password .

In this Post we will use same Server with 2 ID's . We will see how to connect one to another with password then will see how we can configure password less authentication .

In the organisation we may have server to server authentication mostly . some time with in the same server between multiple id's it may need to exchange for ease of day to day operations .

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from host A / user source to Host B / user destination. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to Create a New User in Linux

To create a new user account, invoke the useradd command followed by the name of the user.

For example to create a new user named username you would run:

sudo useradd username
[root@ip-172-31-14-154 ~]# useradd source
[root@ip-172-31-14-154 ~]# id source
uid=1002(source) gid=1003(source) groups=1003(source)
[root@ip-172-31-14-154 ~]# 
[root@ip-172-31-14-154 ~]# 
[root@ip-172-31-14-154 ~]# useradd destination
[root@ip-172-31-14-154 ~]# id destination
uid=1003(destination) gid=1004(destination) groups=1004(destination)

Set some password for each of the source and destination id's using passwd userid command

root@ip-172-31-14-154 destination]# passwd source
Changing password for user source.
New password: 
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@ip-172-31-14-154 destination]# 
[root@ip-172-31-14-154 destination]# 
[root@ip-172-31-14-154 destination]# passwd destination
Changing password for user destination.
New password: 
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@ip-172-31-14-154 destination]# 

password of source is zaq12wsx and password of destination is mko09ijn

Let us see how we can do ssh from source to destination . Some OS will have tectia where sshg3 and scpg3 are available instead of ssh and scp 
[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154
The authenticity of host 'ip-172-31-14-154 (172.31.14.154)' can't be established.
ECDSA key fingerprint is SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ip-172-31-14-154,172.31.14.154' (ECDSA) to the list of known hosts.
destination@ip-172-31-14-154: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[source@ip-172-31-14-154 ~]$

Update /etc/ssh/sshd_config with PasswordAuthentication yes if it was no and then restart sshd service to get rid of the above error 

 [root@ip-172-31-14-154 destination]# grep -i PasswordAuthentication /etc/ssh/sshd_config 
 #PasswordAuthentication yes
 PasswordAuthentication yes 
 # PasswordAuthentication.  Depending on your PAM configuration,
 # PAM authentication, then enable this but set PasswordAuthentication
 [root@ip-172-31-14-154 destination]# systemctl restart sshd
 [root@ip-172-31-14-154 destination]#  

SSH from Source to Destination . Now it will ask to enter destination id password

[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154
destination@ip-172-31-14-154's password: 
[destination@ip-172-31-14-154 ~]$ 

With Password we are able to authenticate from source to destination id successfully . Now we will see how we can make this password less using the public and private keys of the id's .

Let's do the password authentication by generating a pair of public and private keys of id's and exchange for authentication using below command

ssh-keygen -t rsa -b 4096

bit size can be 2048,1024,3072 or 4098 or any other bit size that supports

Run the command for source id

source@ip-172-31-14-154 ~]$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/source/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/source/.ssh/id_rsa.
Your public key has been saved in /home/source/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA source@ip-172-31-14-154.us-east-2.compute.internal
The key's randomart image is:
+---[RSA 4096]----+
|               +.|
|       . o    o.+|
|        * .  .o*.|
|         B o .=oo|
|        S = o  +o|
|       + + o .o.+|
|      . o o  E++.|
|       .   .oo*B |
|          ..oBO+.|
+----[SHA256]-----+
[source@ip-172-31-14-154 ~]$ cd .ssh/
[source@ip-172-31-14-154 .ssh]$ ls -lrt
total 12
-rw-r--r--. 1 source source  192 May  2 04:49 known_hosts
-rw-r--r--. 1 source source  776 May  2 05:04 id_rsa.pub
-rw-------. 1 source source 3422 May  2 05:04 id_rsa
[source@ip-172-31-14-154 .ssh]$ 

Run the same command for destination id

[destination@ip-172-31-14-154 ~]$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/destination/.ssh/id_rsa): 
Created directory '/home/destination/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/destination/.ssh/id_rsa.
Your public key has been saved in /home/destination/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:V8THYedkP6waQjWcxK+4uFITKsk6ul7gXbqW6m+KDO8 destination@ip-172-31-14-154.us-east-2.compute.internal
The key's randomart image is:
+---[RSA 4096]----+
|          +=o.o.+|
|          .=o.+=.|
|         .  o. oo|
|       ..  . .. .|
| .. ... S.o...   |
|. o+o. o o..o    |
|...+o . o ..     |
|++.+.. . .       |
|BOEo  ...        |
+----[SHA256]-----+
[destination@ip-172-31-14-154 ~]$ 

ssh-keygen command will create 2 files one id_rsa [ Private key ] and id_rsa.pub [ public key ]

Note : For Source id to connect to Destination then source public key need to upload to destination 

When i try to connect still asking password . so trying to un on debug mode vith -vvv
[source@ip-172-31-14-154 .ssh]$ ssh destination@ip-172-31-14-154 -vvv
OpenSSH_8.0p1, OpenSSL 1.1.1g FIPS  21 Apr 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: /etc/ssh/ssh_config line 52: Including file /etc/ssh/ssh_config.d/05-redhat.conf depth 0
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug2: checking match for 'final all' host ip-172-31-14-154 originally ip-172-31-14-154
debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 3: not matched 'final'
debug2: match not found
debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 5: Including file /etc/crypto-policies/back-ends/openssh.config depth 1 (parse only)
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug3: gss kex names ok: [gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-,gss-gex-sha1-,gss-group14-sha1-]
debug3: kex names ok: [curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1]
debug1: configuration requests final Match pass
debug1: re-parsing configuration
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: /etc/ssh/ssh_config line 52: Including file /etc/ssh/ssh_config.d/05-redhat.conf depth 0
debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
debug2: checking match for 'final all' host ip-172-31-14-154 originally ip-172-31-14-154
debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 3: matched 'final'
debug2: match found
debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 5: Including file /etc/crypto-policies/back-ends/openssh.config depth 1
debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
debug3: gss kex names ok: [gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-,gss-gex-sha1-,gss-group14-sha1-]
debug3: kex names ok: [curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1]
debug2: resolving "ip-172-31-14-154" port 22
debug2: ssh_connect_direct
debug1: Connecting to ip-172-31-14-154 [172.31.14.154] port 22.
debug1: Connection established.
debug1: identity file /home/source/.ssh/id_rsa type 0
debug1: identity file /home/source/.ssh/id_rsa-cert type -1
debug1: identity file /home/source/.ssh/id_dsa type -1
debug1: identity file /home/source/.ssh/id_dsa-cert type -1
debug1: identity file /home/source/.ssh/id_ecdsa type -1
debug1: identity file /home/source/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/source/.ssh/id_ed25519 type -1
debug1: identity file /home/source/.ssh/id_ed25519-cert type -1
debug1: identity file /home/source/.ssh/id_xmss type -1
debug1: identity file /home/source/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.0
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0
debug1: match: OpenSSH_8.0 pat OpenSSH* compat 0x04000000
debug2: fd 4 setting O_NONBLOCK
debug1: Authenticating to ip-172-31-14-154:22 as 'destination'
debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from ip-172-31-14-154
debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent
debug3: receive packet: type 20
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc
debug2: ciphers stoc: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc
debug2: MACs ctos: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
debug2: MACs stoc: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
debug2: compression ctos: none,zlib@openssh.com,zlib
debug2: compression stoc: none,zlib@openssh.com,zlib
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug2: peer server KEXINIT proposal
debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1
debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc
debug2: ciphers stoc: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc
debug2: MACs ctos: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
debug2: MACs stoc: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512
debug2: compression ctos: none,zlib@openssh.com
debug2: compression stoc: none,zlib@openssh.com
debug2: languages ctos: 
debug2: languages stoc: 
debug2: first_kex_follows 0 
debug2: reserved 0 
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug1: kex: curve25519-sha256 need=32 dh_need=32
debug3: send packet: type 30
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug3: receive packet: type 31
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM
debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from ip-172-31-14-154
debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1
debug3: load_hostkeys: loaded 1 keys from 172.31.14.154
debug1: Host 'ip-172-31-14-154' is known and matches the ECDSA host key.
debug1: Found key in /home/source/.ssh/known_hosts:1
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey out after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug3: receive packet: type 21
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 4294967296 blocks
debug1: Will attempt key: /home/source/.ssh/id_rsa RSA SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA
debug1: Will attempt key: /home/source/.ssh/id_dsa 
debug1: Will attempt key: /home/source/.ssh/id_ecdsa 
debug1: Will attempt key: /home/source/.ssh/id_ed25519 
debug1: Will attempt key: /home/source/.ssh/id_xmss 
debug2: pubkey_prepare: done
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug3: receive packet: type 6
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug3: send packet: type 50
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic,password
debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup gssapi-with-mic
debug3: remaining preferred: publickey,keyboard-interactive,password
debug3: authmethod_is_enabled gssapi-with-mic
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KCM:)


debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KCM:)


debug2: we did not send a packet, disable method
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /home/source/.ssh/id_rsa RSA SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA
debug3: send packet: type 50
debug2: we sent a publickey packet, wait for reply
debug3: receive packet: type 51
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/source/.ssh/id_dsa
debug3: no such identity: /home/source/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/source/.ssh/id_ecdsa
debug3: no such identity: /home/source/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/source/.ssh/id_ed25519
debug3: no such identity: /home/source/.ssh/id_ed25519: No such file or directory
debug1: Trying private key: /home/source/.ssh/id_xmss
debug3: no such identity: /home/source/.ssh/id_xmss: No such file or directory
debug2: we did not send a packet, disable method
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
destination@ip-172-31-14-154's password: 
chmod 0600 /home/your_home/.ssh/authorized_keys

After that go to /etc/ssh/sshd_config
PubkeyAuthentication yes
systemctl restart sshd
Source Public key is updated on the destination authorized keys . Now try 

destination@ip-172-31-14-154 .ssh]$ cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCqLtvCpcoVJi5NmVNEwgf1+RyNvpvVF5iIKGRAZYZZJv18qeOx5Rm+cIhw3xPmqrEDQrxOJmSwC5Z+CRVO0BHNf8uvln1c6ES+4QevfmKgmTFMy5oIpfyr+00AUHEBAvxlb/y0slvg0LvdP/f5S2UW97AkjioHcb0YUUHGchUuCgePKt/HbVlomoTfQdlf7gm2QejynWIADD6KgOHCqxP4rJuYe9sp3nxC/ViwIqSOsGKHi8bCKf1g8OU9M57A4vlwaORnUKZ8v2mJeXUQkkGJI4cfVfv2tS/Y9jETyBjDR4/m46Kb0que08Swe0pMouOJFK4pXMjNLXOLAiCpIwmtzkiq6Q9G5Qw5FDCo6AiFRXv1IQNCDqXcXRDfhMh6C81u969Xpk+uWi6tYOXIV4cib+aIEU295GXOAEcSbJKujhmXuF6FHUUsHeK1+0etaB29XPjbkYvOHdsbnBwspS0tKp+Siba1+HutKn3kuZGn9HJWdVn83zBh8FIjMIKtb69S9P2zgprM1Y/1M+Bo0yRMxZwTY/FPHx0BXrGIGCzR2qhWeNk/w/N1qkG3nUkDFfX4Lh1IvrCQjHuZwH88JBVsSC7MqFoMlfd7D+1f6q+oQ73ARtdCMwEo2irmUbv5rpnFNFn84MdIhk6UlDs8xhjYAOmhfDKTEVyyATjGIN5r3Q== source@ip-172-31-14-154.us-east-2.compute.internal
[destination@ip-172-31-14-154 .ssh]$ ls -lrt
total 12
-rw-r--r--. 1 destination destination  781 May  2 05:05 id_rsa.pub
-rw-------. 1 destination destination 3434 May  2 05:05 id_rsa
-rw-------. 1 destination destination  776 May  2 05:45 authorized_keys
[destination@ip-172-31-14-154 .ssh]$ 
[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154
Last login: Sun May  2 05:45:50 2021 from 172.31.14.154
[destination@ip-172-31-14-154 ~]$ 

YESSSS Successful after a couple of issues . Now source is able to connect to destination without password . scp also now works without password authentication

[source@ip-172-31-14-154 ~]$ scp /tmp/1 destination@ip-172-31-14-154:/home/destination
1                                                                                                                                                                     100%    0     0.0KB/s   00:00    
[source@ip-172-31-14-154 ~]$ 
[source@ip-172-31-14-154 ~]$ 

Now we can do vice versa . Means upload destination public key [.pub file ] to source and configure in authorized_keys then destination will be able to connect to source without password .Lets do it quickly .

source@ip-172-31-14-154 ~]$ scp destination@ip-172-31-14-154:/home/destination/.ssh/id_rsa.pub .
id_rsa.pub                                                                                                                                                                                               100%  781   686.2KB/s   00:00    
[source@ip-172-31-14-154 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[source@ip-172-31-14-154 ~]$ 
[destination@ip-172-31-14-154 ~]$ ssh source@ip-172-31-14-154
The authenticity of host 'ip-172-31-14-154 (172.31.14.154)' can't be established.
ECDSA key fingerprint is SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ip-172-31-14-154,172.31.14.154' (ECDSA) to the list of known hosts.
source@ip-172-31-14-154's password: 
Last login: Sun May  2 05:44:10 2021
[source@ip-172-31-14-154 ~]$ 
[source@ip-172-31-14-154 ~]$ 
[source@ip-172-31-14-154 ~]$ ls -lrt
total 4
-rw-r--r--. 1 source source 781 May  2 05:54 id_rsa.pub
[source@ip-172-31-14-154 ~]$

[source@ip-172-31-14-154 .ssh]$ ls -lrt

total 20

-rw-r--r--. 1 source source  776 May  2 05:04 id_rsa.pub

-rw-------. 1 source source 3422 May  2 05:04 id_rsa

-rw-r--r--. 1 source source  776 May  2 05:14 destination@localhost

-rw-r--r--. 1 source source  363 May  2 05:15 known_hosts

-rw-rw-r--. 1 source source  781 May  2 05:55 authorized_keys

[source@ip-172-31-14-154 .ssh]$ chmod 600 authorized_keys 

[source@ip-172-31-14-154 .ssh]$ cat authorized_keys 

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnTQ1Wba93oW7t+Em/X1MgxTSfKD7+7bv6GOntukBGCNchFsj6AeoSIvXP0JqvtX596uTHvZVVA3bMjVPDnf6Q36S8sWuJerFmY6Qn7MkCsmcOBN91ImQUXaLPLP1P+2NNST4IU/YNA6KpBnLkD6PZv74glkkhEHMDIHXjymAlOQqHFzIUYNqCkFDbT39LadcGVeU0yw/AXNwn9URQ/V8TGGvSYvejUlW4qqvsJc8HyI3ChTG+VBHTX/XwYACUAcDXCevfTx3YYWFhSqu342tb3CtRVk6fnaVgPbs7PeRLKdB2U9+rCSSI2R/0RPHdxuArVfzVRXKLtCyb1BgD5+672p13MEybVzzKFIhafp3+rMP77oR2OCoNE6LEfcTG+h9HL+/z1m/9JTDfnWvoCHFCHUP0QV0ZuVhNjLPRKvP/1E5ygH7YgFX/ZAzmTHVPSC8rmh3FNjS0NGP3YElNIWGcI1Dit8ZLaUHlaWmIgUi3AE6vgFPSGkk8wLAslqhEG+zkLW8mc6sMko6y8/xAie3LKELpSbTRSk/lNZA72qw7809KWbnZ+hOXXequai+jCjTgaHQi+5oYFf2GMRVJIVvW4WIWMJqe9+U0ygZDwJB0AMPkz7m5FErk/8hJT/zc/0f3lFwQJ/9rOHux/GT9IyFknduQuauNWz5MxnrK5wNs7w== destination@ip-172-31-14-154.us-east-2.compute.internal

[source@ip-172-31-14-154 .ssh]$ 

Finally we are able to connect to both id's vice versa without password . SCP also now can without password authentication .

[source@ip-172-31-14-154 .ssh]$ ssh destination@ip-172-31-14-154
Last login: Sun May  2 05:56:42 2021 from 172.31.14.154
[destination@ip-172-31-14-154 ~]$ ssh source@ip-172-31-14-154
Last login: Sun May  2 05:57:09 2021 from 172.31.14.154
[source@ip-172-31-14-154 ~]$ 

If SSH Tectia installed then sshg3 instead of ssh and scpg3 instead of scp tools available . rest of the steps remain same .

Wednesday, February 26, 2020

awk utility for shell script

 

awk works on programs that contain rules comprised of patterns and actions. The action is executed on the text that matches the pattern. Patterns are enclosed in curly braces ({}). Together, a pattern and an action form a rule. The entire awk program is enclosed in single quotes (').

Here is the output of -h

Perhaps i don’t need all the output I want only mounted file systems to print and it size let say.

df -h |grep -v Filesystem |awk '{ print $2 " " $NF }'

In the above we are ignoring first description line using grep –v. Then printing required values .Infact we can print as many values as we want . NF holds last values .

$0 represents entire line 
$1 represents the first field 
$2 represent second field
$7 represent 7th field
$NF represents the last record.

In the previous command if we don’t specify delimiter then space is the default .  so df –h delimited with space then we are printing the values.Also we used “ “ space when printing .If we use , then space used while printing.

df -h |grep -v Filesystem |awk '{ print $2 , $NF }'

Let’s see different delimiter “:” with simple echo passing to awk

echo “ABC:DEF:XYZ” |awk –F”:” ‘{ print $1,$2,$NF }’

In this both NF and $3 are having same value .

Another example just want to print the date in Mon DD YYYY format

date | awk '{print $2,$3,$6}'

OFS --> Output field Separator

date | awk 'OFS="-" {print$2,$3,$6}'

The BEGIN and END Rules

A BEGIN rule is executed once before any text processing starts. In fact, it’s executed before awk even reads any text. An END rule is executed after all processing has completed. You can have multiple BEGIN and END rules, and they’ll execute in order.

awk 'BEGIN {print "File Systems"} {print $NF}' /tmp/dfout.txt

Adding pattern’s or Conditions

We can also add patterns before printing with AWK . In the below example we will check if the 3rd value greater or equal 1000 the print those rows or fields on those rows .

Combining both BEGIN and patterns on awk

In case if we want to develop our own logic using awk follow below

The first line of the script tells the shell which executable to use

#!/usr/bin/awk -f

BEGIN {
  # set the input and output field separators
  FS=":"
  OFS=":"
  # zero the accounts counter
  accounts=0
}
{
  # set field 2 to nothing
  $2=""
  # print the entire line
  print $0
  # count another account
  accounts++
}
END {
  # print the results
  print accounts " accounts.\n"
}

Hope this article help to use awk for data processing with advance utility ...

Tuesday, February 25, 2020

Keytool commands for Certificate provision and management

 

Follow below steps to get SSL certificate for an JAVA based Application server .In the below steps we will use keytool which is availale in JAVA_HOME/bin/

Create a new keystore keystore.jks for managing your public/private key pairs and certificates.

Note : -v option is for detailed output

Keytool help for the commands 
[wlsuser@localhost tmp]$ keytool
Key and Certificate Management Tool

Commands:

 -certreq            Generates a certificate request
 -changealias        Changes an entry's alias
 -delete             Deletes an entry
 -exportcert         Exports certificate
 -genkeypair         Generates a key pair
 -genseckey          Generates a secret key
 -gencert            Generates certificate from a certificate request
 -importcert         Imports a certificate or a certificate chain
 -importpass         Imports a password
 -importkeystore     Imports one or all entries from another keystore
 -keypasswd          Changes the key password of an entry
 -list               Lists entries in a keystore
 -printcert          Prints the content of a certificate
 -printcertreq       Prints the content of a certificate request
 -printcrl           Prints the content of a CRL file
 -storepasswd        Changes the store password of a keystore

Use "keytool -command_name -help" for usage of command_name
[wlsuser@localhost tmp]$ 
Generate key

-genkey
keytool -genkey -v -alias mycert -keyalg RSA -keysize 2048 -sigalg SHA256withRSA \
-dname "CN=www.abc.com, OU=abc, O=ABC Corp, C=IN, ST=Banglore, L=India" \
--keypass pkpassword -storepass storepassword -validity 365 -keystore keystore.jks
Generate a CSR in the file carequest.csr for submission to a CA. The CA signs and returns a certificate or a certificate chain that authenticates your public key.
CSR

keytool -certreq -v -alias mycert -file carequest.csr -keystore keystore.jks -storepass storepassword

Send or upload the csr file to the third pary site like and get it signed . Then download root ,intermediate certificates and carequest.cer file .

Print the contents of a certificate file in a human-readable form.
keytool -printcert -v -file carequest.cer
Import Root
keytool -importcert -alias root -file root.cer -keystore keystore.jks -storepass storepassword
Import Intermediate 
keytool -importcert -alias inter -file intermediate.cer -keystore keystore.jks -storepass storepassword
Import sever Certificate 
keytool -importcert -alias mycert -file carequest.cer -keystore keystore.jks -storepass storepassword
verify keystore
keytool -list -v -alias mycert -keystore keystore.jks -storepass storepassword
change keystore password
keytool -storepasswd -keystore keystore.jks
Change key password
keytool -keypasswd -alias mycert -keystore keystore.jks
Delete the certificate with the alias aliasname from the keystore keystore.jks.
keytool -delete -alias aliasname -keystore keystore.jks -storepass storepassword
print the certificate 
keytool -printcert -f mycert.cer

Now the keystore is ready. we can use this keystore to configure in Application server's like Jboss,Weblogic,Websphere,tomcat ..etc

In a certificate if both Owner and Issuer are same then it is Self signed certificate . We can use self signed cert for the server but it is not secured and not recommended.
Every root certificate of the Third party provider are self signed it self and used to sign other certificates .

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

 If we have any SSL handshake issue first we need to understand the details steps that take place during SSL handshake then only we will be able to address it easily . Find the below some of the details .

There are multiple reasons for SSL handshake . When we get this Error on the logs . First of all we need to verify server side whether the SSL configured is 2 Ways SSL or 1 Way SSL .

SSL Handshake Issue troubleshooting :

Make sure the Public keys [ Trusted certificates (root & Intermediate )] are imported in the client truststore .

For 2 Ways SSL both side Signer certificate should be imported .

If Server side enforce the certificate then server certificate also need to import at the client trust store .

Both side should have at-least a common Allowed Protocol . Ex TLS1.2 and a cipher

If something is not satisfied in the above then make the changes accordingly .Like importing the signers in to truststore

keytool -import -file /tmp/root.cer -alias root -keysoore /pathToSSL/***.jks then enter and provide the password .

In some case we can not figure out what is the issue .Then enable SSL debug using below parameter in startup script or in the server JVM arguement .then restart the JVM

-Djavax.net.debug=ssl:handshake:verbose

once restarted test the connectivity . In the logs we can see full debug statements . and also we can see what trusted certs are loaded .There are certain steps in SSL handshake all are printed . Refer below table for SSL Handshake to get better idea .

Exception:

*** ClientHello, TLSv1.2
..
...
Compression Methods: { 0 }
Extension elliptic_curves, curve names: {secp256r1, secp384r1, secp521r1, sect283k1, sect283r1, sect409k1, sect409r1, sect571k1, sect571r1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
Extension signature_algorithms, signature_algorithms: SHA512withECDSA, SHA512withRSA, SHA384withECDSA, SHA384withRSA, SHA256withECDSA, SHA256withRSA, SHA256withDSA, SHA1withECDSA, SHA1withRSA, SHA1withDSA
Extension extended_master_secret
Extension renegotiation_info, renegotiated_connection: b7:c5:d2:43:3b:dd:24:c8:33:41:15:8b

***
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', WRITE: TLSv1.2 Handshake, length = 288
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', WRITE: TLSv1.2 Application Data, length = 384
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', WRITE: TLSv1.2 Application Data, length = 1808
...
...
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', RECV TLSv1.2 ALERT: fatal, handshake_failure
%% Invalidated: [Session-3, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384]
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', called closeSocket()
[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)', handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

In the above error handshake failed because Server side rejectClientNegotiation is set to true . and client tried to negotiate for multiple call's renegotiation_info that is where it failed .

Now you can match every step in the SSL handshake debug log with the attached screen to see at what step the failure happened .

Featured

Weblogic Domain Migration

 In this blog we will see domain re-configuration which will be done as part of Weblogic migration from lower version to higher version [ Ex...