DOS Batch scripting to make sure server ports are open (or closed)

This project is a result of a series of DevOps challenges I have recently taken on.  It builds on two previous projects. (See this and this).

This particular challenge was to:

  • Write a script using any language that takes an IP address and list of ports from standard input
  • Check to see if the ports are open
  • If not, send a command via SSH to the server at the IP address to start the service that makes the port open
  • Output the results to a log file
  • For extra credit let the script run against a server brought up by a vagrantfile

I extended the requirements in three ways:

  1. Add a “Usage” section to the script which is displayed if arguments are missing or are “/?”, “/h” or “-h” for example.
  2. Add the ability to close as well as open the ports. This is to readily support testing if not for completeness sake.
  3. Build a test script that verifies that the solution meets the requirements.

Also in the interest of user friendliness, I followed these principles:

  • Just before running a command that could take time depending on the speed of software, networks or servers, echo to the terminal a line explaining what operation is in the works.
  • In the log file be more terse and to the point unless there was an error.  In that case try to bring in information that can help lead to a solution such as the command being run, its standard output and error output. Try this out by deliberately messing with the name of the Linux command or service embedded in the script.  Where there is no error, check out the sample logs generated by running the test script in the link below.

This challenge is a natural follow on to the previous challenge which brings up a virtual server on one’s personal computer utilizing a vagrant file.  So we’ll take the extra credit by running against that.  See:

Since the virtual server has a website and port 80 is where the website is accessed from the browser, testing the script could have the very tangible result of taking the website on or off line.

The scripting language I chose is DOS Batch. This is handy because I’m running Windows on my personal computer and have used DOS Batch in the past.

In order to begin, I had to find a utility to check the status of ports and ended up downloading and using Nmap. See . With Nmap, I also discovered some additional open ports on the server which allowed me to extend testing to multiple ports.  So in addition to port 80, I also support port 111 which it turns out is easy to open and close as well on the virtual server.

In order to test graceful failing where the script is not programmed to start or stop a service to open or close a port, I arbitrarily chose port 99 which was closed on the virtual server in the first place.

Google and Stack Overflow continue to be my constant consultants.  I used these to determine which commands I would use on the virtual server to start and stop services and which services controlled which ports.   Nmap also provides some intelligence on this as well.

Once I knew how to check a port and make it open or closed from the DOS command line I was ready to begin scripting. To review the language (and quirks) of DOS batch, I read the beginning and other portions of .

I worked through the inevitable bugs in the development process by temporarily:

  1. Commenting out the initial command of @echo off
  2. Inserting extra ECHO commands
  3. Inserting TYPE commands to dump the contents of a temp or log file to the screen

Once I got the main script into satisfactory shape, I capped this project with the automated test script also in DOS batch.

See my solution at:

… and thanks for looking!

Using Vagrant to deploy a server and website

I took on some DevOps challenges that include one to:

Create a vagrantfile that brings up a server with a working data driven website.  The vagrant file is expected to use a configuration management framework such as Chef, Puppet, Ansible, Salt or Docker.

I started by banging out a sample website. See A mini Data Driven site in PHP and MySQL.  I thought that might be the easy part of the challenge.

Next in just a few days, I did an initial dive starting with the getting started guide. I was delighted to learn that Vagrant will not only allow one to configure a real server, but will also allow one to install a virtual server on one’s personal computer.  That simplified the problem in that I would not need to bring Amazon Web Services or something else into the mix for this challenge.

I then did a quick survey of the various configuration management frameworks mentioned above.  I was looking for how to copy files from GitHub using one of the above configuration management software, called from a vagrantfile.  I decided instead to write a Linux shell script function imbedded in the vagrantfile.

I took the approach of doing my own simple configuration management with shell script because I wanted to hit my first goal of bringing up a website from scratch with a vagrantfile as quickly as possible and Vagrant software suggests in a few places to start with a shell script (here,  here, and here).  Also Vagrant has a catalog of pre-configured servers some of which have a ready to go web server stack and GitHub installed as well!  So all I needed to add was script to call GitHub on the server and do some file copying.

I may very well go back and use Docker or some other configuration management software to copy in the website files to my virtual server as a second pass.  Stay tuned.

Conclusion: I was surprised at the relative ease of use Vagrant provides to deploy a server with a single configuration file on one’s personal computer.  I know I just scratched the surface with Vagrant.  However if you’d like to try out Vagrant and bring up a website with a single vagrantfile and the command “vagrant up”, check out my solution at:


Trouble Shooting Section:

On 2/4/2016 after a reboot, running vagrant up in my test folder failed to make my web page available as expected at  Uninstalling and re-installing vagrant seemed to resolve the problem.

A mini Data Driven site in PHP and MySQL

I’ve been investigating some DevOps techniques – and for testing needed a website that Created, Updated, Deleted and Displayed data. I decided to write a minimal sample website.

Here’s the design:

Mini Data Driven Website

  1. Use a single index.php file
  2. Display an input for a “Key” and a “Value”
  3. Display a Submit button
  4. Display a table of existing keys and values
  5. Submit button posts to the same php file
  6. Index.php does the following before returning any text
    • Login to database
    • Retrieve posted key and value data
    • Special case: if key=”drop” and value=”table”, drop the table and exit
    • Create the empty key value table if it does not already exist
    • If key is supplied and value is not empty, check for pre-existence of key and either update the corresponding value or insert the key value pair
    • If key is supplied but value is empty delete matching key value pair
  7. That’s it!

Here’s a link to the source code:

Here’s a link to the working site: