Tuesday, April 28, 2020

how to find my public ip address in linux ?

Using dig


dig (domain information groper) is a simple command line utility for probing DNS name servers. To find your public IP addresses, use the opendns.com resolver as in the command below:


Syntax:


dig +short myip.opendns.com @resolver1.opendns.com


Example:


dig +short myip.opendns.com @resolver1.opendns.com


111.125.200.200


Using host


host command is an easy-to-use command line utility for carrying out DNS lookups. The command below will help to display your system public IP address.


Syntax:
host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'


Example:


host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'
111.125.200.200


Using wget


curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.


Syntax:


wget -qO- http://ipecho.net/plain | xargs echo


Or


wget -qO - icanhazip.com


Example:


 wget -qO- http://ipecho.net/plain | xargs echo
111.125.200.200


 wget -qO- http://ipecho.net/plain | xargs echo
111.125.200.200


Using curl


curl is a popular command line tool for uploading or downloading files from a server using any of the supported protocols (HTTP, HTTPS, FILE, FTP, FTPS and others). The following
commands display your public IP address.


Syntax:


curl ifconfig.me


Example:
curl ifconfig.me
111.125.200.200


The following commands will get you the IP address list to find public IP addresses for your machine:

  • curl ifconfig.me
  • curl icanhazip.com
  • curl ipinfo.io/ip
  • curl api.ipify.org
  • curl checkip.dyndns.org
  • curl ident.me
  • curl bot.whatismyipaddress.com
  • curl ipecho.net/plain

Monday, April 20, 2020

nc command examples in linux?

Netcat or nc is a networking utility for debugging and investigating the network.

This utility can be used for creating TCP/UDP connections and investigating them.
The biggest use of this utility is in the scripts where we need to deal with TCP/UDP sockets.



How to Install and Use Netcat in Linux
To install the netcat package on your system, use the default package manager for your
Linux distribution.


[On CentOS/RHEL]

$ yum install nc                  

 [On Fedora 22+ and RHEL 8]
$ dnf install nc                 

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


Port Scanning

Netcat can be used for port scanning: to know which ports are open and running services on
a target machine. It can scan a single or multiple or a range of open ports.

 It may be useful to know which ports are open and running services on a target machine.
  The -z flag can be used to tell nc to report open ports, rather than initiate a connection.
Usually it's useful to turn on verbose output to stderr by use this option in conjunction with -
v    option.


Where 

    -v      Have nc give more verbose output.
    -w    timeout      Connections which cannot be established or are idle timeout after timeout
seconds.   The -w flag has no effect on the -l option, i.e. nc will listen forever for a connection, with or without the -w flag.  The default is no timeout.

    -z      Specifies that nc should just scan for listening daemons, without sending any data
to them.  It is an error to use this option in con‐junction with the -l option.

  -n      Do not do any DNS or service lookups on any specified addresses, hostnames or
ports.





  #scan a single port
$ nc -v -w 2 z 192.168.56.156 22   

Example:
    
 nc -v -w 2 -z 192.168.56.156 22 
 Connection to 192.168.56.156 22 port [tcp/ssh] succeeded!


#scan multiple ports

$ nc -v -w 2 z 192.168.56.156 22 80  

 #scan range of ports

$ nc -v -w 2 z 192.168.56.156 20-22 
Example:

nc -v -w 2 -z 192.168.56.156 20-22
nc: connect to 192.168.56.156 port 20 (tcp) failed: Connection refused
nc: connect to 192.168.56.156 port 21 (tcp) failed: Connection refused
Connection to 192.168.56.156 22 port [tcp/ssh] succeeded!





Find a Service Running on Port

You can also use Netcat to obtain port banners. In this case, it will tell you what service is
running behind a certain port. For example to know what type of service is running behind
port 22 on a specific server, run the following command

nc -v -n 192.168.56.156 22
Connection to 192.168.56.156 22 port [tcp/*] succeeded!
SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.7

Wednesday, January 15, 2020

Which directory is that bash script in?

There may be times when you need to know the actual location a BASH script is located
within the script. This can be done with a combination of the $0 value and the dirname
command.

The $0 value

A BASH script makes the first command line argument available as $1, the second as $2
and so on. The command run, exactly as it was called but without the command line
arguments, is stored in $0

The dirname command

The dirname command returns the directory name part of a filename. Note that the directory
and/or file do not actually exists; dirname simply strips the last component of a file path.

For example, "dirname /a/b/c" will echo "/a/b", "dirname ../a/b/c" will echo "../a/b", "dirname
../" will echo "." and so on.

Putting it all together
The directory the BASH script is located can be retrieved using dirname $0 like so:

DIRECTORY=`dirname $0`

BUT note that this may be a relative path and not necessarily an absolute one, depending 
how the script is called. Take the following script, for example, which saves the directory
name to a variable for later use, and then echos it:

#!/bin/bash
DIRECTORY=`dirname $0`
echo $DIRECTORY
If the file is saved at /home/arun/bin/test1.sh, the permissions are changed so it can be
executed (chmod 0700 ~/bin/test1.sh) and it is run from /home/arun like so:

/home/arun/bin/test1.sh
then it will echo this:

/home/arun/bin
It it is run like so:

bin/test1.sh
then it will echo this:

bin
Because the path used to run the script in the second example is a relative path, "dirname
$0" only echo’s "bin", which may not be suitable.

Always return the absolute path

To always return the absolute path to the directory the script is located, change to the scripts
directory and get the dirname like so, again saving the value to a variable for later use and
then echoing it:

#!/bin/bash
DIRECTORY=$(cd `dirname $0` && pwd)
echo $DIRECTORY

If this script is saved as /home/arun/bin/test2.sh , then whether it is run as
/home/arun/bin/test2.sh or bin/test2.sh or by some other path, it will always echo:

/home/arun/bin

Note that although the change directory command (cd) is used, the script will not change
directory and any other calls within it are still relative to the current working directory.

If we were running the above script in /home/arun, then calling "pwd" in the next line would
still echo "/home/arun"