How to check status of port using NMap

NMap is a classic Swiss army knife for network operations. I recently had to check if a certain port on a LAN computer was open or not. This type of diagnostic is very easy to do with NMap.

  • NMap can be installed easily:
$ sudo apt install nmap
  • To check if port 9010 was open on the medusa computer:
$ nmap -p 9010 medusa

Starting Nmap 6.40 ( http://nmap.org ) at 2017-08-25 13:05 SGT
Nmap scan report for medusa (192.168.110.22)
Host is up (0.00035s latency).
PORT     STATE SERVICE
9010/tcp open  sdr

Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds

Turns out that the port 9010 was open.

  • To check port 9999 on medusa, which was actually closed:
$ nmap -p 9999 medusa

Starting Nmap 6.40 ( http://nmap.org ) at 2017-08-25 13:06 SGT
Nmap scan report for medusa (192.168.110.22)
Host is up (0.00030s latency).
PORT     STATE  SERVICE
9999/tcp closed unknown

Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
  • To check the status of ports in the range on the medusa computer:
$ nmap -p 0-10 medusa

Starting Nmap 6.40 ( http://nmap.org ) at 2017-08-25 13:04 SGT
Nmap scan report for medusa (192.168.110.22)
Host is up (0.00027s latency).
PORT   STATE  SERVICE
0/tcp  closed unknown
1/tcp  closed tcpmux
2/tcp  closed compressnet
3/tcp  closed compressnet
4/tcp  closed unknown
5/tcp  closed unknown
6/tcp  closed unknown
7/tcp  closed echo
8/tcp  closed unknown
9/tcp  closed discard
10/tcp closed unknown

Nmap done: 1 IP address (1 host up) scanned in 0.08 seconds

Tried with: NMap 7.01 and Ubuntu 16.04

Advertisements

ImportError on tilde path

Problem

I had a Python module named foobar.py located inside ~/foobar directory. To be able to import it, I did this in my Python code:

import sys
sys.path.append("~/foobar")
import foobar

However, when that script was run, it gave this error:

Traceback (most recent call last):
  File "./hello_world.py", line 10, in 
    import foobar
ImportError: No module named foobar

Solution

It turns out that Python cannot expand the tilde to my home directory! This was quite surprising to me. Once I expanded it to the absolute path, the import worked fine.

Tried with: Python 2.7.12 and Ubuntu 16.04

AdvancedSorters plugin for Vim

Vim has an inbuilt :sort function that can be used to sort lines. However, I recently had to sort words in a line. I could have broken the line and put each word on one line, sort them and put the results back as a line. Thankfully, I found the AdvancedSorters plugin for Vim that does this job easily in one command.

Tried with: Vim 7.4 and Ubuntu 16.04

Please exit X before installing NVIDIA driver

Problem

I was trying to install a NVIDIA graphics driver on a machine. The installer failed with this error:

ERROR: You appear to be running an X server; please exit X before installing. For further details, please see the section INSTALLING THE NVIDIA DRIVER in the README available on the Linux driver download page at www.nvidia.com

Solution

This is a common error when installing the graphics driver on Linux. You just need to exit X before you install the driver.

This can be done on Ubuntu with this command:

$ sudo service lightdm stop

Tried with: Ubuntu 14.04

NVIDIA module already loaded in kernel

Problem

I was trying to install the latest NVIDIA graphics driver on a machine. When I ran the installer, it would complain like this:

An NVIDIA kernel module nvidia appears to already be loaded in your kernel

Solution

I tried to remove the module:

$ sudo modprobe -r nvidia

But, the kernel complained that the module was being used.

I tried to see which process was using this module:

$ lsof /dev/nvidia*

This showed that no process was using the module. This was surprising.

This is what worked for me in the end. I added a blacklist nvidia line to /etc/modprobe.d/blacklist.conf and rebooted the machine.

I then invoked the NVIDIA driver installer with the --uninstall option. Another reboot later, I found that the module was no longer in the kernel.

I was able to install the NVIDIA driver after that.

Tried with: Ubuntu 14.04

How to cherry pick in Git

Cherry picking in Git allows you to pick a specific commit and replay the changes done in that single commit on your current commit. This is different from a merge, where Git tries to replay the changes of all the commits from the other branch. Even if you specified a commit for a merge, Git will try to fuse the branch of which that commit is head to your current branch. After a merge, the two branches are fused in the DAG.

In contrast, a cherry pick replays changes on your current workspace. After you cherry pick a commit and replay it on your current commit, you will see that there is no branch being merged to your commit in the DAG. There will just be one or more new commits.

  • To cherry pick a specific commit and replay it on your current commit, use the hash of that commit:
$ git cherry-pick 123456
  • You can use a branch name too, in which case only topmost commit of the branch is replayed:
$ git cherry-pick some_branch
  • You can specify multiple commits to cherry pick, in that order:
$ git cherry-pick 123456 some_branch some_tag

The above command will result in 3 new commits on your current branch.

Cherry-picking changes all the files that were changed in a commit. If you need to pick only the changes from a specific file in a specific commit, we need to be a bit more clever.

First, we get the diff of the changes on a specific file in a specific commit:

$ git show 123456 -- foo/bar.cpp

We can then pipe this diff to be applied as a patch on our current workspace:

$ git show 123456 -- foo/bar.cpp | git apply -

Tried with: Git 2.7.4 and Ubuntu 16.04

How to auto-rotate images based on EXIF information

Image viewers on the computer, in smartphones or cameras can today read the orientation information in the EXIF data of an image and auto-rotate an image when displaying it to you. However, you might want some images to be actually stored in the orientation specified in their EXIF data. For example, this helps to render correctly in old or simpler image viewers that ignore the EXIF orientation information. EXIFTran is a tool that can help you achieve this.

  • Installing it is easy:
$ sudo apt install exiftran
  • To do in-place auto-rotate of images based on their EXIF orientation:
$ exiftran -a -i *.JPEG

Tried with: EXIFTran 2.10 and Ubuntu 16.04

How to use Vim with Cscope

Cscope provides a rudimentary commandline interface for exploring your C or C++ codebase. But using it along with Vim provides a much richer interface.

  • Make sure you have installed Cscope and know how to use it. This is explained here.

  • To be able to use Cscope inside Vim, install the cscope_maps.vim plugin file. This old file can be found mirrored at this Github repository here.

  • Open Vim in the root of the code directory that has the generated Cscope cross-reference file cscope.out.

  • Open any source file and move cursor to any symbol and use the following commands: Ctrl-\ s to jump to any instance of the symbol, Ctrl-\ f to jump the file under the cursor, Ctrl-\ g to jump to the definition and Ctrl-\ c to jump to calls of the function under cursor.

  • Use the Vim command Ctrl-o to jump back to previous locations in files.

  • If you are using a cross-reference file with a filename that is not cscope.out or it is not in the current directory, then export that file path in the CSCOPE_DB environment variable. Vim will use that path to read the cross-reference file.

Tried with: Cscope 15.8b, Vim 7.4 and Ubuntu 16.04

How to install and use cscope

Cscope is a classic source code explorer that can be used to explore a directory containing C source and header files. It can also be applied on C++ files, but do not expect much intelligence on C++ features. It indexes the source files, just like ctags, but provides more useful ways to query the code. It provides a simple commandline interface to query the codebase. You might want to configure Vim to use cscope for a more user-friendly interface.

  • Installing cscope is easy:
$ sudo apt install cscope
  • To recursively scan and index all C source and header files in a directory and bring up the interface to explore it:
$ cscope -R

This will produce a cscope.out file which is where the symbol cross-references are stored. In the interface, you can look up symbols, definition of a symbol, all instances of a function call and even do find-replace. To exit the interface, press Ctrl-D.

  • To only produce the cross-reference file:
$ cscope -R -b
  • To request cscope to write cross-reference file to a different filename:
$ cscope -b -f .some_cscope_file
  • To request cscope to index only files you want, list them in a namefile that is named cscope.files and invoke:
$ cscope -i -b
  • To use a different namefile:
$ cscope -i some_namefile -b
  • To be able to index C++ or CUDA files, we need to be able to list them out in a namefile. This can be done using the find command and we can ask cscope to use that namefile:
$ find -iregex '.*\.\(h\|hpp\|c\|cpp\|cu\)$' > .files_for_cscope
$ cscope -i .files_for_cscope -b

If you use these commands frequently, then wrap them up in a shell function for ease of use.

Tried with: Cscope 15.8b and Ubuntu 16.04