Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create a MacTracker App

DZone's Guide to

How to Create a MacTracker App

In this tutorial, learn to make a simple app to help users keep track of the devices connected to a given network using Java and the NMAP tool.

· DevOps Zone ·
Free Resource

Discover how quick and easy it is to secure secrets, so you can get back to doing what you love. Try Conjur, a free open source security service for developers.

This is a simple application that can help a user keep track of the devices connected to a network. It uses the capabilities of a tool called NMAP and very simple logic to deduce the MAC address and the device connected to the network.

Requirements

1. NMAP installed in the machine.

2. Java installed in the machine.

3. crontab.

NMAP

NMAP (Network Mapper) is a command that can help users discover hosts and services on a computer network. I have used NMAP as a service in my application to identify devices that are connected over the network. To install NMAP within your system I would suggest my viewers to install ZENMAPwhich is just a GUI wrapper over the NMAP command. It will help harness the capabilities of NMAP over a lightweight GUI. To get ZENMAP installed on your system, just follow this link. Since I am using a Mac environment to set up my application, I just downloaded the required version of Zenmap, extracted it, and placed it in my application directory. Before installing the application NMAP into your Mac, please check that your machine has the permission to install applications outside your app store. To do that, just visit System Preferences -> Security -> Click on "Anywhere" . To check whether NMAP has properly been installed in your machine, just run the command nmap --version.
In this application, I have taken a dump of NMAP output in an XML format using the parameter -oX. The NMAP command looks like this: nmap -sP -n -oX - <ip_address>/24 | tee nmap.xml. You can use the parameter -oN to output the desired result in a text format as well.

MAC_IDENTIFIER

The MAC_IDENTIFIER directory has a Java logic written that would extract the MAC Address and the device name connected over the network. A BufferedReader object would contain the required nmap dump, which can be iterated to get the corresponding MAC address of the device connected over the network, and the device name as well.

TRACKER_EXECUTE

The TRACKER_EXECUTE directory has two shell scripts. run_nmap.sh would run the nmap command and dump the entire log into a file called nmap. The Java logic written utilizes this dump to iterate the MAC address and the device name. Then comes the logic for Extractor.sh ; it simply traverses to the MAC_IDENTIFIER directory, the compiles and executes the Java class. In the corresponding shell script run_nmap.sh I have used the IP address of my network. Please make sure to enter the IP address of your respective network.

Now comes the interesting part of setting a cron job to automate the entire process.

TASK_RUNNER

Under the root directory, I have another shell executed that basically runs my two processes of dumping the nmap log and iterating the java logicthereafter.

Setting the Cron Job

UNIX and LINUX by default provides a default service called crontab that helps in executing services periodically. I have a cron job set that would run this task_runner logic every after thirty minutes. Here is the logic for it:
*/30 * * * * /Users/soumyajit/Documents/git_repos/target_mac/task_runner.sh > /Users/soumyajit/Documents/git_repos/target_mac/task_runner.log 2>&1
N.B: I have configured the cron so that I get the redirected output of the terminal into a log file called task_runner.log.

Setting Up CRONTAB

Crontab utility is a program that would run applications or scripts within a system at a certain interval of time. It basically has four modes of working:

  1. crontab -e: Suggests editing the current crontab in the system.
  2. crontab -l: Displaying the current version of the crontab.
  3. crontab -u: Specifying the name of the user whose crontab is to be tweaked.
  4. crontab -r: Removes the current crontab for the user.

Please follow my github page for more information on the project.

Conjur is a free open source security service built by DevOps engineers. With integrations with all your favorite tools and an easy way to secure secrets, it's a no brainer. Come check it out!

Topics:
devops ,java ,network automation ,tutorial ,app development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}