Thursday, December 8, 2016

handling blank iputs in a shell script.


Here is simple script to handle blank inputs in your interactive shell scripts.

#!bin/sh
while :
echo "enter value for a:";read -r a;  do
         if [ -z "${a}" ]; then
         echo "That was empty, do it again!"

        else ## echo "Checking now..."
                echo "value passed is :$a"
                echo "valid input received."
                break
    fi
done

echo "We are out of loop:"

Sample Output :

[Linux01] $ . Test_input11.sh
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:
1
value passed is :1
valid input received.
We are out of loop:

Error handling in shell script


Error handling in shell script while receiving user input

 
This is help full sample script that can help in error handling.In situations where I give user to choose 1 or 2 but lets say in rush/hurry user inputs a diff value. This script can handle those situations. In below script the user will be in loop until he passes a expected value (1 or 2 in this case). This script can be altered as per your requirement.

On how to handle just blank inputs use this link


-- Script starts here

#!bin/sh
## Created by     :    Arvind Toorpu
## This script will prompt user for input until the user passes the expected input.
##
while :
echo "enter value for a:";read -r a;  do
         if [ -z "${a}" ]; then
         echo "That was empty, do it again!"

        elif [ "${a}" -eq "1" ]  || [ "${a}" -eq "2" ]; then
                ## echo "Checking now..."
                echo "value passed is :$a"
                echo "Condition success"
         break

        elif [ "${a}" -ge "3" ]
        then
        echo "Select either option :1 or :2 "
    fi
##break
done


Sample output :

atoorpu@Linux01:[~/TEST_DIR/scripts] $ . Test_input2.sh
enter value for a:

That was empty, do it again!
enter value for a:

That was empty, do it again!
enter value for a:
4
Select either option :1 or :2
enter value for a:
5
Select either option :1 or :2
enter value for a:
6
Select either option :1 or :2
enter value for a:
6
Select either option :1 or :2
enter value for a:
7
Select either option :1 or :2
enter value for a:
2
value passed is :2
Condition success
do your thing here with read value 2

Monday, September 26, 2016

service enable slapd enable: unrecognized service


Problem: 

[root@linuxOS cn=config]# service start slapd
start: unrecognized service


[root@linuxOS cn=config]# start slapd
start: Unknown job: slapd


[root@linuxOS cn=config]# start slapd
start: Unknown job: slapd


Solution:
 Use the start command after ServiceName

Syntax :
# service ServiceName Start

[root@linuxOS app]# service slapd start

Checking configuration files for slapd:                    [WARNING]
57e427f1 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
57e427f1 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={2}bdb.ldif"
config file testing succeeded
Starting slapd:                                            [  OK  ]

Friday, September 2, 2016

gpg encryption helpfull cmds and examples


to create a key:
gpg --gen-key
generally you can select the defaults.

to export a public key into file public.key:
gpg --export -a "User Name" > public.key
This will create a file called public.key with the ascii representation of the public key for User Name. This is a variation on:
gpg --export
which by itself is basically going to print out a bunch of crap to your screen. I recommend against doing this.
gpg --export -a "User Name"
prints out the public key for User Name to the command line, which is only semi-useful

to export a private key:
gpg --export-secret-key -a "User Name" > private.key
This will create a file called private.key with the ascii representation of the private key for User Name.
It's pretty much like exporting a public key, but you have to override some default protections. There's a note (*) at the bottom explaining why you may want to do this.

to import a public key:
gpg --import public.key
This adds the public key in the file "public.key" to your public key ring.

to import a private key:
NOTE: I've been informed that the manpage indicates that "this is an obsolete option and is not used anywhere." So this may no longer work.
gpg --allow-secret-key-import --import private.key
This adds the private key in the file "private.key" to your private key ring. There's a note (*) at the bottom explaining why you may want to do this.

to delete a public key (from your public key ring):
gpg --delete-key "User Name"
This removes the public key from your public key ring.
NOTE! If there is a private key on your private key ring associated with this public key, you will get an error! You must delete your private key for this key pair from your private key ring first.

to delete an private key (a key on your private key ring):
gpg --delete-secret-key "User Name"
This deletes the secret key from your secret key ring.

To list the keys in your public key ring:
gpg --list-keys

To list the keys in your secret key ring:

gpg --list-secret-keys

deleting gpg key from store

[oracle@Linux03 ~]$ gpg --list-keys
/home/oracle/.gnupg/pubring.gpg
-------------------------------
pub   2048R/17DC5B7A 2016-09-02
uid                  arvind (arvind gpg test) <arvind@gmail.com>
sub   2048R/5C9DA3C6 2016-09-02


[oracle@Linux03 ~]$ gpg --delete-keys arvind
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

gpg: there is a secret key for public key "arvind"!
gpg: use option "--delete-secret-keys" to delete it first.
[oracle@Linux03 ~]$ gpg --delete-secret-keys arvind
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


sec  2048R/17DC5B7A 2016-09-02 arvind (arvind gpg test) <arvind@gmail.com>

Delete this key from the keyring? (y/N) y
This is a secret key! - really delete? (y/N) y
[oracle@Linux03 ~]$ gpg --list-keys
/home/oracle/.gnupg/pubring.gpg
-------------------------------
pub   2048R/17DC5B7A 2016-09-02
uid                  arvind (arvind gpg test) <arvind@gmail.com>
sub   2048R/5C9DA3C6 2016-09-02

[oracle@Linux03 ~]$ gpg --delete-keys arvind
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


pub  2048R/17DC5B7A 2016-09-02 arvind (arvind gpg test) <arvind@gmail.com>

Delete this key from the keyring? (y/N) y
[oracle@Linux03 ~]$ gpg --list-keys
[oracle@Linux03 ~]$ 

how to generate a new key with gpg encryption


Assuming that you have installed the gpg package on your machine.

[oracle@Linux03 ~]$ gpg --gen-key
gpg (GnuPG) 2.0.14; Copyright (C) 2009 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default)
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
Your selection? 1
RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: arvind
Email address: arvind@gmail.com
Comment: arvind gpg test
You selected this USER-ID:
    "arvind (arvind gpg test) <arvind@gmail.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.

can't connect to `/home/oracle/.gnupg/S.gpg-agent': No such file or directory

(pinentry-gtk-2:3073): GLib-GObject-CRITICAL **: Object class GtkSecureEntry doesn't implement property 'editing-canceled' from interface 'GtkCellEditable'

(pinentry-gtk-2:3076): GLib-GObject-CRITICAL **: Object class GtkSecureEntry doesn't implement property 'editing-canceled' from interface 'GtkCellEditable'
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key 17DC5B7A marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   2048R/17DC5B7A 2016-09-02
      Key fingerprint = 7B68 5877 A612 F10A AD43  5E01 2320 7F55 17DC 5B7A
uid                  arvind (arvind gpg test) <arvind@gmail.com>
sub   2048R/5C9DA3C6 2016-09-02



[oracle@Linux03 ~]$ gpg --list-keys
/home/oracle/.gnupg/pubring.gpg
-------------------------------
pub   2048R/17DC5B7A 2016-09-02
uid                  arvind (arvind gpg test) <arvind@gmail.com>
sub   2048R/5C9DA3C6 2016-09-02

[oracle@Linux03 ~]$

Thursday, June 30, 2016

Copy files from linux to windows using PSCP (putty)


This is a  simple batch script that will prompt you for the remote file location and when  provided will copy file from remote location to local destination directory (will prompt for local dir too ).
This script will need putty pscp executable file and you will need to configure the create and save connection to the remote hosts. As below

You can download putty from here






-- Script starts here 
-- copy below to file and save it as batch file Ex:  filename.bat 



@ECHO off

SET /P REMOTE_FILE=ENTER FULL PATH TO REMOTE FILE:
SET /P LOCAL_FOLDER=ENTER PATH TO LOCAL FOLDER:
SET PATH=C:\Program Files (x86)\PuTTY;%PATH%
IF "%REMOTE_FILE%"=="" GOTO Error1
IF "%LOCAL_FOLDER%"=="" GOTO Error2
ECHO EXECUTING :
ECHO pscp username@Linux01:%REMOTE_FILE% %LOCAL_FOLDER%
pscp username@Linux01:%REMOTE_FILE% %LOCAL_FOLDER%
GOTO End
:Error1
ECHO You didn't enter REMOTE_FILE details!!
:Error2
ECHO You didn't enter LOCAL_FOLDER details!!
:End




Sample output :

C:\Windows\System32>pscp_script.bat
ENTER FULL PATH TO REMOTE FILE: /u01/app/oracle/dpump/test1.log
ENTER PATH TO LOCAL FOLDER: C:\test
EXECUTING :
pscp username@Linux01:/u01/app/oracle/dpump/test.log C:\test
test.log                  | 18 kB |  18.5 kB/s | ETA: 00:00:00 | 100%