Pages

Wednesday, July 14, 2021

Ways to check reachability of remote ports

A port is a logical entity which acts as a endpoint of communication associated with an application or process on an Linux operating system. It is useful to know which ports are open and running services on a target machine before using them.


We can easily list open ports in Linux on a local machine using the netstat or several other Linux commands such NMAP.


In this guide, we will show you how to determine if ports on a remote host are reachable/open using simple netcat (in short nc) command.


netcat (or nc in short) is a powerful and easy-to-use utility that can be employed for just about anything in Linux in relation to TCP, UDP, or UNIX-domain sockets.


# yum install nc                  [On CentOS/RHEL]

# dnf install nc                  [On Fedora 22+]

$ sudo apt-get install netcat     [On Debian/Ubuntu]

We can use it to: open TCP connections, listen on arbitrary TCP and UDP ports, send UDP packets, do port scanning under both IPv4 and IPv6 and beyond.


Using netcat, you can check if a single or multiple or a range of open ports as follows. The command below will help us see if the port 22 is open on the host 192.168.56.10:


$ nc -zv 192.168.1.15 22

In the command above, the flag:


-z – sets nc to simply scan for listening daemons, without actually sending any data to them.

-v – enables verbose mode.

The next command will check if ports 80, 22 and 21 are open on the remote host 192.168.5.10 (we can use the hostname as well):

nc -zv 192.168.56.10 80 22 21


It is also possible to specify a range of ports to be scanned:’


$ nc -zv 192.168.56.10 20-80

Tuesday, July 13, 2021

builtin(command) bash

builtin command is useful to define a shell function with the same name as a shell builtin, retaining the functionality of the builtin within the function.


 The main use of this command is to define a shell function having the same name as the shell builtin by keeping the functionality of the builtin within the function.


Syntax:


builtin [shell-builtin [arg ..]]

Example: Here, we are creating a function to replace the ‘cd’ command. When you will use cd() function it will change the directory to the desktop directly.

Saturday, May 15, 2021

C style for loop

The syntax of the C-style for loop is taking the following form:

    for ((INITIALIZATION; TEST; STEP))

        do

        [COMMANDS]

    done


The INITIALIZATION part is executed only once when the loop starts. Then, the TEST part is evaluated. If it is false, the loop is terminated. If the TEST is true, commands inside the body of the for loop are executed, and the STEP part is updated.

In the following example code, the loop stars by initializing i = 0, and before each iteration checks if i = 10. If true it prints the current value of i and [increments the variable] i by 1 (i++) otherwise the loop terminates.


    for ((i = 0 ; i <= 1000 ; i++)); do

          echo "Counter: $i"

    done

The loop will iterate 1001 times.

Friday, May 14, 2021

typeset and declare

In bash, typeset and declare are exactly the same. The only difference is that typeset is considered obsolete.


typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...

    Set variable values and attributes.

    Obsolete.  See `help declare'.

The man page even lists them in the same breath:


'typeset'

typeset [-afFrxi] [-p] [NAME[=VALUE] ...]

The 'typeset' command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the 'declare' builtin command.


declare [-aAfFgilrtux] [-p] [name[=value] ...]

typeset [-aAfFgilrtux] [-p] [name[=value] ...]

    Declare variables and/or give them attributes.

typeset is portable to some other shells, for example, ksh93. If you are aiming for cross-shell portability, use typeset (and make sure that the way you are calling it is portable). If you don't care about such portability, use declare.

Difference between typeset and declare:

The typeset is more portable(e.g. ksh), while the declare is more preferable when portability is not a concern.

Difference between declare(or typeset) and local when used inside a function:

The typeset implies the declare, but more powerful. For example, declare -i x makes x have the integer attribute, declare -r x makes x readonly, etc.

local and declare are mostly identical and take all the same arguments with two exceptions: local will fail if not used within a function, and local with no args filters output to print only locals, declare doesn't.



ssh user@host "$(declare -f foo);foo"


#!/bin/bash

# Define your function

myfn () {  ls -l; }

To use the function on the remote hosts:


typeset -f myfn | ssh user@host "$(cat); myfn"

typeset -f myfn | ssh user@host2 "$(cat); myfn"

Better yet, why bother with pipe:


ssh user@host "$(typeset -f myfn); myfn"

Or you can use a HEREDOC:


ssh user@host << EOF

    $(typeset -f myfn)

    myfn

EOF

If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:


ssh user@host "$(typeset -f); myfn"

Explanation

typeset -f myfn will display the definition of myfn.

cat will receive the definition of the function as a text and $() will execute it in the current shell which will become a defined function in the remote shell. Finally the function can be executed.

The last code will put the definition of the functions inline before ssh execution.




Tuesday, May 11, 2021

mapfile

mapfile also called (read array) is a command of the Bash shell used to read arrays. It reads lines from the standard input into an array variable. Also, mapfile must read from substitution (< <) and not from a pipe. 

Only available from bash 4.

Also, mapfile is faster and more convenient when compared to a read loop. It returns 1 or 0 depending on whether the command was successful or not. If no array name is not specified, the default variable MAPFILE is used as the target array variable.

Syntax: mapfile [array]

Alternatively, we can use read array [arrayname] instead of mapfile.

Example 1. Reading an array from a file:

$ mapfile MYFILE < example.txt

$ echo ${MYFILE[@]}

$ echo ${MYFILE[0]}

Example 2. Capture the output into an array:

$ mapfile TESTARRAY < <(printf "Item 1\nItem 2\nItem 3\n")

$ echo  ${TESTARRAY[@]}

Here, Item1, Item2, and Item 3 have been stored in the array TESTARRAY.

Example 3. Strip newlines and store item using -t:

$ mapfile -t TESTARRAY< <(printf "Item 1\nItem 2\nItem 3\n")

$ printf "%s\n" "${TESTARRAY[@]}"

$ mapfile -n 2 TESTARRAY < example.txt

$ echo  ${TESTARRAY[@]}