nmap command to scan TCP/UDP ports

Nmap, short for Network Mapper, emerges as a command-line tool capable of scanning networks by sending packets and analyzing the responses. It’s particularly adept at identifying open ports and services running on a target system.

Scanning TCP Ports

Nmap’s TCP port scanning is robust. For instance, scanning ports 1 to 100 on a target:

nmap -p 1-100 <target>

To focus on specific ports, say 80, 443, and 8080:

nmap -p 80,443,8080 <target>

Or a comprehensive scan across all TCP ports (1 to 65535):

nmap -p- <target>

Scanning UDP Ports

UDP port scanning differs due to the protocol’s connectionless nature. Scanning UDP ports 1 to 100:

nmap -sU -p 1-100 <target>

For specific UDP ports, e.g., 53 and 161:

nmap -sU -p 53,161 <target>

Scanning Both TCP & UDP ports

nmap -sU -sT -p 53 <target>

or

`nmap -sUT -p 53 <target>`

Keepalive ssh sessions for longer durations

 

In general most of the ISP providers will terminate idle sessions as early as possible(maybe in a couple of minutes).

This will be an irritating thing if you work on a remote server with ssh. I had a similar issue with my ISP(Act Fibernet). To fix this issue I have experimented in multiple ways and I am sharing the easiest way to that works.

Add following lines in your /etc/ssh/sshd_config file:

ClientAliveInterval 60
ClientAliveCountMax 5

where ClientAliveInterval 60 seconds will send a null request from your node(client) to server every 60 seconds. ClientAliveCountMax 5 is to give up if it doesn’t receive any response after 5 retries.

After adding the above configurations restart ssh with the following command:

sudo service ssh restart

You can try with different values for ClientAliveInterval based on your ISP. In general most of the ISP’s will persist idle sessions for a couple of minutes. In my case Act Fibernet sessions are not responding after 2 minutes(approximately), so I used 60 seconds.

 

Ansible Playbook – Print command output

 

By using the following play I am printing command output in Ansible playbook:

---
- hosts: all
  user: ubuntu
  tasks:
    - name: uptime
      command: 'uptime'
      register: output

    - debug: var=output.stdout_lines

Here I am registered output as output variable, in debug task printing the same with output.stdout_lines.

Other ways to print output:

#- debug: msg="{{ output.stdout }}"
#- debug: msg="{{ output.stderr }}"

Nginx SSL – Generate chained.crt

 

To generate chained.crt file you need following 2 files:

  • server.crt(other name yoer_domain_name.crt)
  • IntermediateCA.crt

Concatenate above 2 files in the same order to generate chained.crt file.

$ cat server.crt IntermediateCA.crt > chained.crt

Add these 2 lines in Nginx config:

ssl_certificate    /etc/nginx/ssl/chained.crt
ssl_certificate_key    /etc/nginx/ssl/your_domain_name.key;

Once it is done restart the Nginx.

Scheduling conditional statements with crontab

 

Recently while trying to auto-restart a daemon which is down by identifying with ps aux and if condition, it is not worked as intended with crontab.

I used following command which is perfectly running in from command line but not through crontab.

if [ `ps aux | grep nrpe | grep -v grep | wc -l` -eq 0 ]; then service nagios-nrpe-server restart ;fi

After trying with different commands following thing worked for me:

pgrep nrpe; [ $? != 0 ] && /etc/init.d/nagios-nrpe-server restart

where pgrep returns non zero exit code if the process nrep is not running and with $? is used to get the exit code of previous command (in this case pgrep) and start the process.

ImportError: No module named ‘boto3’

 

I got this error while I am trying to use boto3 module in python.

The main reason for this error is, there is no boto3 module installed.

To install boto3 user following command:

sudo pip3 install boto3

One thing you need to observe here is to use pip3 to install boto3.

If pip3 not installed in your node user following command to install it:

sudo apt-get install python3-pip

Nginx – Host/serve an Android apk file

 

To host/serve Android apk with Nginx following changes are required:

  • Adding mime type in /etc/nginx/mime.types
  • Explicitly adding header *.apk in site conf

Adding mime type in /etc/nginx/mime.types:

Add following line in mime.types file

application/vnd.android.package-archive apk;

Explicitly adding header *.apk in site conf: 

Update following configuration in your site conf file

location ~* \.(apk)$ {
  ......
  add_header Content-Type application/vnd.android.package-archive;
  ......
}

Finally restart Nginx.

sudo service nginx restart

 

Lists/Remove all empty files in a directory

 

To list all empty files in a directory use follwoing command:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; fi; done

To remove all empty files in a directory use following commad:

for file in `ls`; do if [[ ! -s $file ]]; then echo $file; rm $file; fi; done

ftp – sync data from remote ftp server

 

To sync data from remote ftp server install ncftp with following command:

apt-get install ncftp

Now use following command to sync data from remote ftp server:

ncftpget -R -T -v -u userName -p 'password' ftp_hostname "directoryPathInFtpServer" "directoryPathInLocalMachine"