KalyanChakravarthy.net

Thoughts, stories and ideas.

Network Scans with arp and nmap

Once in a while, I get around running network scans to check what devices and ports are open on my network. The proliferation of IoT devices in my home, combined with lack of VLAN capabilities of my router, has upped my uneasiness in the recent times.

There are two modes of scans I often run

MAC Address scans

All devices on my router have to be manually mac address whitelisted before they are able to connect to either the internet or be used internally. There is also a separate network interface for guests that are barred from the private network.

arp is a handy command that allows listing of all mac addresses on the current network. I use this often for whitelisting and verifying devices.

$ arp -an

nmap network scan

nmap is much more versatile, but also verbose and so I use it sparingly. I do perform network scans in two modes

1. Network namespace scan

The following command scans the entire /24 of the current subnet. It produces a report of all open ports.

sudo nmap -v -O -sS 192.168.1.0/24

2. Specific IP scan

For a much more detailed scan, I want to inspect a specific IP address, all its open ports, the services running and potentially the OS as well.

sudo nmap -v -O -A 4.3.2.1
  • The -A enables OS and service detection
  • Combining this with -T4 allows for faster execution.

Books in 2019

As with 2016, 2017 & 2018, I spent time lot of time reading in 2019. I managed read a total of 28 books, of which a fair amount ended up being science fiction.

Non-fiction

Its good to get the nonfiction list out of the way as its pretty short. I enjoyed every single one of them. They were light, informative and useful.

  1. Kubernetes by Kelsey Hightower

  2. The Gene by Siddhartha Mukherjee

  3. The Culture Map by Erin Meyer

  4. The Algebra of Happiness by Scott Galloway

Culture Series

This was the largest series I have ever read. A grand total of 10 books. It is incredible how my perspective changed over the course of this epic, as it went from "I hate this" to "What there isn't any more of this"

  1. The State of the Art by Iain M. Banks

  2. Excession by Iain M. Banks

  3. Inversions by Iain M. Banks

  4. Look to Windward by Iain M. Banks

  5. Matter by Iain M. Banks

  6. Surface Detail by Iain M. Banks

  7. The Hydrogen Sonata by Iain M. Banks

  8. The Player of Games by Iain M. Banks

Revelation Series

  1. Revelation Space by Alastair Reynolds

  2. Absolution Gap by Alastair Reynolds

  3. Redemption Ark by Alastair Reynolds

Scifi (other)

  1. Flowers for Algernon by Daniel Keyes

  2. The Three-Body Problem by Liu Cixin

Hyperion & Endymion series

  1. Hyperion by Dan Simmons

  2. The Fall of Hyperion by Dan Simmons

  3. Endymion by Dan Simmons

  4. The Rise of Endymion by Dan Simmons

Commonwealth Series

  1. Pandora's Star by Peter F. Hamilton

  2. Judas Unchained by Peter F. Hamilton

Fiction

  1. Sacred Games by Vikram Chandra

  2. Eleanor Oliphant is Completely Fine by Gail Honeyman

Thrillers

  1. Die Trying by Lee Child

  2. Tripwire by Lee Child

  3. And Then There Were None by Agatha Christie

Automated Plant Watering System - Part 3 - the software

This is a follow up to Part 2 and Part 1 of my Mark2 design and build of Plant watering system.

Learning

  1. The 3.7v LiPo was unable to deliver the required current of about 350 mA for running 2 motors simultaneously. This lead to the batteries dying out prematurely while being perfectly capable of squeezing out juice for just one motor.
  2. Different plants have different water consumption characteristics. For example with the current setup my Basil plant was thriving while the Coriander plant withered away due to over-watering
  3. The use of a push button without a pull down resistor makes it unusable and therefore less useful.

Fortunately the first two learnings can be incorporated by some software changes. Following are the goals I set out to accomplish in the new firmware and the code to achieve it.

Goals

  1. Drive motors independently for different "ON" and interval duration.
  2. Drive motors sequentially, for overlapping run-times/run-intervals. The assumption is its ok if a run is delayed by few seconds.
  3. Support for multiple motors to make the code re-usable for Mark3 prototype.

Solution

  1. Motors are configured by the use of an array of MotorConf structs.
  2. whosNext() computes which motor is to be activated. It does so by checking if the delta time from last run exceeded the runtime+waittime.
  3. The watchdog timer is set to wake up the loop every 1 second. However the clock is prescaled to 1/16th the speed. The combined effect should reduce further power consumption.
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <avr/power.h>

#define adc_disable() (ADCSRA &= ~(1<<ADEN)) // disable ADC (before power-off)
#define adc_enable()  (ADCSRA |=  (1<<ADEN)) // re-enable ADC

#define led 4
#define MPIN_1 1
#define MPIN_2 0
#define MPIN_EXT_1 3
#define MPIN_EXT_2 2

#define setHigh(pin) pinMode(pin, OUTPUT); digitalWrite(pin, HIGH);
#define setLow(pin) pinMode(pin, OUTPUT); digitalWrite(pin, LOW);

unsigned int lpCntr=0;
unsigned int isOn=0;

struct MotorConf {
  byte pin;
  byte onInterval;
  unsigned short offInterval;
  unsigned long lastUsed;
}


const unsigned int offInterval_2hours = 2 * 60 * 60; //(60 * 60 * 6) - onInterval; //round it off to 6 hours
const unsigned int offInterval_4hours = 4 * 60 * 60; //(60 * 60 * 6) - onInterval; //round it off to 6 hours
const unsigned int onInterval_2secs = 2; // 2 sleep cycles
const unsigned int repInterval = onInterval_2secs + offInterval_2hours;


struct MotorConf motorState[] = {
  { 1, 2, offInterval_2hours, 0 },
  { 0, 2, offInterval_4hours, 0 },
}


unsigned int currMotor = -1;
unsigned int maxMotors = 2;

void setup() {
  clock_prescale_set(clock_div_16);

  for(int i=0; i<6; i++) {
    pinMode(i, INPUT);
    digitalWrite(i, LOW);
  }

  pinMode(led, OUTPUT);
  for(int i=0; i<2; i++) {
    digitalWrite(led, HIGH);
    delay(50);
    digitalWrite(led, LOW);
    delay(50);
  }
  digitalWrite(led, LOW);

  adc_disable();
  wdt_reset();          //watchdog reset
  wdt_enable(WDTO_1S);  //1s watchdog timer
  WDTCR |= _BV(WDIE);   //Interrupts watchdog enable
  sei();                //enable interrupts
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}

int whosNext() {
  unsigned long currTime = millis() / 1000;
  for(int i=0; i<maxMotors; i++) {
    struct MotorConf currConf = motorState[i];
    if( (currTime - currConf.lastUsed) > (currConf.onInterval+currConf.offInterval) ) {
      return i;
    }
  }
  return -1;
}

void loop() {
  // Main loop

  int nextMotor = whosNext();

  if( nextMotor >= 0 && nextMotor <= maxMotors ) {
    struct MotorConf currConf = motorState[i];
    setHigh(currConf.pin);
    delay(currConf.onInterval);
    setLow(currConf.pin);
    currConf.lastUsed = millis() / 1000;
  }

  sleep_enable();
  sleep_cpu();

}

ISR (WDT_vect) {
  WDTCR |= _BV(WDIE);
}

Future improvements

Mark 2 provided fair amount of learnings. However it was plagued with limitations. Here are some improvmenets planned in Mark 3 re-design

  • Use ESP8266 along with MCP23017 GPIO Expander.
    • ESP8266 will aid in improving the configurability as well as observability.
    • MCP23017 I2C GPIP Expander will dramatic increase in number of IO ports to 16.
  • Ports for 8x Motors, 8x Soil input sensors
  • NodeMCU compatibility along with support for ESP8266 12-E bare IC. The former helps with programming, the later with power consumption.

Starlink satellite tracker

This is my first app that is published on both Android Play Store & Apple Appstore platforms. Despite React Native's brittleness and shortcomings, the productivity I experienced while working with it was phenomenal. I think I might just migrate all my old "pure" Objective-C apps to RN.

Really happy with how it turned out. Am even proud of what my lackluster design skills managed to churn out. The apps were built as a companion to the web version that my friend built (me.cmdr2.org/starlink)

What do you think?

Getting rid of Alternate Screen

... or simply making the damn terminal content persist when I seek any form of --help or run git diff.

I don't know whoever thought or why they thought that alternate screen was a sane idea is beyond me. I'd do a git diff to check my commits and promptly forget the specifics by the time I get to writing the commit message. Same with the python REPL's help function. The frustration was the kind that builds up like a thousand paper cuts, each seemingly an insignificant occurrence, only day you suddenly find yourself quadriplegic. It doesn't make any sense.

By changing OSX's terminal from xterm-256 to vt100 (Preferences → Profiles → Advanced → 'declare terminal as') I have been delivered into the sweet arms of salvation and from inflicting cuddles of frustration on my cat that has been patient (for a cat) with me through this ordeal.

Am writing this down here because I seem to run into this every few years and promptly forget how I ever managed to get rid of this pest. No more alternate screen, no more, not until this blog and OneNote chose to go away atleast.

Update 1: Oh god, that was terrible in its own right with vim becoming color blind, which sent down on yet another wild goose chase. So the next solution is to configure the pager, which you better hope is less everywhere

export LESS="-iMFXRj4a"

Now the side effect has been magical of man pages and --help now showing up line numbers! I'd call this a success (for now).

Reference and further reading

  1. http://www.shallowsky.com/linux/noaltscreen.html
  2. https://hints.macworld.com/article.php?story=20110905185128781
  3. https://superuser.com/questions/136162/how-can-i-still-see-the-man-text-after-i-quit-man/136167#136167